PROJECT Nº 5: Control the light sensor

Learn how to create a program so that when the light sensor doesn’t detect light, a LED turns on, and when the light sensor detects light, the LED turns off.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 Green LED
  • 1 Analog Light Sensor
  • 1 USB – Micro USB cable
  • Computer

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

What is an analog light sensor (LDR)?

An analog light sensor is a resistor whose resistance decreases with increasing incident light intensity. It is also called photoresistor.

CONNECTIONS:

  1. Connect the analog light sensor to the analog port A0 of the Build&Code 4in1 board.
  2. Connect the LED to the digital port 10 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 pinlight = A0, luz; // LDR
    int led10 = 10; // LED PIN
    void setup() {
      // put your setup code here, to run once:
      // LEDS CONFIGURATION
      pinMode ( led10, OUTPUT);
    }
    void loop() {
      // put your main code here, to run repeatedly:
      //READ THE LIGHT INTENSITY FROM THE LIGHT SENSOR AND TURN ON OR TURN OFF THE LED10 BASING ON THE AVERAGE VALUE
     luz = analogRead( pinlight); // READING OF THE LIGHT INTENSITY OF THE LIGHT SENSOR 
     if (luz > 400) // IF THE VALUE IS HIGHER THAN 400
      {
        digitalWrite ( led10, LOW); // LED10 = OFF
      }
      else // IF THE VALUE IS LOWER THAN 400
      {
        digitalWrite ( led10, HIGH); // LED10 = ON
      }
    }
    
  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 covering the light sensor you will get a value lower than 400 and the LED will turn on. When the value is higher than 400, the LED will turn off.

1 0

0