Trying to make HC-SR04 sensor work
SR04 sensor does not respond
I usually first give a long answer, with research notes, then summarize the long answer to a short answer, then delete the long answer, ...
And my short answer usually begin with pictures.
Now a more detailed timing diagram.
- Rpi sends 10μs width trigger pulse to HR04
- HR04 sends eight 40kHz signal, detects echo, and "calculates" distance
- HR04 sends a long output pulse proportional to distance
- Rpi reads the long pulse and calculates the distance
- If nothing to echo back, HR04 outputs a 38mS pulse
- Sample period should be less than 50mS, or frequency = 1/50mS = 1000/50 = 20 samples/second
SR04 Hardware Setup
Now I am doing calibration. I am placing the reflecting object about 30cm from SR04. Then I applied a 4 mS pulse every 150mS. I found the echo pulse is around 2mS. Now I need to do the calculation, ...
/ to continue, ...
I usually begin googling things, doing some experiments, then programming, ...
/ to continue, ...
tlfong01's little python serial port test program version 16.0
The following are random, unstructured notes which I just now googled and have not read thoroughly and therefore might be rubbish, and links broken. They will be deleted or tidied up after I have completed the short answer, ...
HC-SR Datasheet - Arm Mbed
HC-SR04 Datasheet - P Matrin
HC-SR04 Schematics - Arm Mbed 2014
HC-SR04 Schematics - Elec Freaks
HC-SR04 Schematics - ITead Studio
HC-SR04 Specifications Summary
Python Programming Ultrasonic Distance sensor HC-SR04 -
Raspberry pi with Python for Robotics 10 - Programming Distance Sensor HC-SR04 - sentdex 2014may02 30,363 views
HC-SR04 Specifications Summary
HC-SR04 is an ultrasonic ranging module that provides 2 cm to 400 cm
non-contact measurement function.
The ranging accuracy can reach to 3mm and effectual angle is < 15°. It
can be powered from a 5V power supply
Working Voltage: DC 5V Working Current: 15mA Working Frequency: 40Hz
Max Range: 4m Min Range: 2cm Measuring Angle: 15 degree Trigger Input
Signal: 10µS TTL pulse Echo Output Signal Input TTL lever signal and
the range in proportion Dimension 45 * 20 * 15mm
HC-SR04 Notes - Arm Mbed
The HC-SR04 is one of the lowest cost Sonar-based distance sensor
options available (as low as $3 US). There seem to be several hardware
versions and/or clones that work the same as far the functions of the
two pins (trigger and echo). It operates off of 5V DC power at around
15 mA (not active is 2 mA).
The detection range is 3-400 cm with around a 15 degree beam width.
Like most Sonars, it more readily detects large hard objects that
reflect sound, so it might not see something soft like a fluffy cat.
It uses a short 40 kHz ultrasonic ping that humans can't hear and then
listens for the ping signal to return after reflecting off an object.
The time delay for the reflected signal to echo back is used to
measure the distance using a simple scaling calculation based on the
speed of sound in air.
There are two tranducers, one to transmit and one to receive. Two
transducers turn out to be cheaper, since a higher voltage is needed
to transmit and switching modes using only one transducer takes a lot
of analog circuitry.
Small Sonar sensors are often used in robots to detect objects.
Sometimes they are used along with an IR-based sensor to improve the
likelihood of object detection. Some robots and other devices even add
a mechanical bump or limit switch. To detect objects in different
directions, some robots rotate sensors on a turret or use several
sensors mounted facing out at different angles .
HC-SR04 Operation - Arm Mbed
As seen in the timing diagram above, only two signal pins are used.
Trigger starts a measurement cycle and sends out a short ultrasonic
pulse (eight cycles at 40Khz) and then listens for a reflected signal
(echo). Several cycles at 40Khz are needed for the analog receiver
circuit to detect the reflected signal. The width of the echo pulse
output pin indicates distance. A hardware timer would typically be
used to measure the echo pulse width. A simple divide operation can
then scale the value to cm or inches, if needed.
The device should not be triggered again until waiting for the longest
possible echo return time delay (maximum detection distance times
speed of sound in air). This prevents any echos from the previous
ultrasonic pulse interfering with the next pulse measurement or the
new outgoing pulse being heard as the echo from the previous pulse.
Something a bit over 10 measurements per second is typical on most
small sonar sensors. The HC- SR04 data sheet suggests a min cycle time
of 60 ms.
Python porgramming tutorial - pythonprogramming.net
This tutorial covers how to write Python code to work with the HC-sr04
The distance sensor works by shooting ultrasonic waves, calculating
the amount of time between sending the signal and receiving it.
We can use this time, and our knowledge of the speed of sound constant
to calculate distance.
It should noted here that the distance sensor works well even on some
slants, if the object that you're bouncing sound off of is more than a
33 degree angle, your results are likely to be very inaccurate.
What this script will do for us is convert the sensor time to a
distance. For now, we just have it printing out the distance, but
we're going to use this script later as an import into our car, so
that it can read distances and either be semi-auto-piloted, braking
when objects get too close, or we can allow the car to just run on its
own until it detects a close object.
Example Program - pythonprogramming.net
import RPi.GPIO as gpio
while gpio.input(16) == 0:
nosig = time.time()
while gpio.input(16) == 1:
sig = time.time()
tl = sig - nosig
if measure == 'cm':
distance = tl / 0.000058
elif measure == 'in':
distance = tl / 0.000148
print('improper choice of measurement: in or cm')
distance = None
distance = 100
if __name__ == "__main__":