PROJECT Nº 2: LED Brightness Control

Build and code a program that allows you to control an LED brightness using a potentiometer. An LED is a light-emitting diode that emits light when electricity passes through it.

What is an LED?

An LED (Light Emitting Diode) has an Anode and a Cathode, the anode is the positive side and the cathode is the negative side.

You can distinguish between the anode and the cathode by looking at its legs, the larger leg is the anode and the shorter leg is the cathode. The electricity can only pass through in one direction, from the anode to the cathode and for it to pass through it is essential that the voltage is higher in the anode than in the cathode.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 LED
  • 2 Resistance 220
  • 1 Potentiometer
  • 1 Build&Code UNO board
  • 1 Protoboard

CONNECTIONS:

  1. The protoboard will receive the electricity from the 5V pin of the Build&Code UNO, and then it will go back to the Build&Code UNO through the GND pin (Ground). This is why all the GNDs of the circuit must be connected to each other in order for them to have the same GND value. In the image, the GND is represented with the black cable, in which all the Ground are connected with each other and to the board’s GND. The red cable represents the 5V, that feeds both the servomotor and the potentiometer.
  2. Connect the potentiometer central pin to an analog pin, because the potentiometer is an analog sensor. The image shows a blue cable connecting the potentiometer and the analog pin A0. This cable transmits information from the potentiometer to the board in order to read the values of the sensor.
  3. Connect the LED to a PWM digital pin, which is the one that will control the LED brightness depending of the information received from the sensor (potentiometer). The image shows how the LED connects to the digital pin PWM with a blue.

PROGRAMMING CODE

You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

You will code a program that will constantly read the potentiometer information. Depending on the information received from the sensor (in this case, the potentiometer), the LED will bright more or less.

Depending on how much you turn the potentiometer, it will provide data from 0 to 1023.

The digital PWM pin will work as an output pin, but instead of giving a high or low value, it can measure and give middle values. This way the LED can have different levels of brightness.

For example, if an LED turns on and off for 100 times in 1 second, and 50% of the times is on and the other 50% is off, it means that the human eye will perceive the brightness at 50%. But if the LED is off most of the time (for example, 75% off and 25% on) then the human eye will detect less brightness.

The PWM has a 256 states resolution, it goes from 0 to 255. If you write 15, it will bright very low; if you write 127, it will bright at 50%; if you write 255, the brightness will be at 100%.

To relate the analog input of the potentiometer with the PWM digital pin, you have to divide the two maximum values:

build-and-code-actividad-2

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    int input = A0; //Define the analog input A0
    
    int LEDR = 9;  //Define the digital pin 9 as PWM digital output 
    
    
    int value=0; // Create an integer variable value to store the values of the analog readings
    
    
    
    void setup ()
    { 
     pinMode(LEDR,OUTPUT); // Define the digital pin as output
    }
    
    void loop()
    {
    
     int value = analogRead(input); // Read the potentiometer value
    
    
    
     analogWrite(LEDR, value/4); // Because the analog inputs have a maximum resolution of 1024 states and the PWM has a resolution of 256, you need to divide the input value by 4 so the analog input reading is proportional with the intensity output of the PWM
    
    }
    

Code for the visual programming software by blocks compatible

  1. Download and install the program.
    1.1 Open the software.
    1.2 Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
  2. Open the program and copy the following code. Use the following image as a guide:

RESULT OF THE EXERCISE

The program will be reading the information sent by the potentiometer constantly. Basing on the received from the sensor, the LED will bright more or less.

1 0

0