Problem with Lopy4 and MaxBotix Ultrasonic sensor



  • Hi

    I have a problem and have been trying to solve it for 1 week. I have a MaxBotix MB7052 ultrasound sensor and I want to get the readings with a Pycom Lopy4.

    https://www.maxbotix.com/documents/XL-MaxSonar-WR_Datasheet.pdf

    I have used it before with an ESP8266 and ESP32 development board, but now I want to use Lopy4.
    I use the following code in the ESP:

    data = pulseIn(pwPin1, HIGH);
    cm = data/58;

    Where pwPin1 is:
    const int pwPin1 = 2;

    With Lopy 4 I have tried with Analogic, Pulse-Width and UART, but none of the ways I can get the right information

    Is there a function equivalent to Arduino's PulseIn? What could I be doing wrong?

    Try with:
    pulses_get https://docs.pycom.io/firmwareapi/pycom/pycom/
    https://forum.pycom.io/topic/5737/maxbotix-ultrasonic-sensor-gpy/2
    https://docs.pycom.io/firmwareapi/pycom/machine/uart/



  • @jlproyectos

    Hi,

    It is the PING trigger to establish the duration of the ping for the Sensor that I used (Ultrasonic Distance Sensor - HC-SR04 (4 pins)), to convert the time into a distance.



  • @ric2498 said in Problem with Lopy4 and MaxBotix Ultrasonic sensor:

    Hi,

    With this code found online and adapted for my ultrasonic range finder sensor I can read the distance on my sensor .... If it can help.... for sure you will have to modify it according to the PINs used by the MaxSonar... you should be able to read the data on the pin 2 provided by Maxsonar.

    Pin 2-This pin outputs a pulse-width representation of range. To calculate the distance, use a scale factor of 58uS per cm. (MB7051, MB7052, MB7053, MB7060, MB7062, MB7066, MB7067, MB7068)This pin outputs the analog voltage envelope of the acoustic waveform. For the MB7070 series and MB7092 sensors, this is a real-time always-active output (MB7070, MB7072, MB7076, MB7077, MB7078, MB7092)

    Here's the code extracted from my project (it may need mods...)

    import pycom
    import time, utime
    from machine import Pin
    from machine import Timer
    import binascii
    
    # create an output pin on pin #12
    p12 = Pin('P12', Pin.OUT)
    chrono = Timer.Chrono()
    
    #def ReadDistance(pinRM):
    def ReadDistance():
        p12.mode(p12.OUT)
        p12.value(0) #low
        utime.sleep_ms(2)
        p12.value(1) #high
        utime.sleep_ms(5)
        p12.value(0) #low
        p12.mode(p12.IN)
    
        duration = 0
        chrono.reset()
    
        while p12() == 0:
            pass
    
        chrono.start()
    
        while p12() == 1:
            pass
    
        chrono.stop()
    
        duration = chrono.read_us()
        print("Duration = ",duration)
    
        return duration
    
    while True:
        distancerm = 0
        distancerm = ReadDistance()
        pouces = distancerm/74/2
        print('%3.1f = formatted rounded float\n' % round(pouces, 1))
        pouces = int(pouces)
        cm = distancerm/29/2
        cm = int(cm)
        print("Distance de l'objet est = ",cm," cm ou ",pouces," pouces")
        time.sleep(5)
    

    Good luck.

    Richard.

    Thanks Richard for the response. Its code works correctly, although sometimes there is an error in the measurements.

    at distances of 114 cm it sometimes indicates 120, 140 or even 201!

    I will try to adjust it so that the values are correct. However, I have questions about your code

    Why wait 2 and 5ms? And what do the functions mean?
    p12.value(0) #low, #hight and #low again



  • Hi,

    With this code found online and adapted for my ultrasonic range finder sensor I can read the distance on my sensor .... If it can help.... for sure you will have to modify it according to the PINs used by the MaxSonar... you should be able to read the data on the pin 2 provided by Maxsonar.

    Pin 2-This pin outputs a pulse-width representation of range. To calculate the distance, use a scale factor of 58uS per cm. (MB7051, MB7052, MB7053, MB7060, MB7062, MB7066, MB7067, MB7068)This pin outputs the analog voltage envelope of the acoustic waveform. For the MB7070 series and MB7092 sensors, this is a real-time always-active output (MB7070, MB7072, MB7076, MB7077, MB7078, MB7092)

    Here's the code extracted from my project (it may need mods...)

    import pycom
    import time, utime
    from machine import Pin
    from machine import Timer
    import binascii
    
    # create an output pin on pin #12
    p12 = Pin('P12', Pin.OUT)
    chrono = Timer.Chrono()
    
    #def ReadDistance(pinRM):
    def ReadDistance():
        p12.mode(p12.OUT)
        p12.value(0) #low
        utime.sleep_ms(2)
        p12.value(1) #high
        utime.sleep_ms(5)
        p12.value(0) #low
        p12.mode(p12.IN)
    
        duration = 0
        chrono.reset()
    
        while p12() == 0:
            pass
    
        chrono.start()
    
        while p12() == 1:
            pass
    
        chrono.stop()
    
        duration = chrono.read_us()
        print("Duration = ",duration)
    
        return duration
    
    while True:
        distancerm = 0
        distancerm = ReadDistance()
        pouces = distancerm/74/2
        print('%3.1f = formatted rounded float\n' % round(pouces, 1))
        pouces = int(pouces)
        cm = distancerm/29/2
        cm = int(cm)
        print("Distance de l'objet est = ",cm," cm ou ",pouces," pouces")
        time.sleep(5)
    

    Good luck.

    Richard.


Log in to reply
 

Pycom on Twitter