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)
    


Pycom on Twitter