SRF04 Sonar

We chose the SRF04 Sonar for its abundance in the lab, and ease of use: given a short trigger pulse, the sonar provides a corresponding echo pulse. Once the timing of this echo pulse is known, the length of that pulse in inches or centimeters can be found by a simple division. Measuring a test object’s height with a ruler showed that the sonar was accurate to within 1 centimeter, which was certainly adequate for our goals.

The precise operation and timing of the SRF04 is given in the following diagram.

Timing specs for the SRF04 sonar's pulses

To properly connect the device we followed the pin diagram given in the SRF04 PDF.  We were able to connect the sonar to the power supply on the Seeeduino board without using an external regulator.  However, future projects should use an external power regulator so that the power fluctuations caused by the sonar do not interfere with other devices.

To test that the sonar was functioning properly, we wrote a small piece of code to start the sonar and output results serially so we could verify them with the aid of a serial monitor.

#include "sonar_test.h"

#define ECHO_PIN                      13
#define TRIG_PIN                      12

Triggers sonar and returns the distance of sensed object in centimeters.
For return in inches, divide distance by 148 instead of 58.
Maximum range is ~180 centimeters.
int doSonar() {
	// Sonar setup stuff
	digitalWrite(TRIG_PIN, HIGH);                  // Send a 10uS high to trigger ranging
	digitalWrite(TRIG_PIN, LOW);

	// Now ping for objects
	int distance = pulseIn(ECHO_PIN, HIGH);        // Read in times pulse
	distance = distance / 58;                     // Calculate distance from time of pulse: centimeters
	return distance;

void setup() {
	// Setup deb
	pinMode(ECHO_PIN, INPUT);

void loop(){