PROJECT Nº 2: Control the intensity of the LEDs

Learn to create a program to control the intensity of the LED indicators by doing these two exercises.

EXERCISES INDEX: 

  • Exercise 2.1: Use the potentiometer to vary the brightness of an LED
  • Exercise 2.2: Use the potentiometer to vary the brightness of two LEDs inversely 

Exercise 2.1: Use the potentiometer to vary the brightness of an LED

Create a program to control the brightness of an LED by using the potentiometer. 

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 20 min.

MATERIALS:

  • 1 Green LED
  • 1 Potentiometer
  • 1 USB – Micro USB cable
  • Computer

The Mini Lab will have to be built according to the instructions manual.

CONNECTIONS:

  1. Connect the LED to the digital port 10 of the Build&Code 4in1 board.
  2. Connect the potentiometer to the digital port A1 of the Build&Code 4in1 board.

PROGRAMMING CODE:

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

To control the brightness of the LED, you have to connect it to the digital port with PWM output. Then, the digital port PWM will be working as an output port, but instead of giving a constant high or low value, it will be capable of changing the values very fast in order to present medium values too. This allows different levels of brightness.

The PWM output has a resolution of 256 states, from 0 to 255. If you write 15, it will barely turn on, since it will be more time off than on. If you write 127, it will bright with 50% of its maximum power, since it will be 50% time off and 50% time on. If you write 250, it will bright almost at its 100% capacity.

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

This means that if you divide the values of the sensor by 4, you will establish a lineal relation between the rotation of the potentiometer and the LED brightness.

Arduino Code 

  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 led10 = 10, brightness; // PORT 10 AND BRIGHTNESS VALUE OF THE LED 
    int pot = 0, pinpot = A1; //PORT A1 AND POTENTIOMETER VARIABLES
    
    void setup() { // put your setup code here, to run once: 
    pinMode (led10, OUTPUT); 
    
    } 
    
    void loop() { 
      // put your main code here, to run repeatedly: 
    pot = analogRead (pinpot); //POTENTIOMETER VALUE READING
    brightness = map (pot, 0,1023, 0, 255); // SCALE THE VALUES OF THE POTENTIOMETER WITH THE LED BRIGHTNESS
    analogWrite (led10,brightness); 
    }
    
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware
    • Software
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE:

When rotating the potentiometer, you will vary the intensity of the LED.

 

Exercise 2.2: Use the potentiometer to vary the brightness of two LEDs inversely

Learn to create a program to control the brightness of two LEDs inversely by using the potentiometer. This way, when one LED is at its maximum, the other one will be completely off.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 green LED
  • 1 red LED
  • 1 Potentiometer
  • 1 USB – Micro USB cable
  • Computer

The Mini Lab will have to be built according to the instructions manual.

CONNECTIONS:

  1. Connect the LEDs to the digital pins 10 and 3 of the Build&Code 4in1 board.
  2. Connect the potentiometer to the analog pin A1 of the Build&Code 4in1 board.

PROGRAMMING CODE:

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code

  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 led10 = 10, led3 = 3, brightness10, brightness3; // PIN AND BRIGHTNESS VARIABLE FOR LED 10 AND LED 3
    int pot = 0, pinpot = A1; // POTENTIOMETER PINS VARIABLES
    void setup() {
      // put your setup code here, to run once:
      // LEDs CONFIGURATION
      pinMode ( led10, OUTPUT);
    pinMode ( led3, OUTPUT);
    }
    void loop() {
      // put your main code here, to run repeatedly:
      pot = analogRead (pinpot); // POTENTIOMETER VALUE READING
     brightness10 = map (pot, 0,1023, 0, 255); // SCALE THE POTENTIOMETER VALUES WITH THE LED BRIGHTNESS
     brightness3 = map (pot, 1023,0, 0, 255); // SCALE THE POTENTIOMETER VALUES WITH THE LED BRIGHTNESS
      analogWrite (led10,brightness10); // LED 10 BRIGHTNESS
      analogWrite (led3,brightness3); // LED 3 BRIGHTNESS
    }
    
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code 

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware
    • Software
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE:

When rotating the potentiometer, you will vary the intensity of the LEDs.

 

1 0

0