Author's Picture
Author: Joel Gray Published: 25 August 2020 Read Time: ~4 minutes

How to Control LED Brightness using Arduino

How to Change the Brightness of an LED Using a Potentiometer

Here we will be altering the brightness of an LED by turning the dial of a potentiometer. You could use the principles of this project to make yourself a dimmable light or lamp. A potentiometer is also known as a variable resistor. This restricts the current provided to the LED and as voltage is dropped over the potentiometer it reduces the voltage across the LED, therefore cause the change in brightness.

This project can be easily edited using more LEDs/potentiometers, different coloured LEDs and possibly combining it with some of our other projects. Always try to be creative and do a little but more than what the tutorial shows you.

Links to what you’ll need:

1x Breadboard

1x Arduino Uno

1x 330 Ohm Resistor

1x Potentiometer

1x LED (Any colour)

6x Jumper Wires

1x USB-A Cable

How do I start?

The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.

The positive led of the LED is connected to Pin 3 via a current limiting resistor and the negative led of the Green LED is connected to the Ground Pin via a jumper wire.

The middle pin of the potentiometer is connected to Pin A0 which is an Analog Input pin. Looking at the potentiometer from the back the left pin is connected to Ground and the right pin is connected to 5V Pin.

A quick summary of the circuit can be seen below:

Pin 3 > Resistor > LED > Ground Rail

Pin 11 > Resistor > Amber LED > Ground Rail

Pin 1 of Potentiometer > Ground Rail

Pin 2 of Potentiometer > A0

Pin 3 of Potentiometer > 5V Pin

Ground Rail on Breadboard > Jumper Wire > Ground Pin


Let’s run through what the code is doing

The following code is used to implement the project by uploading it to the Arduino Microcontroller.

We initialise the Pins that the have connections. Eg. ‘int ledPin = 3;’

Inside the Setup function we use the pinMode function to set our Pins to which the LED are connected to be Outputs and set the Potentiometer pin as an Input.

Then inside the main loop function we are using the analogRead function to read the analog value of the potentiometer pin.

We then calculate the correct value to which we should set the output of the LED. To set the value of the LED to the correct level, we use the analogWrite function.

When we are reading from an analog pin between 0 – 5V it is read by the Arduino as values between 0 – 1023 and when we write an analog voltage the Arduino writes this in values between 0 – 225. Therefore, we are required scale these values between 0 – 225 to get an appropriate value that the Arduino will write the LED. We do this by the following equation:

Write Value = (255/1023) * Read Value

Check out the information given on Arduino documentation to learn more.


Now open the Arduino IDE and copy the code below

If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.

  
// This code will use a potentiometer to control the brightness of an LED.



int PotPin = A0;                       // Setting the analog pin that the potentiometer will be connected to
int ledPin = 3;                       // Setting the pin of the LED 
int readVal;                           // This is a variable to read the value coming from the potentiometer
int writeVal;                          // This is a variable to write the value coming from the potentiometer


void setup() {
  pinMode(PotPin, INPUT);              // Sets the Potentiometer to an input
  pinMode(ledPin, OUTPUT);             // Sets the LED as an output  
}

void loop() {
  readVal = analogRead(PotPin);        // Reading the value from the potentiometer 
  writeVal = (255./1023.) * readVal;   // Calculating the value which will be writen to the LED **
  analogWrite(ledPin,writeVal);        // Writes the calculated value to the LED
}

/*

 - When reading an Analog voltage between 0 - 5 volts it is read by the arduino as 0 - 1023.
 -  When writing an Analog votlage between 0 - 5 volts it is read by the arduino as 0 - 225.
 - We must scale the values between 0 - 225 hence the calculation on line 18
 
*/


/*
  created 02/2020
  by Jamie Buick 

  graycode.ie
*/