This project is created as my journey on Learning IoT.
Things
- Arduino Uno
- LCD with i2c
- HY-SRF05 Module
HY-SRF05 is a module to read Distance with Ultrasonic, This module has 5 pin:
- VCC
- GND
- Out (not used)
- Trigger (Digital) | Output
- Echo (Digital) | Input
HY-SRF05 work based on Principle of Ultrasonic Echo-Ranging.
This module has 2 sensors, one to trigger an ultrasonic waves, and one to receive ultrasonic waves.
Trigger Sensor, send a short pulse to the trigger Pin (about 10 microseconds), then Trigger Sensor generate a burst of ultrasonic pulses. The sound waves will travels through the air, then after hit an object, the waves will be reflected. The reflected waves will be received by Echo Sensor.
We can calculate duration using method buildIn pulseIn.
With the duration of ultrasonic waves traveled, we can count the distance with it.
unsigned long duration = pulseIn(echoPin, HIGH);
unsigned long distance = duration * 0.034 / 2;
What is 0.034? Where is it Come from?
As we all know, the speed of the sound is 340 m/s, then we need to change it as cm/microseconds
340 * 100 / 1 * 1000000
Then, Because the duration that we get is the duration of wave traveling from Trigger Sensor -> Object -> Back to Echo Sensor. So it is a round-trip, to calculate only from Sensor to Object we can divided it by 2.
That's the distance in centimeters.
Instead of having to pulse, and calculate, there's an awesome library we can use to calculate distance with HY-SRF05 Module called New Ping. Easy Peasy.