Lopy with a hc-sr04 endless loop



  • ****0_1526567392015_WhatsApp Image 2018-05-17 at 16.28.54.jpeg

    Hi everybody,

    like you see in the picture, i made it like that and i wrote this programm, i have a sensor hc-sr04 and Lopy with an expansionboard 2.0.
    The source:

    from machine import Pin
    echo = Pin('P19', mode=Pin.IN) #on the expansionboard G6
    trig = Pin('P20', mode=Pin.OUT) #on the expansionboard G7
    while True:
        trig.value(0)
        time.sleep(2)
        trig.value(1)
        time.sleep(0.00001)
        trig.value(0)    
        while echo()== 0:
            pulse_start = time.time()
            print("echoValue=0")
        while echo()== 1:
            pulse_end = time.time()
            print("echoValue=1")
        pulse_duration = pulse_end - pulse_start
        distance = pulse_duration * 17150
        distance = round(distance, 2)
        print ("hello am i here?")
        if distance > 2 and distance < 400:
            print ("Entfernung: ",distance - 0.5,"cm")
        else:
            print ("Zu weit")
        print ("ENDE")
    

    Hardware: I give small V to echo of course, and VCC goes to VIN on the expansionboard. GND to GND.
    And with this Code i am coming everytime in a endless loop for echo = 0. I dont understand why. I read already other post about it but i think it should be one year later better :))

    Thanks a lot for your help.
    Ciao.****



  • @rootmi I’m no electronics expert and I don’t know the full specs of the sensor, but I’m not sure I understand how those two resistors would be enough. You are still powering a 5V device with 3.3V and sending it a 3.3V signal, aren’t you?



  • @jcaron Thanks for your answer.
    I think it should be enough that i use 2 resistance for the 3.3V. You can see them in the picture :)



  • @mdalmis the HC SR04 has an operating voltage of 5V while the Pycom devices use 3.3V. Not sure what the input tolerance and actual output are, you may want to check...



Pycom on Twitter