SRF05 Ultrasonic

Feb 15, 2011

The Devantech SRF05 Ultrasonic Range Finder is one of my two favorite sensors. The other is the Sharp IR ranger but I’m not talking about IR this time. I am so all about the SRF05. This time anyway.

It’s a pretty cool device and it looks like a pair of eyes –which is why so many people use them. Because they look like eyes. Actually, people use them because they work really well and are pretty robust. They work on 5v DC so they’re easy to power with Vcc from an Arduino. The photo there is an SRF05 fully connected to an Arduino Pro. It requires four wires, one for power, one for ground, and one for an initialization pin and the last is the actual sensor pin, the echo pin.

To make it work is mildly tricky; you have to set the init pin high for 10 milliseconds and then shut it off and then watch the echo pin to go high and then low again. The amount of time the echo pin remains high is measured and that time can mathematically calculate the distance in centimeters or inches, depending on the value you divide the time by.  Below is some test code I use as the basis for my Arduino sketches. Once you have the distance calculated, then you can control something else like motors or steering. The way I use it is both, because I rely on differential power to steer my wheeled or tracked robots. Here’s the code:

int echoPin = 2;                         // SRF05 echo pin (digital 2)
int initPin = 3;                            // SRF05 trigger pin (digital 3)
float pulseTime = 0;                   // stores the pulse in Micro Seconds
float distance = 0;                     // variable for storing the distance (cm)

void setup() {
pinMode(initPin, OUTPUT);       // set init pin 3 as output
pinMode(echoPin, INPUT);        // set echo pin 2 as input
Serial.begin(9600);
}

void loop() {
scan();                                                // read the sensor
Serial.println(distance);                       // report what it said
}

float scan() {
digitalWrite(initPin, HIGH);                     // send 10 microsecond pulse
delayMicroseconds(10);                         // wait 10 microseconds before turning off
digitalWrite(initPin, LOW);                      // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH);      // Look for a return pulse, it should be high
distance = pulseTime/58;                        // Distance = pulse time / 58 for cm. 72 for in.
delay(100);                                              // Wait at least 100 Ms before allowing a new scan
return(distance);
}

As you can see, this isn’t rocket science, but fairly easy. Well, actually it CAN be used for rocket science and a lot of people use it that way. Of course, those are the rocket experimenters and not robotics people, so screw ‘em.  I stole the timing algorithm from the luckylarry.co.uk website, just like pretty much everyone else who uses the SRF05. Good old Lucky Larry was the only one with the patience to actually figure out how this stupid thing works.  Hey, credit where it’s due, you know.

Anyway, that’s how to use the Devantech SRF05 ultrasonic range finder. It is so much better than the sensors by Maxbotics in my estimation because it is more accurate and produces fewer false readings (one reading of many that is way off scale) and they’re usually cheaper.  I buy mine from the RobotShop for about $27, which, considering everything, is pretty cool.

Here’s a photo of a little autonomous vehicle that used a pair fore and aft to allow the robot to drive forward or backwards. I called it the PushPull. Also shown is an Arduino Duemilanove and Adafruit Motor Controller. The top layer of the boards is a proto shield and baby breadboard I used simply to make connections to the sensors with. It would drive along until it met an obstacle too wide to steer around, and so it would just reverse direction and continue on patrol. The robot used a pair of twin Tamiya gear motors, one front and one rear, a set of 4 Tamiya ‘off road’ tires, and some Erector Set parts for the chassis.  A honking 3000 Mah Lipo 11.2 v battery gave it enough power to drive around for 50 minutes before recharging was necessary.

by | Categories: Stuff I Wrote |

Share with others

No Responses so far | Have Your Say!

Comments are closed.

p5rn7vb