I2C communication between sensor and Pysense not working
-
Hello all together,
I posted my issue under another topic three weeks ago but it could not be solved and I still got no idea why my setup fails to work. I have a hc-sr04 ultrasonic sensor (5V signal level) level shifted from 5v to 3v3 and connected to the Pysense external I/O header. Echo goes to PIN7, Trigger to Pin8 and Ground to Pin 1. Very sporadically I receive a distance measurement that sometimes seems to be correct and sometimes is 0; 99% of the time, the serial port seems to be dead! Do you have an idea what could be the issue? Is there a mistake in the code? I have two Pysense and the problem with them both. Thank you very much for your time in advance!
import pycom import time from machine import Pin, Timer from machine import I2C import ubinascii import micropython echo = Pin(Pin.exp_board.G17, mode=Pin.IN) trigger = Pin(Pin.exp_board.G22, mode=Pin.OUT) trigger(0) chrono = Timer.Chrono() while True: chrono.reset() trigger(1) time.sleep_us(10) trigger(0) while echo() == 0: pass chrono.start() while echo() == 1: pass chrono.stop() distance = chrono.read_us() / 58.0 if distance > 400: print("Out of range") else: print("Distance {:.0f} cm".format(distance)) time.sleep(500)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login