We have upgraded the community system as part of the upgrade a password reset is required for all users before login in.

SR04T Ultrasonic sensor

  • I am currently using a SR04T to measure a distance. I am using the same program that I have previously used on a raspberry pi, however, it does not work. I have updated the GPIO references to suit pyOnionGpio. The error is occurring in the While loop. Can someone tell me where I am going wrong?

    #This program is to be added to crontab for 15min updates
    import time
    import datetime
    import onionGpio

    Program setup

    speedSound = 34300 #Speed of sound in cm/s at temperature

    Define GPIO for sensors

    GPIO_TRIGGER = onionGpio.OnionGpio(0)
    GPIO_ECHO = onionGpio.OnionGpio(1)

    Set pins as output and input

    GPIO_TRIGGER.setOutputDirection(0) # Trigger
    GPIO_ECHO.setInputDirection() # Echo

    Taking measurment

    GPIO_TRIGGER.setValue(0) # Set trigger to False (Low)
    time.sleep(0.5) # Allow module to settle
    GPIO_TRIGGER.setValue(1) # Send 10us pulse to trigger
    time.sleep(0.0001) # Wait 10us
    GPIO_TRIGGER.setValue(0) # Trigger
    start = time.time()
    while GPIO_ECHO.getValue() == 1:
    stop = time.time()

    Calculate distance

    elapsed = stop-start
    distance = round((elapsed * speedSound)/2,2)
    print (distance)

  • @Denver-Pollock Can anyone from onion please help me out with this?

  • Maybe the pins for GPIO0 and 1 are not set to GPIO mode.
    Have you checked the pin multiplexer with omega2-ctrl gpiomux get?
    This lists the current function for all groups. GPIO0/1 are in the i2s groups, so this group's line should show gpio in sqare brackets. If it does not, use omega2-ctrl gpiomux set i2s gpio to switch those pins to GPIO.

  • @luz thanks for your help. I have checked the pins and they are definitely set to GPIO. I believe the problem is with the while loop in the program. For some reason it is not running any commands in the loop. I thought someone else would have used an ultrasonic sensor and had a python program that worked.

    I am still looking for a solution to the problem if anyone else has a suggestion.

  • @Denver-Pollock maybe GPIO_ECHO.getValue() is not equal 1 so while is never entered?

  • @Douglas-Kryder great suggestion however I have tried checking the true value of the returned signal and it shows a true value. So it should go into the While loop.

  • @Denver-Pollock well, you could always drop a print at various points in the code to verify as well as determine the code has reached that point, be able to tell were things do not execute.

  • @Douglas-Kryder I tried adding print commands through the program and it still showed that it didn't enter While loop even when it should. The program works with a raspberry pi.

  • @Denver-Pollock welll, if that is the case then the condition
    while GPIO_ECHO.getValue() == 1:
    has not been met? one thing, i think the sr04t is a 5v module and unless you have it set up with 5v, separated from the omega2 3.3v it will not work.

  • I have looked at interfacing an HC-SR04 sonar module to the Omega2+. The interface technique used by the Arduinos of toggling Trig and then sampling Echo to look for a high then grabbing the time in mS, then sampling Echo looking for a low, then grabbing time again and subtracting to find the duration of Echo high, does not work on the Omega2. There are multiple reasons why it does not work. Mainly due to the Linux OS not allowing the GPIO pins to react fast enough.

    You are stuck in the while loop because by the time the Omega2 changes Trig to low and starts sampling the Echo input for the low to high transition, it has already happened and you missed it.

    I know this because I tried the same thing and looked at it with a logic analyzer and saw how slow the Omega2 reacted.

  • @James-Behrens Thanks. i pretty much came up with the same solution.

Log in to reply

Looks like your connection to Community was lost, please wait while we try to reconnect.