Hi everyone. This post is going to be a little different than some of the others. In fact it is different than what I had originally intended to blog about. I was going to do a post using temp sensors, but I see that several others have chosen that topic, so I thought I would write a couple of posts about simple projects that allow the Raspberry Pi to measure some other things around them.
I looked through my toolkit and discovered I had one of the sensors that I had purchased several years ago when i first got interested in the Arduino world. I thought it would make an interesting project for the Raspberry Pi B+, although this project would work just as well on the standard B, and probably the Model A as well.
The sensor I discovered was the Ping))) Ultrasonic Sensor manufactured by Parallax. According to the documentation:
"The Parallax PING))) ultrasonic distance sensor provides precise, non-contact distance measurements
from about 2 cm (0.8 inches) to 3 meters (3.3 yards). It is very easy to connect to microcontrollers such
as the BASIC Stamp®, SX or Propeller chip, requiring only one I/O pin.
The PING))) sensor works by transmitting an ultrasonic (well above human hearing range) burst and
providing an output pulse that corresponds to the time required for the burst echo to return to the
sensor. By measuring the echo pulse width, the distance to target can easily be calculated. "
The Ping (as I will refer to it from this point forward) is a very simple device. It requires only 3 wires to connect it to the Pi. One is +5V which can be grabbed directly from pin 2 of the Raspberry Pi, one is Ground, which is provided on pin 4, and the third is pin 11, one of the available GPIO pins. A Fritzing diagram is provided for reference.
That's it!!! That is all that is required to connect this circuit!!!
According to the Ping documentation, it is perfectly safe to connect to a 3.3v device such as the Raspberry Pi. This is something very important to check because providing an input over 3.3v to any of the pins on the Raspberry Pi can cause damage to the device. Also, all electrical connections should be made when the Raspberry Pi is off and there is no power applied. Even a circuit as simple as this can be miswired so double check all connections.
OK, now we have the device connected. What does it do? How do I use it? I'll answer those questions below:
What does it do?
As described the Ping is an Ultrasonic Distance measuring device. The theory is that when the device is signaled to start, an audio signal (above the range of human hearing) is emitted from one of the "speakers" on the device, that signal is expected to bounce off of a solid object and return to the Ping as a reflected sound wave, a pulse that corresponds to the return time of the reflected signal is sent back to the signalling device. Dividing that value by 2 (because it had to travel to the object and back, you only need 1/2 of the distance) and then multiplying that object by the speed of sound in centimeters (34000cm/s) will give you the approximate distance from the Ping sensor to the object. You can demonstrate this to yourself while running the example code and moving the sensor around or placing your hand at different distances from the sensor.
How do I use it?
I've included some sample code that sends the start signal, captures the return pulse, measures the duration of the pulse, then prints out the distance in both cm and inches:
#!/usr/bin/python import time import RPi.GPIO as GPIO # Use board based pin numbering GPIO.setmode(GPIO.BOARD) def ReadDistance(pin): GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, 0) time.sleep(0.000002) #send trigger signal GPIO.output(pin, 1) time.sleep(0.000005) GPIO.output(pin, 0) GPIO.setup(pin, GPIO.IN) while GPIO.input(pin)==0: starttime=time.time() while GPIO.input(pin)==1: endtime=time.time() duration=endtime-starttime # Distance is defined as time/2 (there and back) * speed of sound 34000 cm/s distance=duration*34000/2 return distance while True: distance = ReadDistance(11) print "Distance to object is ",distance," cm or ",distance*.3937, " inches" time.sleep(.5)
I've included the above code as an attachment that you can download and run locally. You will need to ensure that the proper python libraries have been installed.
You just need to run the following commands at the command line
sudo apt-get update sudo apt-get install python-dev sudo apt-get install python-rpi.gpio
If you are not sure whether those things are installed just run them, it will either download the latest version or tell you that you have the latest version already installed. It may prompt you to answer 'Y' to confirm the installations.
Once the board is wired up, and you have downloaded the attached loo-ping.py, you can open a command shell, go to the directory where you saved loo-ping.py and type:
chmod 755 loo-ping.py
that will change the permissions on the file to executable. (only necessary right after downloading)
and then type
That will execute the program. It is necessary to type sudo to execute the program as the super user so you will have access to the gpio pins, and the ./ simply indicates that the file is in the current path.
When it executes, you can expect the following output:
You can stop execution of the program by typing ctrl-c (the ctrl key and the c key at the same time), this will send an interrupt to the process and tell it to stop executing. Wave your hand in front of the sensor, pick it up and point it at the monitor, watch the values change.
Why would I use it?
Why would anyone use an Ultrasonic Distance Sensor? Well, they are commonly used in robotics as the "eyes" of the robot. When it detects it is getting too close to an obstacle, the robot can be commanded to change direction to avoid a collision. But
there are many potential uses. Googling around will probably provide several examples of applications that you never would even consider.
Hopefully I've given a simple example of how to use such a device, the code should be simple enough to pull out and use in your own designs. If this is your first exposure to such a measurement tool I hope that I have explained it well enough for you to think up some interesting uses.
One final picture, the sensor in action:
loo-ping.py.zip 504 bytes