Me-UltraSonic Sensor v2, RaspberryPi and python


#1

Hi!

I just bought Me-UltraSonic Sensor v2. And I woul like to connect it directly with RaspberryPi. Just now I realised that it has only 3 pins. If I understand correctly, then SIG pin is for sending (trigger) and receiving signal. I looked at Makeblock.cpp and TestUltrasonicSensor.ino and if I understand it correctly, then I have to send signal for 10 microsec, and then listen for response and measure time when the signal is on. I used this script http://www.raspberrypi-spy.co.uk/2013/01/ultrasonic-distance-measurement-using-python-part-2/. I connect pin 23 directly to SIG and then also connected SIG through voltage divider to pin 24. But I never get signal.

Any ideas what I’m doing wrong. Any hint.

Thanks.

tmz


#2

hi,@tmz,the ultrasonic sensor use the pulseIn function to measure the distance.

long MeUltrasonicSensor::measure(){   
   long duration;   
   MePort::Dwrite2(LOW);    
   delayMicroseconds(2);    
   MePort::Dwrite2(HIGH);	
   delayMicroseconds(10);  
   MePort::Dwrite2(LOW);  
   pinMode(s2, INPUT);   
  duration = pulseIn(s2, HIGH);   
  return duration;
}

https://github.com/Makeblock-official/Makeblock-Library/blob/master/makeblock/Makeblock.cpp


#3

I saw long MeUltrasonicSensor::measure(){ ...

But for pulseIn function I only found
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
and
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

in
https://roboticsclub.org/redmine/projects/quadrotor/repository/revisions/58d82c77908eee0e1c222f7b38691e6532deb77b/entry/arduino-1.0/hardware/arduino/cores/arduino/Arduino.h

and I didn’t know how to use these. But I thought it just measures the signal length (how long it is high = on). So my code in python which is presumably doing the same:

GPIO.setup(GPIO_PORT,GPIO.IN)  # Echo

  start = time.time()

  while GPIO.input(GPIO_PORT)==0:
    start = time.time()

  while GPIO.input(GPIO_PORT)==1:
    stop = time.time()

  elapsed = stop-start
......

But I never get the high signal.

Where I can get the code for pulseIn function?

And does the duration of 10 microsec of the trigger pulse has to be exactly 10 microsec - I mean the delay between high and low?

   MePort::Dwrite2(HIGH);	
   **delayMicroseconds(10);**  
   MePort::Dwrite2(LOW);

Thanks.

tmz


#4

SIG pin is for sending (trigger) and receiving signal,so you can do it as follows:

GPIO.output(GPIO_SIG, True)
time.sleep(0.00001)
GPIO.output(GPIO_SIG, False)
start = time.time()

while GPIO.input(GPIO_SIG)==0:
start = time.time()

while GPIO.input(GPIO_SIG)==1:
stop = time.time()

elapsed = stop-start
distance = (elapsed * 34300)/2

return distance


#5

Hmm. I also tried this, but didn’t get anything. So I tried with two pins. If this should work, then something else is wrong. But no ideas what. Perhaps I must start from the beginning.

How many LED are on the sensor? I see only red when the +5V is applied. Is there another LED on the opposite side? When it should be on?

Thanks.

tmz


#6

Does anybody knows, what is the voltage of the return signal? Is it 5V or less? A what is the delay between trigger signal and return signal?

tmz


#7

hi,@tmz
This is my colleague‘s test code for driving the me ultrasonic sensor.
python’s code:

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

while True:
        raw_input("press some key to trigger ultrasonic sensor:")
        GPIO.setup(25, GPIO.OUT)
        GPIO.output(25,False)
        time.sleep(0.0001)
        GPIO.output(25,True)
        time.sleep(0.0001)
        GPIO.output(25,False)
        cnt = 0
        GPIO.setup(25, GPIO.IN,pull_up_down=GPIO.PUD_UP)
            while GPIO.input(25)==0:
                    time0 = time.time()
            while GPIO.input(25)==1:
                    cnt=cnt+1
            time1 = time.time()
            print (time1-time0)
            print cnt
        GPIO.cleanup() 

PS:Connect ultrasonic sensor sig pin to raspberry pi pin 25


#8

Thanks. I will try this out. Does he connects raspberry and sensor directly or trough resistor(s)?


#9

@tmz, the ultrasonic senser use the raspberry’s 3.3v power,and the pin directly connect .


#10

Ha. I thought if there is a sign +5V it must be +5V. And won’t work on 3.3V.
It’s working. Thanks.


#11