Professional Documents
Culture Documents
Measures The Distance of A Target Object
Measures The Distance of A Target Object
target object by emitting ultrasonic sound waves, and converts the reflected
sound into an electrical signal.
Ultrasonic sensors have two main components:
Transmitter (which emits the sound using piezoelectric crystals)
Receiver (which encounters the sound after it has travelled to and from the target).
In order to calculate the distance between the sensor and the object, the
sensor measures the time it takes between the emission of the sound by the
transmitter to its contact with the receiver.
The formula for this calculation is D = ½ T x C (where D is the distance, T is
the time, and C is the speed of sound ~ 343 meters/second).
DAY-5 Arduino Programming Workshop 2
Ultrasonic sensors are used primarily as proximity sensors.
They can be found in automobile self-parking technology, anti-collision safety
systems, robotic obstacle detection systems also used as level sensors to
detect, monitor, and regulate liquid levels in closed containers.
The HC-SR04 Ultrasonic Module has 4 pins, Ground (Gnd), VCC (5V) , Trig
and Echo. The trig and echo pins to any Digital I/O pin on the Arduino Board.
Then the HC-SR04 automatically sends Eight 40 kHz sound wave and wait for rising
edge at Echo pin.
When the rising edge capture occurs at Echo pin, start the Timer and wait for falling
edge on Echo pin.
As soon as the falling edge is captured at the Echo pin, read the count of the Timer. This
time count is the time required by the sensor to detect an object and return back from
an object (to and from).
When no obstacles then timeout after 38ms When obstacle detected pulse width of 50 µS to 25 mS received
DAY-5 Arduino Programming Workshop 5
Read the distance value from HC-SR04 and print it on serial monitor
Write a program to read distance value from HC-SR04 ultrasonic sensor module in cm
and print it on the serial monitor. Simulate and verify this logic on Arduino Uno using
Tinkercad circuits simulator.
pulseIn() - Reads a pulse (either HIGH or LOW) on a pin. For example, if value is
HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for
the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or
gives up and returns 0 if no complete pulse was received within the timeout.