This project uses the HC-SR04 Ultrasonic Sensor with a common anode RGB LED to act as a proximity sensor with visual feedback. The aim is to create a device where the LED fades from green to red as an object is moved towards the device.
TheoryThe HC-SR04 Ultrasonic Sensor works by using sound navigation and ranging (SONAR). The sensor emits an ultrasonic sound wave at a frequency of 40kHz. This wave then hits an object and is reflected back to the sensor. The sensor measures the time taken for the wave to be emitted, hit the object, then return to the sensor. Figure 1 shows the timing diagram for the sensor which shows a trigger input to send an ultrasonic sound wave. The echo pulse signal is then returned to the sensor in proportion to its range.
The distance the wave travels is equal to twice the distance of the object, as the wave goes to the object and back. Therefore, the equation distance = speed ofsound* time / 2 can be used to calculate the distance of the object.
MethodologyThis project uses an Arduino Leonardo clone (ProMicro), however can be recreated using other microcontrollers.
The Trig and Echo pins are to be connected to the digital out pins of the Ardunio, in this case pins D9 and D8 respectively. The RGB LED is connected as shown in the circuit diagram and a more detailed explanation can be found within my guide entitled 'Common Anode RGB LED'.
The Trig pin is set to HIGH for 10µs to send an 8 cycle sonic burst which is reflected off an object and received by the Echo pin. The distance is then calculated using distance = speed ofsound* time / 2 and can be read using the serial monitor using Serial.print. The LED will also visually display the distance by fading from red to green showing a distance of 20cm to 0cm.
Comments