PROJECT Nº 1: Turn the LEDs on and off according to the light

Create a program to turn the LEDs on and off according to the light detected by the analog light sensors.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE:  20 min.

MATERIALS:

  • 1 Green LED
  • 1 Red LED
  • 2 Analog Light Sensors
  • Computer

The Code&Drive will have to be built according to the instructions manual.

What is an LED?

An LED is a light-emitting diode and it emits light when electricity passes through it. It has an Anode and a Cathode. “LED” comes from “Light Emitting Diode”.

What is an analog light sensor?

An analog light sensor is a resistor that variates its electric resistance basing on the amount of light that strikes on it. It can also be called photoresistor.

CONNECTIONS:

  1. Connect the light sensors to the analog pins A0 and A1.
  2. Connect the LEDs to the digital pins 9 and 10.

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 PinSpeedMotorA = 5, PinSpeedMotorB = 6; // DIGITAL PIN FOR THE MOTORS SPEED
    int PinLED1 = 9, PinLED2 = 10; // DIGITALPIN LED1 AND LED2
    int PinLight1 = A0, PinLight2 = A1, ValueLight1 = 0, ValueLight2 = 0;
    void setup() {
      // put your setup code here, to run once:
      // CONFIGURATION OF THE DIGITAL PINS
      pinMode(PinSpeedMotorA, OUTPUT);
      pinMode(PinSpeedMotorB, OUTPUT);
      pinMode(PinLED1, OUTPUT);
      pinMode(PinLED2, OUTPUT);
      // MOTORS SPEED A 0
      analogWrite(PinSpeedMotorA, 0);
      analogWrite(PinSpeedMotorB, 0);
    }
    void loop() {
      // put your main code here, to run repeatedly:
      ValueLight1 = analogRead(PinLight1); // SAVE THE VALUES OF THE LIGH SENSOR 1
      ValueLight2 = analogRead(PinLight2); // SAVE THE VALUES OF THE LIGH SENSOR 2
      if ((ValueLight1<400)&&(ValueLight2<400)) /IF THE LIGHT SENSORS DETECT VALUES LOWER THAN 400
      {
        digitalWrite(PinLED1, HIGH);// LED1 = ON
        digitalWrite(PinLED2, HIGH);// LED2 = ON
      }
      else // IF THE LIGHT SENSORS DETECT VALUES HIGHER THAN 400
      {
        digitalWrite(PinLED1, LOW); // LED1 = OFF
        digitalWrite(PinLED2, LOW); // LED2 = OFF
      }
    }
    
  3. Configure and upload the code, following the indications on the Code&Drive 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 Code&Drive 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 Code&Drive 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 covering the light sensors, they will receive values lower than 400 and the LEDs will turn on. When the light sensors detect values higher than 400, the LEDs will turn off.

 

1 0

0