Ultrasonic sensor with onion

I recently purchased an ultrasonic sensor and an onion omega 2+. I have connected the two and wrote a program that should give me a distance but the program does not work. I have checked the sensor and it is working. The program was based on a program I created for the raspberry pi which was modified for the onion GPIO. Can someone help me and let me know 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)

Hi @Denver42165,

@Dave is our tech hero for Onion hardware and is back in tomorrow. Hold onto your hat and we should be able to help out more then (unless someone in the community jumps in first).

Hi @Denver42165!

How does it behave? Does it output any value at all?
Do you know if the trigger pulses are being generated?
I’ve run your code here and it does generate an output pulse, the error seems like it might be because the

while GPIO_ECHO.getValue() == 1:
    stop = time.time

Doesn’t get a chance to run, which would mean that ECHO pin is still LOW when it gets to that line.

Can you provide a little more information about exactly how it’s behaving?

It definitely generates a pulse and receives a signal return. It appears as though the CPU is not fast enough to record the return pulse. I agree the problem does lie with the While loop however, The program is the same that was used on a rpi3.

What ultrasonic sensor are you using? Is it 3.3V or 5V?

It is a 5v sensor. I have used resistors to drop down the voltage back to 3.3v.
The sensor is the jsn-sr04t-2.0 ultrasonic sensor.
I have a omega 2+ with the power dock.
I am taking the 5v from the dock.
The power dock is powered by a raspberry Pi official power source(240v adaptor)

Have you come up with a solution yet @Dave. I have exhausted all my ideas.

Hi Denver,

Just jumping in here. After a bit of poking around, it looks like there shouldn’t be any issues using the HCSR-04 with the Onion board apart from the voltage issue. Could you provide the resistor values that you’re using? Do you have a scope handy to verify the waveform voltages?

I found a post on the onion forums about using it and one user even provided his code for getting it up and running: http://community.onion.io/topic/2167/how-to-connect-ultrasonic-sensor-hc-sr04/3

I’d take a swing at following his implementation, using a proper logic level converter if possible, and see how it goes.

Thanks @Sam. I have read through that post before and all of the code is in C+. As i am only a noob and previously only used python, I decided it was not worth trying to learn a new coding language to understand how the program works and modify it for my needs. I will go back to my original design and use a raspberry pi to work with the ultrasonic sensor. I will just have to use the onion for a different project. Thanks everyone at Core Electronics for their help.

1 Like

Hi Denver,

Not a problem, I would suggest that it would be worthwhile taking a look at it as the structure of C+ code isn’t too different from Python once you understand the syntax changes. But perhaps the Pi would be a better fit for this project then.

Hey friend, could you please submit your code used in your raspberry? :c

Checkout his project writeup:

I’d bet it’s the same application :slight_smile: