1

I am working on a project with HC-SR04 Ultrasonic sensors. I am a beginner in raspberry-pi.

I have 3 sensors which are calculates the distance. But one of them has problem which is echo (input) pin always has HIGH value.

Here is my code : (You can mainly focus on the main function.)

    #!/usr/bin/env python

    # Import required modules
    import time
    import RPi.GPIO as GPIO

    class UltrasonicSensor:
      def __init__(self, sensor, gpioMode=GPIO.BCM):

        self.trig = sensor["trig"]
        self.echo = sensor["echo"]
        self.gpioMode = gpioMode
        GPIO.setmode(self.gpioMode)

        # set up GPIO pins
        GPIO.setup(self.trig, GPIO.OUT)
        GPIO.setup(self.echo, GPIO.IN)
    
      def get_distance(self):

        pulseStart = time.time()
        pulseStop = time.time()

        GPIO.output(self.trig, False) 
        
        GPIO.output(self.trig, True)
        time.sleep(0.00001) 
        GPIO.output(self.trig, False)
        
        while GPIO.input(self.echo) == 0:
            pulseStart = time.time()

        while GPIO.input(self.echo) == 1:
            pulseStop = time.time()
    
        # time difference between start and arrival
        pulseDuration = pulseStop - pulseStart
        # multiply with the sonic speed (34300 cm/s)
        # and divide by 2, because there and back
        distance = (pulseDuration * 34300) / 2
    
        return distance

Here is the main function:

    # Define pins
    sensor_R = {"trig":24 , "echo":23}
    sensor_C = {"trig":6 ,  "echo":5}
    sensor_L = {"trig":16 , "echo":25}

    if __name__ == '__main__':
      try:
        while True:
          sensorR = UltrasonicSensor(sensor_R)
          sensorC = UltrasonicSensor(sensor_C)
          sensorL = UltrasonicSensor(sensor_L)
          print ("Right  = %.2f cm" % sensorR.get_distance())
          print ("Center = %.2f cm" % sensorC.get_distance())
          print ("Left   = %.2f cm" % sensorL.get_distance())
          print ("")
            
          time.sleep(0.5)
 
      # Reset by pressing CTRL + C
      except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

For the sensor_L, pin 25 always returns 1, so that it could never enter the first while loop and never end the second while loop.

After such a research I run the gpio readall command. In that table, LOW is set as default for each echo(input) pins which are GPIO(BCM)5 and 23 but GPIO(BCM)25. (I have no idea how my board recognize it as input because I did not specify them explictly.)

Here is the table:

pi@raspberrypi:~ $ gpio readall
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
|   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | IN   | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 1 | 11 || 12 | 1 | ALT0 | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 1 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 1 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 1 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 1 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 1 | 21 || 22 | 1 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 1 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
|   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
|   5 |  21 | GPIO.21 |   IN | 0 | 29 || 30 |   |      | 0v      |     |     |
|   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
|  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
|  19 |  24 | GPIO.24 | ALT0 | 1 | 35 || 36 | 1 | IN   | GPIO.27 | 27  | 16  |
|  26 |  25 | GPIO.25 |   IN | 1 | 37 || 38 | 0 | ALT0 | GPIO.28 | 28  | 20  |
|     |     |      0v |      |   | 39 || 40 | 0 | ALT0 | GPIO.29 | 29  | 21  |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+

Additionaly, I have 2 motor driver card and 1 MAX98357 audio card connected.

Here is audio card pins:

DIN to Raspbery Pi #21
BCLK to Raspbery Pi #18
LRCLK to Raspbery Pi #19

Motor driver pins:

enable_L, fwd_L, bwd_L = 11, 9, 10
enable_R, fwd_R, bwd_R = 17, 27, 22
5
  • Perhaps it is not the GPIO pin always High, but the sensor always High, and this is a common FAQ. I have a couple of posts on this forum about how to solve the problem..
    – tlfong01
    Feb 14 at 1:21
  • 1
    Unfortunely, I exchanged the sensor with the working one, however I got the same error on same gpio pin.
    – hyorman
    Feb 14 at 1:31
  • 1
    This is terrible code and you have provided no real diagnostics nor explained exactly what you have done. It is difficult to work out what you are trying to do. I defy anyone to decode this - try a simple example. I suggest you try gpiozero.readthedocs.io/en/stable/recipes.html#distance-sensor
    – Milliways
    Feb 14 at 1:36
  • Or compare and contrast with the code in this Q&A: HC-SR04 Ultrasonic Senor Wiring, Operation, and Python Programming Problem raspberrypi.stackexchange.com/questions/96647/…. The demo code there tests only one sensor. Your 3 sensor code makes simple stuff three times more complicated to debug! (Or you might prefer to modify your 3 sensor code to just one sensor, and we debuggers would appreciate that.:)
    – tlfong01
    Feb 14 at 1:40
  • 1
    If you can look at the code, there is only one function which calculates the distance. you can focus on the main function. only repated lines are initialization for each one and 3 lines are not so confusing I guess. However code is not really important for the issue I guess.
    – hyorman
    Feb 14 at 1:45

Your Answer

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

Browse other questions tagged or ask your own question.