Learn to code

ARDUINO & CODES

Previous Post
Connecting LEDs with Arduino
Next Post
Adjusting the color of a RGB LED

Adjust the brightness of an LED with PWM for Arduino

in Arduino Visual Output

In this tutorial, you will make a LED dimmer by using a potentiometer to control the brightness of the LED. The potentiometer is like a button that sends an analog signal to the Arduino Board. We will use PWM to manipulate the signal from the Arduino Board to the LED.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Dupont Wires Dupont Wires × 1
10K Potentiometer 10K Potentiometer × 7
220 ohm resistor 220 ohm resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Pulse Width Modulation

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED, for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino’s PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

BreadBoard Layout

We will use the same circuit diagram as in the previous tutorial. 4.4 Reading analog values. For more information about the circuit diagram please read that tutorial. One adjustment is made since we are using PWM we need to connect the LED to a pin that supports this. Therefore, we connect the LED to pin 3 instead of 13.

The Code: Fading LED

// code for controlling the brightness of LED by a Potentiometer
int potPin = A0 ; // analog pin from the potentiometer
int potValue = 0; // value of reading the potentiometer
int LED = 3; // pin that is connected to the LED
void setup() {
  pinMode(LED, OUTPUT); // declare Pin 9 as an output pin
}
void loop () {
  // reading the value of the potentiometer
  potValue = analogRead(potPin);
  // send a value to control brightness LED
  // we need to convert the value of 0 - 1023 to 0 - 255
  // therefore we devide the reading by 4
  analogWrite(LED, potValue / 4);
  delay(10); // delay for 10 ms
}

Code Explanation

After you wired everything correctly we can begin with the coding part. First, we will declare our variables.

int potPin = A0 ; // analog pin from the potentiometer
int potValue = 0; // value of reading the potentiometer
int LED = 3; // pin that is connected to the LED

 

The next step is to set the ledPin as an output pin in the void setup() part of our sketch.

 pinMode(LED, OUTPUT); // declare Pin 9 as an output pin

Now we are ready to code the loop.  We assigned a variable for the readings of the Potentiometer. To be able to read the value of the potentiometer we need to read the analog pin.

The analogRead() command converts the input voltage range, 0 to 1023, to 0 to 5 voltage. After the reading is successful we are able to control the brightness of the LED depending on the value that is read. An interesting part of this sketch is the way we convert the reading to a digital number for controlling the LED.

First, we need to consider that PWM uses a range of values between 0 and 255 while analogRead() returns a value between 0 and 1023. Therefore we will divide the value of analogRead() by 4. By doing this we are creating values that work with PWM.  If you like, you can change the number 4 to 8 to see the effect.

To send the reading of the potentiometer to the LED, we are using the analogWrite() command.

// reading the value of the potentiometer
  potValue = analogRead(potPin);
  // send a value to control brightness LED
  // we need to convert the value of 0 - 1023 to 0 - 255
  // therefore we devide the reading by 4
  analogWrite(LED, potValue / 4);
  delay(10); // delay for 10 ms

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu