Seeed grove ultrasonic ranger
This sensor measures the distance to the nearest object by measuring the time an ultrasound pulse takes to reflect from the object.
http://www.seeedstudio.com/wiki/Grove_-_Ultrasonic_Ranger is Seeed's docs for the module.
Hardware¶
The range finder has a 4 pin connector, 3 of which are used. It has GND, Vcc and Signal lines. These are labelled on the board. The docs say the sensor is designed for 5V, but it works fine with the mbed's 3.3V. The signal line can be connected to any of the mbed's digital in/out pins.
To trigger a measurement a short pulse (around 10 us) is sent on the signal line. The ranger then send out some pulses and time when they return. The distance is then returned as a pulse of varying length such that
distance [m] = pulse width [us] / 5800 [us / m]
The two images below show the pulses being sent and received for a couple of different distances.
Software¶
I've written some code that runs the ultrasound ranger. It uses a /handbook/DigitalInOut to send a pulse and then to measure the width of the returning pulse using a /handbook/Timer. The code allows you to set a cut off time so that, should you lose connection to your range finder, it will return a negative distance and not hang up your program. I've tried this out and it is quite happy for me to disconnect the sensor and it will just return a distance of -1.0 m each loop until I reconnect it and it will resume giving me measured distances.
My code uses a library I wrote to read and write digital pulses:
Import libraryPulse
Output digital pulses of specified length and read the length of input pulses.
and also a library for range finders:
Import libraryRangeFinder
Class to handle triggering and reading the distance measured by a range finder.
This means that you can use a range finder with a very simple piece of code such as:
Import program
00001 /* Copyright (c) 2012 Nick Ryder, University of Oxford 00002 * nick.ryder@physics.ox.ac.uk 00003 * 00004 * MIT License 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00007 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00008 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00009 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in all copies or 00013 * substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00016 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00017 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00018 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00020 */ 00021 00022 #include "mbed.h" 00023 00024 #include "RangeFinder.h" 00025 00026 // Seeed ultrasound range finder 00027 RangeFinder rf(p21, 10, 5800.0, 100000); 00028 DigitalOut led(LED1); 00029 00030 int main() { 00031 led = 1; 00032 float d; 00033 while (1) { 00034 d = rf.read_m(); 00035 if (d == -1.0) { 00036 printf("Timeout Error.\n"); 00037 } else if (d > 5.0) { 00038 // Seeed's sensor has a maximum range of 4m, it returns 00039 // something like 7m if the ultrasound pulse isn't reflected. 00040 printf("No object within detection range.\n"); 00041 } else { 00042 printf("Distance = %f m.\n", d); 00043 } 00044 wait(0.5); 00045 led = !led; 00046 } 00047 }