First things first, we need to build our circuit. This is the easy bit! We’ll be using the Arduino to control a servo that will rotate our sensor around 180 degrees. The Arduino will then send the value from the sensor along with the current angle of the servo to the serial port.
Before proceeding please take a moment to check out some of my other work with the SRF-05 and servos if you’re unfamiliar with either.
Stuff with SRF-05
Stuff with Servos
I’m building this with the SRF-05 ultrasound range finder, but because this has a fairly wide field of detection it’s not very precise – I think I’ll end up trying a different range finder maybe an IR one as the SRF-05 works best as a static sensor/ detector, anyway…
Shopping list
SRF05 Ultrasonic range finder
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
1x Servo (has to need no more than 5v supply)
You’ll also need some way to mount the sensor to the servo.
The Circuit
Straight forward, we have the Arduino providing power to the breadboard and we have the servo and the SRF-05 sharing this power. Then we have the servo output pin going to Arduino digital pin 9 and the SRF-05 pins going to digital pin 2 and 3. You’ll notice that in my pictures I have 2 servos – I’m just using the bottom one of the pair to rotate the sensor round. On your servo you’ll need to figure out a way to mount the sensor on to the servo wheel – I used a lot of blu-tac! You’ll also see I’ve mounted my sensor vertically so that the when the servo moves there’ll be less interference with recieving values – placing the sensor horisontally seemed to give differences of up to and sometimes over 5cm between the first and second readings.
My servos do tend to move a bit so I’ve used more blu-tak/ modelling clay to hold them down and in place – if the servos move other than the way they’re meant to then it means dodgy readings.
The Sketch
The hardest bit – rotate the servo from left to right, then right to left and for every degree of movement take a series of readings and send them to the serial port. We’ll want to produce an average reading value for consistancy. Unfortunately with this ultrasound sensor we have to be quite slow to make sure we’re getting accurate values and we have to allow time for the signal to come back each time and register in order to produce the average value.
We do the rotation using a for loop to count to 180 and for each iteration we move the servo by +1 or -1 depending on which way we’re going – if you’ve hacked your servos then you can do a full 360 loop. During this loop we do another FOR loop to count to 10/ numReadings and for each iteration we add the distance measured to the total and after 10 readings we get our average by dividing the total by the number of readings. Then reset the total and the counter to start again for the next servo position. Finally before finishing theĀ the FOR loop for the servo we output the servo position and average reading to the serial port each with a preceeding character for us to later use to identify the values when reading the serial port in Processing. The last line is using println which will start a new line for the next set of values – each reading has its own line in the serial buffer makign it much easier to get our values back out.
/* luckylarry.co.uk Radar Screen Visualisation for SRF-05 Sends sensor readings for every degree moved by the servo values sent to serial port to be picked up by Processing */ #include <Servo.h> // include the standard servo library Servo leftRightServo; // set a variable to map the servo int leftRightPos = 0; // set a variable to store the servo position const int numReadings = 10; // set a variable for the number of readings to take int index = 0; // the index of the current reading int total = 0; // the total of all readings int average = 0; // the average int echoPin = 2; // the SRF05's echo pin int initPin = 3; // the SRF05's init pin unsigned long pulseTime = 0; // variable for reading the pulse unsigned long distance = 0; // variable for storing distance /* setup the pins, servo and serial port */ void setup() { leftRightServo.attach(9); // make the init pin an output: pinMode(initPin, OUTPUT); // make the echo pin an input: pinMode(echoPin, INPUT); // initialize the serial port: Serial.begin(9600); } /* begin rotating the servo and getting sensor values */ void loop() { for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right. leftRightServo.write(leftRightPos); for (index = 0; index<=numReadings;index++) { // take x number of readings from the sensor and average them digitalWrite(initPin, LOW); delayMicroseconds(50); digitalWrite(initPin, HIGH); // send signal delayMicroseconds(50); // wait 50 microseconds for it to return digitalWrite(initPin, LOW); // close signal pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return distance = pulseTime/58; // convert to centimetres total = total + distance; // update total delay(10); } average = total/numReadings; // create average reading if (index >= numReadings) { // reset the counts when at the last item of the array index = 0; total = 0; } Serial.print("X"); // print leading X to mark the following value as degrees Serial.print(leftRightPos); // current servo position Serial.print("V"); // preceeding character to separate values Serial.println(average); // average of sensor readings } /* start going right to left after we got to 180 degrees same code as above */ for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left leftRightServo.write(leftRightPos); for (index = 0; index<=numReadings;index++) { digitalWrite(initPin, LOW); delayMicroseconds(50); digitalWrite(initPin, HIGH); delayMicroseconds(50); digitalWrite(initPin, LOW); pulseTime = pulseIn(echoPin, HIGH); distance = pulseTime/58; total = total + distance; delay(10); } average = total/numReadings; if (index >= numReadings) { index = 0; total = 0; } Serial.print("X"); Serial.print(leftRightPos); Serial.print("V"); Serial.println(average); } }
Part 2: Visualising the Data
Part 3: Visualising the Data from Sharp Infrared Range Finder
This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.
- Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 2: Visualising the Data
- Arduino + Processing: Make a Radar Screen – Part 3: Visualising the Data from Sharp Infrared Range Finder
- Arduino + Processing: Getting values from SRF05 ultrasound sensor & serial port
- Arduino + Processing: 3D Sensor Data Visualisation
- Arduino: Basic Theremin meets Processing!
- Arduino: Sonic range finder with SRF05
- Obstacle avoidance robot – build your own larryBot
- larryBot – versions 0.1 to 0.5 lessons learned
- Arduino: (Very) Basic motion tracking with 2 PIR sensors
- Arduino: A Basic Theremin













Very good work! I had this idea to time but not had the opportunity to implement!
Congratulations!
thanks! took me a while to get it working in Processing – in the end I found that the SRF-05 sonar sensor isn’t actually that accurate at detecting small objects in its path at least not when being rotated anyway.
Very cool. Your project makes me wonder about using Arduino to help an ARM do localization and mapping simultaneously.
Congratulations !!
Jeronimo
http://www.blogdoje.com.br
AVR,Arduino & ARM
nice can this program also use the ping sensor from parallax? if so how
You can use the Parallax ping sensor its basically the same thing, in fact any distance sensor/ range finder will work. With the parallax ping you just need to find some info on wiring it up and there’ll be plenty of code out there. Try here: http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor then you just need to alter my code with the exmple above once you got that working.