Arduino LED brightness in three easy steps.

In this guide, you will learn how to change the brightness of an LED. For this project you will need the ‘Arduino Starter kit’ which is available here. See picture of the final result below.

First of all, we will make the circuit. You will need an LED, a resistor and two wires. Make sure to connect the short side of the LED to the resistor and the long side to pin five. If the led doesn’t turn on, you might want to turn it around.

LED dimmer schematic

Then what you’ll want to do is install the Arduino IDE, make a new file and call it whatever you like. Now that we’ve made a new file, we’re ready to program.

In this piece of code we define pin five as an output pin. This way the Arduino knows that we want to be able to turn on that pin later on in the code.


void setup() {
  pinMode(5, OUTPUT); //setting pin 5 as an output pin.
}

The next piece of code will repeat endlessly. Most Arduino programmes have this loop. Inside of this loop, we will place a for loop. In this case, we use this loop to change the variable i from 0 to 255. Then we tell the Arduino to turn on PWM on the fifth pin. This PWM signal will turn on and off the pin which will turn on and off the LED. In this way, it will seem like the LED is more or less bright.

void loop() { // The program will loop this piece of code.
   for (int i = 0; i <= 255; i++) { 
//A loop that changes the variable i from 0 to 255.
    analogWrite(5, i); // Changing the brightness of the led.
    delay(10); // Waiting 10 microseconds.
  }
}

Now that we have written our code, we can put it on the Arduino. First of all, save the code by going to the top left and clicking on file --> save or pressing ctrl + s. Make sure you have selected the right board by going to Tools --> Board and selecting Arduino Uno. Then plug in the USB cable and check that you have selected the correct port by going to Tools --> Port. After checking everything, you can Upload everything to the Arduino by clicking on the arrow pointing to the right in the top left of the screen.

One thought on “Arduino LED brightness in three easy steps.

  1. Pingback: Using a potentiometer with Arduino to turn on LEDs - Atalla

Leave a Reply

[]