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:
- Connect the analog light sensor to the analog port A0 of the Build&Code 4in1 board.
- 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
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- 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 } }
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- 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
- Download and install the program.
- Open the software and copy the following code. Use the following image as a guide:
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
Bitbloq code
- Download Bitbloq and install the Web2board app.
- Open the software and copy the following code.
- Hardware
- Software
- Hardware
- Configure and upload the code, following the indications on the Mini Lab First Steps guide.
- 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.