I am trying to connect the following range sensor, http://www.adafruit.com/products/172, to my RPi to get the range values (works great on Arduino Uno). Data sheet is here: http://www.maxbotix.com/documents/MB1010_Datasheet.pdf. I have the +5V, GND, and PWM connected directly to the GPIO pins on the RPi. I'd like to be able to write a python script using RPi.GPIO to report the values from the range sensor but I'm not really sure where to go with it from here. I have successfully used pi-blaster (https://github.com/sarfata/pi-blaster/) to handle the software PWM with a 12V analog LED strip but pi-blaster says it turns all GPIO pins to outputs.
Is it possible to accomplish this without buying some kind of expansion board? (i.e. gertboard). Ultimately I am planning to have the range sensor trigger the LEDs when specific ranges are breached; have this working great on the Arduino, just would like to switch platforms.
Appreciate any help!
-
- Posts: 5
- Joined: Wed Mar 06, 2013 9:09 pm
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I'd feed it from the 3.3V pin and configure it to serial mode. I'm assuming Python has no way of accurately timing the echo pulse.
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Although serial maybe the best way to go with this sensor, it is possible to get good results with Python using PWM as I found from this link.
http://www.raspberrypi-spy.co.uk/2013/0 ... on-part-2/
This actually used a different type of ultrasonic sensor that only has a PWM mode but I can't see why the same or similar code wouldn't work here.
I used this code with the very cheap sensors from eBay and get accurate results (3mm) using that code. For improved reliability rather than just take the average of several readings the code can be easily modified to discard the occasional rogue reading if python/linux decides to take a breather during a measurement cycle.
Looking at how Arduino uses these sensors it may be possible to use the same pin for both trigger and PWM to use just one GPIO pin which is what I am trying next.
http://www.raspberrypi-spy.co.uk/2013/0 ... on-part-2/
This actually used a different type of ultrasonic sensor that only has a PWM mode but I can't see why the same or similar code wouldn't work here.
I used this code with the very cheap sensors from eBay and get accurate results (3mm) using that code. For improved reliability rather than just take the average of several readings the code can be easily modified to discard the occasional rogue reading if python/linux decides to take a breather during a measurement cycle.
Looking at how Arduino uses these sensors it may be possible to use the same pin for both trigger and PWM to use just one GPIO pin which is what I am trying next.
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I stand corrected. Those Python results look quite reasonable.btidey wrote:Although serial maybe the best way to go with this sensor, it is possible to get good results with Python using PWM as I found from this link.
http://www.raspberrypi-spy.co.uk/2013/0 ... on-part-2/
This actually used a different type of ultrasonic sensor that only has a PWM mode but I can't see why the same or similar code wouldn't work here.
I used this code with the very cheap sensors from eBay and get accurate results (3mm) using that code. For improved reliability rather than just take the average of several readings the code can be easily modified to discard the occasional rogue reading if python/linux decides to take a breather during a measurement cycle.
Looking at how Arduino uses these sensors it may be possible to use the same pin for both trigger and PWM to use just one GPIO pin which is what I am trying next.
-
- Posts: 5
- Joined: Wed Mar 06, 2013 9:09 pm
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Thanks btidey! Hadn't stumbled upon that one yet. I was instead trying to go off of a PIR sensor tutorial that he had on that same site. I plan on trying both the serial and PWM approaches (yes using the same PWM pin from the sensor as trigger and echo which I think will work since I was getting 1's and 0's as readings. Just have to do the distance calculation as specified here.
Thanks again everyone for your help and info. Will report results back once done
Thanks again everyone for your help and info. Will report results back once done
-
- Posts: 5
- Joined: Wed Mar 06, 2013 9:09 pm
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I was able to get good readings using this sensor with the suggestion you posted btidey. Almost identical to the example there except just using the PWM pin. Since I am also planning to use pi-blaster which configures all GPIO pins as outputs by default to run LED strips; that will conflict with setting up the GPIO for this sensor as an input.
I'd like to also give this a shot setting this up using serial but not really sure where to start there. Any suggestions?
Thanks for your help
I'd like to also give this a shot setting this up using serial but not really sure where to start there. Any suggestions?
Thanks for your help
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I'm not sure but I expect that pi-blaster only configures the 8 normally used gpios as outputs. So perhaps you could use one or more of sda, scl, mosi, miso, sclk, ce0, ce1 for echo/pulse pins.ArbitraryY wrote:I was able to get good readings using this sensor with the suggestion you posted btidey. Almost identical to the example there except just using the PWM pin. Since I am also planning to use pi-blaster which configures all GPIO pins as outputs by default to run LED strips; that will conflict with setting up the GPIO for this sensor as an input.
I'd like to also give this a shot setting this up using serial but not really sure where to start there. Any suggestions?
Thanks for your help
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I successfully tried out a single pin version driving the trigger and echo from one GPIO pin.
I used a simple resistor network to do a bit of level translation and protect the GPIO pins. The values were chosen to give decent levels based on the trigger input having about a 10K pull to +5V.
Attachment shows the connections.
Code was modified to use 1 pin
I used a simple resistor network to do a bit of level translation and protect the GPIO pins. The values were chosen to give decent levels based on the trigger input having about a 10K pull to +5V.
Attachment shows the connections.
Code was modified to use 1 pin
Code: Select all
#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# ultrasonic_3.py
# Measure distance using an ultrasonic module
# in a loop.
#
# Author : Matt Hawkins
# Date : 28/01/2013
# Modded : Bob Tidey
# Date : 13/03/2013
# Uses a common GPIO pin for trigger and echo
# A simple resistor network is used to tie these together
# -----------------------
# Import required Python libraries
# -----------------------
import time
import RPi.GPIO as GPIO
# -----------------------
# Define some functions
# -----------------------
def measure():
# This function measures a distance
# Pulse the trigger/echo line to initiate a measurement
GPIO.output(GPIO_TRIGECHO, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGECHO, False)
#ensure start time is set in case of very quick return
start = time.time()
# set line to input to check for start of echo response
GPIO.setup(GPIO_TRIGECHO, GPIO.IN)
while GPIO.input(GPIO_TRIGECHO)==0:
start = time.time()
# Wait for end of echo response
while GPIO.input(GPIO_TRIGECHO)==1:
stop = time.time()
GPIO.setup(GPIO_TRIGECHO, GPIO.OUT)
GPIO.output(GPIO_TRIGECHO, False)
elapsed = stop-start
distance = (elapsed * 34300)/2.0
return distance
def measure_average():
# This function takes 3 measurements and
# returns the average.
distance1=measure()
time.sleep(0.1)
distance2=measure()
time.sleep(0.1)
distance3=measure()
distance = distance1 + distance2 + distance3
distance = distance / 3
print "raw %.1f" % distance1," %.1f" % distance2," %.1f" % distance3,
return distance
# -----------------------
# Main Script
# -----------------------
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_TRIGECHO = 23
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGECHO,GPIO.OUT) # Initial state as output
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGECHO, False)
# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
try:
while True:
distance = measure_average()
print " Distance : %.1f cm" % distance
time.sleep(1)
except KeyboardInterrupt:
# User pressed CTRL-C
# Reset GPIO settings
GPIO.cleanup()
- Attachments
-
- ultrasonic1pin.jpg (29.56 KiB) Viewed 17650 times
-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Brilliant - never occured to try using 1 pin and switch it from output to input 
This could be 1st thing going into V2 of my Scratch GPIO handler
JFI I use a different approach to averaging at the moment - I take 3 values like you but I sort the 3 values in an array and pick the middle one. My thinking is that 1 stray low/high reading will get ignored.
Simon

This could be 1st thing going into V2 of my Scratch GPIO handler

JFI I use a different approach to averaging at the moment - I take 3 values like you but I sort the 3 values in an array and pick the middle one. My thinking is that 1 stray low/high reading will get ignored.
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Treating the values in a more sophisticated way to weed out any rogues was also something I wanted to play with.
The average of 3 was just borrowed from the original code and I added printing the 3 raw results so I could see what the consistency was like. In general I found it pretty good but you very occasionally get a rogue value.
I quite like the idea of a bit of averaging to potentially improve accuracy so I was thinking along the lines of taking measurements until 3 were close and then averaging those. Most of the time that will still mean 3 raw measurements with the occasional extra thrown in.
The average of 3 was just borrowed from the original code and I added printing the 3 raw results so I could see what the consistency was like. In general I found it pretty good but you very occasionally get a rogue value.
I quite like the idea of a bit of averaging to potentially improve accuracy so I was thinking along the lines of taking measurements until 3 were close and then averaging those. Most of the time that will still mean 3 raw measurements with the occasional extra thrown in.
-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Its just a matter of how long you want to process results for before returning an answer to the user/calling function
We need someone with experience of real-time statisical methods to give us their knowledge wisdom
Simon
We need someone with experience of real-time statisical methods to give us their knowledge wisdom

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Here is a simplistic approach which is certainly not optimum in keeping overall measurement time low but works quite well as the raw readings are fairly consistent anyway.
The average function takes MEASURE_COUNT readings and then checks for consistency by making sure that all measurements lie within a CHECK distance error of the central value. If any fall outside this the complete measurement cycle is repeated up to MAX_RETRIES. The average is taken of a set of consistent readings. If all retries fail then the middle reading is used.
For most cases with a reasonable defined target the retries should be fairly rare.
The average function takes MEASURE_COUNT readings and then checks for consistency by making sure that all measurements lie within a CHECK distance error of the central value. If any fall outside this the complete measurement cycle is repeated up to MAX_RETRIES. The average is taken of a set of consistent readings. If all retries fail then the middle reading is used.
For most cases with a reasonable defined target the retries should be fairly rare.
Code: Select all
#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# ultrasonic_4.py
# Measure distance using an ultrasonic module
# in a loop.
#
# Author : Matt Hawkins
# Date : 28/01/2013
# Modded : Bob Tidey
# Date : 13/03/2013
# Uses a common GPIO pin for trigger and echo
# A simple resistor network is used to tie these together
#Averaging now checks for consistency and eliminates rogue values
# -----------------------
# Import required Python libraries
# -----------------------
import time
import RPi.GPIO as GPIO
# -----------------------
# Define some functions
# -----------------------
def measure():
# This function measures a distance
# Pulse the trigger/echo line to initiate a measurement
GPIO.output(GPIO_TRIGECHO, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGECHO, False)
#ensure start time is set in case of very quick return
start = time.time()
# set line to input to check for start of echo response
GPIO.setup(GPIO_TRIGECHO, GPIO.IN)
while GPIO.input(GPIO_TRIGECHO)==0:
start = time.time()
# Wait for end of echo response
while GPIO.input(GPIO_TRIGECHO)==1:
stop = time.time()
GPIO.setup(GPIO_TRIGECHO, GPIO.OUT)
GPIO.output(GPIO_TRIGECHO, False)
elapsed = stop-start
distance = (elapsed * 34300)/2.0
time.sleep(0.1)
return distance
def measure_average():
# This function takes n measurements ignoring any rogue values
# returns the average.
MAX_TRIES = 5 # Attempts to get consistent results
MEASURE_COUNT = 3 # Nof raw measures in each attempt
CHECK = 2.0 # tolerance in cm between measurements
midpoint = MEASURE_COUNT / 2
for tries in range(MAX_TRIES):
distances = []
for i in range(MEASURE_COUNT):
distances.append(measure())
distances.sort()
measureOK = True
for i in range(MEASURE_COUNT - 1):
if abs(distances[i] - distances[midpoint]) > CHECK:
measureOK = False
break
if measureOK:
break
print "Inconsistent results. ", distances, " Retrying.."
if measureOK:
distance = sum(distances) / len(distances)
else:
print "Inconsistent after retries. Best guess value"
distance = distances[len(distances)/2]
return distance
# -----------------------
# Main Script
# -----------------------
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_TRIGECHO = 23
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGECHO,GPIO.OUT) # Initial state as output
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGECHO, False)
# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
try:
while True:
distance = measure_average()
print " Distance : %.1f cm" % distance
time.sleep(1)
except KeyboardInterrupt:
# User pressed CTRL-C
# Reset GPIO settings
GPIO.cleanup()
-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
I use mine on front of robots crawling across classroom floors so

Simon
not going to be achievablea reasonable defined target

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
nice work btidey! For mproved performance and lower cpu usage you could use the library RPIO ( https://github.com/metachris/RPIO ), which features interrupts on input gpios with very little cpu usage and fast response.
Also, I just received three ultrasonic sensors, and I was wondering how to connect them in series using only 1 5V pin, 1 GND pin and 3 GPIO pins...could you give me some help about the needed resistors and setup? I'm afraid of damaging my raspberry pi with my limited electronics knowledge. I've plentiful of resistors 1K and over.
Thanks in advance!
Also, I just received three ultrasonic sensors, and I was wondering how to connect them in series using only 1 5V pin, 1 GND pin and 3 GPIO pins...could you give me some help about the needed resistors and setup? I'm afraid of damaging my raspberry pi with my limited electronics knowledge. I've plentiful of resistors 1K and over.
Thanks in advance!
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Yes, The software side could certainly be improved. I was initially trying out using python as a quick way of getting going.
For 3 sensors and 3 GPIO pins you can just connect each one as shown. So all 3 connect to +5V and GND in common. Each GPIO then goes through its own 1K resistor to the trigger pin of its corresponding sensor. The Echo of that sensor then has a 2K2 resistor to the GPIO pin and a 2K2 resistor from that point to GND.
When the trigger for a sensor is required then that pin is set to output high then low. After that is set to input and can monitor the Echo. The 2K2 / 2K2 divider drops the 5V pulse out from the sensor to 2.5V so there is no danger to the GPIO. In addition the resistors protect the pin from any inadvertent excess current.
For 3 sensors and 3 GPIO pins you can just connect each one as shown. So all 3 connect to +5V and GND in common. Each GPIO then goes through its own 1K resistor to the trigger pin of its corresponding sensor. The Echo of that sensor then has a 2K2 resistor to the GPIO pin and a 2K2 resistor from that point to GND.
When the trigger for a sensor is required then that pin is set to output high then low. After that is set to input and can monitor the Echo. The 2K2 / 2K2 divider drops the 5V pulse out from the sensor to 2.5V so there is no danger to the GPIO. In addition the resistors protect the pin from any inadvertent excess current.
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Thanks! I'll start experimenting right away!
EDIT:
Here it is, working like a charm! Next step: accelerometer+gyro+magnetometer

Thanks again
EDIT:
Here it is, working like a charm! Next step: accelerometer+gyro+magnetometer

Thanks again

-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Got it working with my Scratch GPIO setup 
Just having to use one pin makes life so much easier
I used to have to basically dedicate 2 pins to the job (which were then not available to other projects that didn't use it )
Now, I can just pick a pin (say 23) and do a broadcast sonar23 and just read the sonar23 sensor value returned
Thanks very much
Simon

Just having to use one pin makes life so much easier

I used to have to basically dedicate 2 pins to the job (which were then not available to other projects that didn't use it )
Now, I can just pick a pin (say 23) and do a broadcast sonar23 and just read the sonar23 sensor value returned

Thanks very much

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Hi,
I'm not sure if it helps you, but I drive my sonar by an atmel microcontroller. I connected it via serial so I only have to send a "start" command and get the pulse length of the sonar back. So I can equal the range with the pi. You can also program it to send the value of the sonar constantly to the pi.
I think it is a little easier like trying to read the value directly via the pi.
Mfg
His
I'm not sure if it helps you, but I drive my sonar by an atmel microcontroller. I connected it via serial so I only have to send a "start" command and get the pulse length of the sonar back. So I can equal the range with the pi. You can also program it to send the value of the sonar constantly to the pi.
I think it is a little easier like trying to read the value directly via the pi.
Mfg
His
http://technikegge.blogspot.de
-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
But increases the cost and complexity of the hardware neededI think it is a little easier like trying to read the value directly via the pi.

1x $5 sensor and 3 resistors is a win in my book

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
OK. This is a reason.
But an Atmel cost less then 1€ and with peripheries is less then 1,5€.
But when your goal is to make it cheap as possible, then it is to much. So I haven't understand that it have to be the minimalistic way. Sorry for that.
And sorry for my english
Mfg
His
But an Atmel cost less then 1€ and with peripheries is less then 1,5€.
But when your goal is to make it cheap as possible, then it is to much. So I haven't understand that it have to be the minimalistic way. Sorry for that.
And sorry for my english
Mfg
His
http://technikegge.blogspot.de
-
- Posts: 2327
- Joined: Fri Feb 24, 2012 6:19 pm
- Location: Euxton, Lancashire, UK
- Contact: Website
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
Your English is good 
Simon

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
http://cymplecy.wordpress.com/ @cymplecy on twitter
-
- Posts: 2
- Joined: Sat Nov 30, 2013 10:23 am
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
using MB1000 with Rpi model B ..... i got good results if any still have any issue contact me.... i ll surly help you
akifnaeem21@yahoo.com
akifnaeem21@yahoo.com
-
- Posts: 9
- Joined: Fri Nov 24, 2017 8:28 am
Re: Maxbotix Ultrasonic Rangefinder - LV-EZ1
can i know the circuit diagram connecting the three ultrasonic sensors and the programming code to take measurement ...
im still newbie into this raspberry pi
im still newbie into this raspberry pi