Figure 2. SONAR and control boards.
pulse coming in on D3. The cap C1 delays the rise of the
D3 line long enough that the PulseIn command will see a
logic low transition to a logic high. This is essential because
the PulseIn command will not start timing until it sees the
D3 line transition from a low to a high logic level. When the
SONAR sees an echo come back, it will drive the Echo line
high which turns on Q2, which then pulls the D3 line low.
This terminates the PulseIn command and returns a timer
value in microseconds.
This circuit is a fancy way to handle three I/O lines’
work, but uses only two I/O lines because the signal line
from the SONAR board does not reach 3V. I added R5
which allows the logic high voltage seen by the Arduino to
be over 3V so that the microcontroller will see this as a
valid logic level and still have a low resistance path to pull
the Init line to the SONAR low. The connector on this
SONAR board is kind-of odd, so it is easier to solder wires
to the SONAR controller chip U2 than it is to make a cable
Listing 1: The Arduino SONAR code.
/*
SONAR
Uses an old Polaroid SONAR unit to measure
distance on an Arduino.
On a cool day (15 C) at sea level the speed
of sound is about 340.3m/s.
Because our timer runs in microseconds we’ll
convert that number to cm/us thusly:
1/34030 1,000,000 = about 29.4us/cm.
Because we’re measuring out and back, we
double that to about 59us/cm. There are
2.54 inches in a cm, so that number becomes
2.54 29.4 2 = about 149us/in.
The Arduino gives us the pulseIn command which
will measure the duration of a pulse in
microseconds. The hacked out Polaroid
SONARs need to have their power cycled between
pings, so we’ll use an interesting circuit
that controls the power on the board
with one I/O pin and will both ping and read
back the pulse delay with another pin. See
the accompanying circuit to see how we do
this. Simple!
I’ve found that these boards only work past
about 18 inches. Some of the Poloroid boards
will work at ranges as close as 11 inches.
On the bright side, they work out to over
10 feet. This is a longer range SONAR than
your modern day small robot boards.
Code by Mr. Roboto May 2010
Use it any way you like.
*/
const int powerPin = 2; // SONAR board power
const int echoPin = 3; // Init/Echo line
// The setup() method runs once, when the
// sketch starts
void setup() {
// Set up our I/O pins and our serial port to
// talk to the Arduino IDE.
Serial.begin(57600);
Serial.print(“SONAR\n”);
pinMode(powerPin, OUTPUT);
pinMode(echoPin,OUTPUT);
digitalWrite(powerPin,HIGH);
digitalWrite(echoPin,LOW);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
long time = 0;
digitalWrite(powerPin, LOW);
// turn the SONAR board on
delay(200);
// wait for board to stabilize
pinMode(echoPin,INPUT);
// pull init pin high
time = pulseIn(echoPin,HIGH);
// Get the echo duration time
time-=200;
// subtract out some delays
// I fudged some here, your adjustment
// may vary...
Serial.print(time/149);
Serial.print(“ in “);
Serial.print(time/59);
Serial.println(“ cm”);
digital Write(powerPin,HIGH);
// Turn power off
pinMode(echoPin,OUTPUT);
// Make the ping/echo pin an output
delay(1000);
// Delay a second for next pulse
}
14 SERVO 07.2010