Build and code a circuit that will control a buzzer depending on the quantity of light it receives, simulating a sunrise wake up alarm.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 30 min.
MATERIALS:
- 1 photoresistance
- 1 potentiometer 10k
- 1 Build&Code UNO board
- 1 Protoboard
- 1 Buzzer
What is a light sensor?
A light sensor is a resistance that changes its value depending on the light received, so if it receives a lot of light (simulating a sunrise) it can activate a buzzer (simulating an alarm). If it doesn’t receive that much light, the buzzer will stay off.
The light sensor operation is very simple: it has an inner resistance that changes its value depending on the light received. If you put the sensor in the dark, its value will be close to 0, and if you put the sensor under a light bulb its value will be close to 1023.
CONNECTIONS:
- 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 supplies the photoresistance.
- To activate the buzzer you must connect it to a digital pin. This pin provides electricity to the buzzer to make it make it sound. If it doesn’t provide electricity, the buzzer will not sound. The image shows a green cable connecting the digital pin 5 with the buzzer.
- Connect in series the light sensor with the resistance. You have to connect this common connection to an analog input of the Build&Code UNO board. This way, the board will be able to read all the values from the sensor. The image shows a yellow cable connecting the photoresistance with the analog pin A0, to allow the readings of the sensor.
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 write a code that will be continuously reading the light sensor information. If the sensor receives a lot of light, it will send values around 1000, but if the sensor receives a small amount of light it will send values around 0. So, knowing this, you can set a value that will simulate the sunrise (when the alarm should sound). You can set, for example, at the 850 value.
Then you must set a condition, and depending if this condition is met or not, the program will behave in one way or another.
The condition will be: if the light sensor value reading is under 850 then it still the “night” so the buzzer will not sound (the condition is met). On the other hand, if the light sensor value reading is over 850 (the condition is not met) so it is “day” so the buzzer must sound.
To make the buzzer sound, you must program the digital pin as an output (in this example it is pin 5). Digital pin 5 will be the one that provides electricity to the buzzer to make it sound when the condition is not met (when the value reading is over 850).
If you want to turn on the buzzer you must give it a high value (High or 1) and when you want the buzzer to turn off you must give it a low value (LOW or 0).
- 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 ldr = A0; //Light sensor connected to analog pin A0 int buzzer = 5; //Buzzer connected to the digital pin 5 int value = 0; // Value created to store the sensor values void setup() { pinMode(buzzer, OUTPUT); //Configure the digital pin 5 as an output } void loop() { value = analogRead(ldr); //Instruction to read and store the analog value (from 0 to 1023). The can be from 0 (almost no light) to 1024 (a lot of light) if (value < 850) //condición que define que recibe poca luz { digitalWrite(buzzer, 0); // If it receives a small amount of light, the buzzer digital output will be in LOW value (0) so it won’t sound } else //if the condition is not met { analogWrite(buzzer, value/4); //it means it receives a lot of light so the buzzer digital output will be sending data so it will sound } }
Code for the visual programming software by blocks compatible
- 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. - Open the program and copy the following code. Use the following image as a guide:
RESULT OF THE EXERCISE
Depending on the values detected by the light sensor and the condition established, the buzzer will sound when detecting a value higher than 850, simulating a sunrise wake up alarm.