Sonar Made Simple
Sonar Made Simple
Overview
With the Devantech SRF04 sonar range finder sensor and the IntelliBrain™
robotics controller, you can enable your robot to see its surroundings through a
set of sonar “eyes“.
Theory of Operation
A sonar range finder works by generating a short burst of sound – a “ping” – then
listening for the echo of the sound when it bounces off the nearest object, as
shown in Figure 1.
Ping
Echo
Figure 1 - Sonar Ping and Echo
By accurately measuring the time from the start of the ping until the echo returns
back to the sensor, the distance to the nearest object can be easily calculated.
Sound travels at 1116.4 feet/second (340.29 meters/second) at sea level. The
distance to the nearest object can be calculated by dividing the elapsed time
Copyright © 2003-2004 RidgeSoft, LLC
1
(time between issuing the sound and hearing the echo) by twice the speed of
sound, as follows:
distance = elapsedTime / (2 * SPEED_OF_SOUND);
The reason for dividing by twice the speed of sound is that the distance to the
object is only half the distance the sound wave actually travels. The sound wave
must travel to the object and back to the sensor in order for the sensor to hear
the echo.
Sensor Operation
The Devantech SRF04 sonar range finder is operated by generating a pulse on
its trigger input signal. This causes the range finder to issue a ping. The range
finder enables its receiver 100 microseconds after the ping and raises the
sensor’s echo output signal. (The delay in enabling the receiver prevents the
receiver from hearing the transmission of the ping.) When the receiver hears the
echo it drops the output signal. The elapsed time in microseconds between the
ping and the echo can be determined by measuring the pulse duration on the
echo line and adding 100 microseconds, as follows:
elapsedTime = pulseDuration + 100;
Ping
Echo Arrival
Trigger Input
> 10 usec
Echo Output
elapsed time
less 100 usec
Figure 2 - Devantech SRF04 Signals
Copyright © 2003-2004 RidgeSoft, LLC
2
Sensor Connections
You must construct a cable to connect the Devantech SRF04 to the IntelliBrain
robotics controller. The cable requires four wires (26 AWG stranded wire
recommended): power (red), ground (black), trigger input (green) and echo
output (white). Solder the wires to the SRF04 sensor, as shown in Figure 3. You
will need to attach two connectors to the IntelliBrain end of the cable, as shown in
Figure 4. The connector parts and tools you will need are listed in Table 1.
Power (red)
Echo Output (white)
Trigger Input (green)
Ground (black)
Figure 3 - Devantech SRF04 Connections
Connectors
Ground (black)
Power (red)
Echo (white)
Trigger (green)
Figure 4 - Range Sensor Connector
Table 1 - Connector Parts and Tools
Part
Molex Part Number
Digi-Key Part Number
Crimp Terminals
16-02-1109
WM2555-ND
3 Circuit Housing
50-57-9003
WM2801-ND
Universal Crimp Tool
63811-1000
WM9999-ND
Insertion Tool
11-02-0022
WM9911-ND
Wire Cutter & Stripper
Copyright © 2003-2004 RidgeSoft, LLC
3
Attach the trigger signal connector to digital I/O port 1 on the IntelliBrain as
indicated in Figure 5. Attach the echo signal connector to digital I/O port 4.
(Note: You may connect the SRF04 to other digital I/O ports; but, the example
program uses ports 1 and 4).
Trigger
Echo
Digital IO 1
Digital IO 4
Ground (black)
Power (red)
Trigger (green)
Echo (white)
RoboJDE v1.3.0r3
Ready
START STOP
Figure 5 – Connections to the IntelliBrain Controller
Interfacing to the Sonar Sensor in Software
The DevantechSRF04 class in the RoboJDE class library supports the SRF04
sonar sensor. Conveniently, this class makes it very easy to use the sensor by
doing the necessary pulse measurement and calculations to find the distance to
the nearest object. Interfacing with the sensor only requires the following steps:
1. Configure the trigger port as an output.
2. Construct a DevantechSRF04 object.
3. Call the ping() method.
4. Wait while the sound travels to an object and back.
5. Call the getDistanceInches() or getDistanceCm() method to read the
distance.
Configuring the Trigger Port
The digital I/O ports on the IntelliBrain are configured as input ports by default.
Since the SRF04 trigger is an output from the IntelliBrain, the following Java
statements are needed to configure the trigger port as an output.
Copyright © 2003-2004 RidgeSoft, LLC
4
IntelliBrainDigitalIO trigger = IntelliBrain.getDigitalIO(1);
trigger.setDirection(true);
Constructing the Range Finder Object
The DevantechSRF04 range finder object in the RoboJDE class library requires
references to the two ports it uses to interface to the sensor. The following
statement constructs the range finder object passing it references to the trigger
port and the echo port (digital I/O 4).
RangeFinder rangeFinder = new DevantechSRF04(trigger,
IntelliBrain.getDigitalIO(4));
Taking a Reading
The following statements are all that is required to take a distance reading from
the sensor.
rangeFinder.ping();
Thread.sleep(40);
float distance = rangeFinder.getDistanceInches();
The call to the ping() method instructs the range finder to issue a ping. The
next statement puts the current thread to sleep for 40 milliseconds, giving the
sound burst time to travel from the sensor to the nearest object and back to the
sensor. When the thread wakes up, the call to getDistanceInches() reads
the distance measured by the sensor in inches. This method will return -1 in the
event the sensor did not sense an echo signal. The sensor will not sense the
echo if it is too far to the nearest object or if the sensor wiring is incorrect.
The maximum measurable distance by the Devantech SRF04 is a little bit less
than 20 feet. The minimum measurable distance is about 3 inches.
Example Application
RoboJDE includes a small example application that continually takes sonar
readings using the Devantech SRF04 and displays the measurement on the
IntelliBrain controller’s LCD screen.
Conclusion
With the IntelliBrain robotics controller and the RoboJDE Java-enabled robotics
software development environment, adding a Devantech SRF04 sonar range
finder to your robot is a simple and inexpensive way to enable your robot to “see”
its surroundings through sonar “eyes.”
Copyright © 2003-2004 RidgeSoft, LLC
5