1

I'm using a Pi Zero W with a soldered project board to interface two ir Sensors and trying to make an HC-SR04 sensor work. I'm using the 5v supply to power all three sensors. I've set up a Motion.py program so that I can easily move the sensors to different GPIO ports and try PUD_UP/PUD_DOWN. I've connected the SR04 directly to a GPIO port, and I've also fed it thru a 1K-2K voltage divider circuit. I've swapped out two different SR04 sensors. But the bottom line is I simply cannot get the Echo signal to respond; is seems to be stuck at the 0v level. Basic SR04 code (python 3) is as follows:

import time
Import RPi.GPIO as GPIO
Trigger = 3
Echo = 36
GPIO.setup(Trigger, GPIO.OUT)
GPIO.setup(Echo, GPIO.IN,pull_up_down = GPIO.PUD_DOWN) # also tried PUD_UP
try:
while True:
GPIO.output(Trigger, False)
time.sleep(2)
GPIO.output(Trigger, True)
time.sleep(0.000002)
GPIO.output(Trigger, False)
if GPIO.input(Echo)==1:
GPIO.output(led_Blue, True) # Blue LED is setup as a signal output.
else:
GPIO.output(led_Blue, False)
if Test_Break == False:
continue
else:
break
finally:
GPIO.cleanup()

I also have two ir sensors and two LEDs running in the try: loop. I can easily swap the echo input with one of the ir sensors to confirm that the GPIO port is still working. I also use one of the ir sensors to toggle the Test_Break variable so that I can cleanly break the program and use the GPIO.cleanup() function. I've concluded that I can feed 5v signals to the GPIO and it works for the ir sensors. The ir sensors also work after I've tried the SR04 and moved it to another port. The PUD_DOWN parameter works for them ir sensors, but does not help the SR04 sensor. Neither does a 1K to 2K voltage divider.

Any ideas or suggestions would be greatly appreciated.

  • Let me see. You have 2 IR sensors and 1 US sensor. The IR sensors have been separately tested OK. It is only the US sensor that does not respond. So the question boils down to "How come my ultrasound sensor HR04 does not respons?“ – tlfong01 Apr 18 at 2:01
  • 1
    We need a clear photo of the connections between the Pi and sonar ranger. You do need to use a resistor divider on the echo line to drop the 5V output to a Pi safe 3V3. – joan Apr 18 at 3:11
  • You must not feed 5V into a Pi GPIO. Your PIR may run from 5V but if they are like mine the output pin is 3V3. – joan Apr 18 at 6:48
0

Question Summary

Trying to make HC-SR04 sensor work

SR04 sensor does not respond

Short Answer

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.

HC-SR04 module

HC-HR04 Operation

Now a more detailed timing diagram.

sr04 timing

Timing Summary

  1. Rpi sends 10μs width trigger pulse to HR04
  2. HR04 sends eight 40kHz signal, detects echo, and "calculates" distance
  3. HR04 sends a long output pulse proportional to distance
  4. Rpi reads the long pulse and calculates the distance
  5. If nothing to echo back, HR04 outputs a 38mS pulse
  6. Sample period should be less than 50mS, or frequency = 1/50mS = 1000/50 = 20 samples/second

SR04 Hardware Setup

sr04 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, ...

sr04 calibration

/ to continue, ...

Long Answer

I usually begin googling things, doing some experiments, then programming, ...

/ to continue, ...

References

tlfong01's little python serial port test program version 16.0

Research Notes

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 distance sensor.

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
import time

def distance(measure='cm'):
    try:
        gpio.setmode(gpio.BOARD)
        gpio.setup(12, gpio.OUT)
        gpio.setup(16, gpio.IN)

        gpio.output(12, False)
        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
        else:
            print('improper choice of measurement: in or cm')
            distance = None

        gpio.cleanup()
        return distance
    except:
        distance = 100
        gpio.cleanup()
        return distance


if __name__ == "__main__":
    print(distance('cm'))

/ to continue, ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.