0

In my current application, I have interfaced 9 US100 sensors to Raspberry Pi's GPIO pins. Pins mapping is as follows:

GPIO_Trig_list =[2, 17, 10, 5, 19, 14, 23, 8, 20]
GPIO_Echo_list =[3, 27, 9, 6, 26, 15, 24, 7, 21]

It means the first sensor's Trig pin is connected to GPIO 2 and the Echo pin is connected to GPIO 3. VCC and GROUND pins of the sensors are connected to the VCC and GROUND of Raspberry PI. Everything is running smoothly, I was able to read distances from all sensors nicely for a few days but suddenly one of the GPIO pins froze and on that GPIO pin I failed to read distance. Later after a few hours or days frozen GPIO pin automatically recovered itself and started responding nicely and I was able to read distance data. This behavior occurs frequently with any GPIO pin. Can somebody help me with this finding a solution?

In this application, I am reading continuous 150 values at a time from the single sensor with an interval of 10ms between readings. once the first sensor has all 150 readings, readings from 2nd sensor will start and will collect all 150 readings same as done for the first. The same will happen for the rest of the sensors. Once all 9 sensors' readings are collected, there is a wait for 6 sec and again cycle will repeat to collect all 9 sensors' data.

Follow the below code to learn more about software details:

import time
import threading
#import config
import RPi.GPIO as GPIO 

#from logger import logger
#if config.USE_ASYNC_LOG:
#    logger = logger.getInstance()

TRIG_PINS = [2, 17, 10, 5, 19, 14, 23, 8, 20]
ECHO_PINS = [3, 27, 9, 6, 26, 15, 24, 7, 21]

module_name = "Distance Sensor"

class DistanceSensor():
    def __init__(self) -> None:
        self.distance = 3.5

        GPIO.setmode(GPIO.BCM)
        self.GPIO_TRIG = 23 # red wire #16
        self.GPIO_ECHO = 24 # Gray wire #18
        GPIO.setup(self.GPIO_TRIG, GPIO.OUT)
        GPIO.setup(self.GPIO_ECHO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

#        logger.info("%s,Started ...",module_name)


    def get_distance(self):
        #print("Reading distance ..................")
        for x in len(TRIG_PINS):
            self.GPIO_TRIG = TRIG_PINS[x]#23 # red wire #16
            self.GPIO_ECHO = ECHO_PINS[x]#24 # red wire #16
            print("x= , TRIG_PINS[x], ECHO_PINS[x]..................",x,TRIG_PINS[x],ECHO_PINS[x])
            GPIO.output(self.GPIO_TRIG, GPIO.LOW) 
            time.sleep(0.0002) 


            GPIO.output(self.GPIO_TRIG, GPIO.HIGH) 
            time.sleep(0.00001) 
            
            GPIO.output(self.GPIO_TRIG, GPIO.LOW)
            
            stime = time.time()
            start_time = time.time()
            bounce_back_time = time.time()

            while GPIO.input(self.GPIO_ECHO)==0: 
                start_time = time.time() 
                
                if start_time - stime > 0.5:
                    GPIO.output(self.GPIO_TRIG, GPIO.LOW)
                    self.distance = None
                    print("******ECHO zero crossd timeout with pin =")
    #                logger.error("%s, Distance sensor (GPIO_ECHO == 0) timeout:%s",module_name,self.distance)
                    return None


            while GPIO.input(self.GPIO_ECHO)==1: 
                bounce_back_time = time.time() 

                if bounce_back_time - start_time > 0.5:
                    GPIO.output(self.GPIO_TRIG, GPIO.LOW)
                    self.distance = None
                    print("******ECHO one crossd timeout with pin =")
    #                logger.error("%s, Distance sensor (GPIO_ECHO == 1) timeout:%s",module_name,self.distance)
                    return None

            pulse_duration = bounce_back_time - start_time 
            #print ("pulse duration", pulse_duration)
            self.distance = round(pulse_duration * 17150, 2) 


            #cm to feet
            if self.distance is not None:
                self.distance *= 0.0328084

            return self.distance
        


if __name__ == "__main__":
    distanceSensor = DistanceSensor()

    while True:
        distance = distanceSensor.get_distance()
        print(distance)
        time.sleep(1)

Here once the GPIO pin starts unresponsive, the observed error is

logger.error("%s, ******ECHO zero crossd timeout with pin = %s ******", module_name, echo)
1
  • I’m voting to close this question because there's zero interest in it.
    – Seamus
    Sep 8 at 22:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.