Build and code a circuit to control the color of an LED using a distance sensor. If an object is less than 20 cm close to the distance sensor, the LED will be red, but if the object is over 20 cm far from the distance sensor then the LED will be green.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 30 min.
- 2 LEDs (1 green and 1 red)
- 2 Resistances 220
- 1 Ultrasonic sensor (distance sensor)
- 1 Build&Code UNO board
- 1 Protoboard
What is an ultrasonic distance sensor?
A distance sensor functions exactly as bat when it flies in the night without hitting any object. The bat sends ultrasonic waves while flying, and if this waves bounce back it means that there is an object near.
The ultrasonic sensor does the same thing. It sends ultrasonic waves and if they bounce back then it recognizes that there is an object near. The distance to the object is measured by the time it takes for the ultrasonic wave to bounce back.
The ultrasonic sensor is composed of two different modules: emitter and receiver. The emitter sends ultrasonic waves, which are sound waves that humans cannot hear (same waves that the bats send). These waves go forward until they hit an object. Then, they bounce back to the sensor, when the ultrasound is detected by the receiver.
Because the sound waves always travel at the same speed when they move through the air: 343 m/s, it is easy to know the distance of the detected object.
First, you must measure the time it takes the sound wave to go and come back to the sensor. Then, you divide this time by 2, so you know exactly how long it took the wave to hit the object. When you know the travel time (to hit the object), you have to multiply it by the speed the waves move through air (343 m/s). The result will be the distance where the object is.
Distance = 343m/s *time (seconds).
If the ultrasonic waves doesn’t come back, it means that there is no object and the sensor will inform that nothing is in front of it.
- The protoboard receives the electricity from the Build&Code UNO 5V Pin. And goes back to the Build&Code UNO through the GND Pin. All grounds of the circuit must be connected to each other so they have the same GND value. In the image, the GND is represented with a black cable, where all the components are connected to each other and to the GND board. The red cable represents the 5V, that supply electricity the ultrasonic distance sensor.
- To activate the green and red LED you have to connect them to a digital pin. These pins will be the ones that send electricity to the LEDs. In the image you will see that the red LED is connected to the digital pin 11 with a green cable and the green LED is connected to the digital pin 10 with a purple.
- The ultrasonic sensor has 4 pins. You have already connected 2 of them (5V and GND). The 2 other pins are ECHO and TRIG, which 2 digital pins, these pins are the ones that will send the information to the Build&Code UNO board. In the image you will see that the TRIG pin is connected to the digital pin 7 with a yellow cable, and the ECHO pin is connected to the digital pin 8 with an orange cable.
You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.
You will write a program that is constantly reading information from the ultrasonic sensor. According to the distance measured by the sensor the red or the green LED will turn on.
For this to happen you will use the next condition: If the distance is less than 20 cm, the red LED will be on and the green LED will be off; but if the distance is more than 20 cm, the green LED will be on and the red LED will be off.
To measure the distance you have to define TRIG Pin as an output and ECHO as an input. TRIG will send the ultrasonic waves forward and ECHO will be waiting for the waves to come back.
Then, each time a wave comes back, you have to measure the distance of the object. Remember that the speed of sound in the air is always: 343,2 m/s.
So if you know the total time of the wave to go and come back you will have to calculate the distance. For that, you have to divide by 2 the total time the wave goes and comes back and then you multiply it by the speed of sound in the air:
Distance = 343m/s *time (seconds)
To work with a better resolution, you will work with cm/us (centimeters / microseconds) using the next conversion:
Once you have calculated the distance you will need to turn the red or the green light. For that to happen, you have to determine as output both pins where you have the connected the two LEDs. And when you want to turn on one of them you have to give it a HIGH value ( 1) and when you want it to be off you have to give it a LOW value (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:
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
When the sensor detects an object less than 20 cm away, the red LED will turn on and when the distance is higher than 20 cm, the green LED will turn on.