Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Problem with Lopy4 and MaxBotix Ultrasonic sensor

    Scheduled Pinned Locked Moved LoPy
    4 Posts 2 Posters 91 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jlproyectos
      last edited by

      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/

      1 Reply Last reply Reply Quote 0
      • R Offline
        ric2498
        last edited by robert-hh

        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.

        J 1 Reply Last reply Reply Quote 1
        • J Offline
          jlproyectos @ric2498
          last edited by

          @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

          R 1 Reply Last reply Reply Quote 0
          • R Offline
            ric2498 @jlproyectos
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1

            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
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom