Ultrasonic Distance Sensor
Parts
-Arduino microcontroller and carrier board
-LiPo battery
-Ping Sensor (Data Sheet)
Prepare the breadboard
Program the Microcontroller
For more information: Ardunio Ping Sensor tutorial
/**
* @file: Ping))) Sensor
* @date: 4/5/2011
*
* @DESCRIPTION
* This sketch reads a PING))) ultrasonic rangefinder and returns the distance
* to the closest object in range. To do this, it sends a pulse to the sensor
* to initiate a reading, then listens for a pulse to return. The length of the
* returning pulse is proportional to the distance of the object from the sensor.
*
*
* The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
**/
#define pingPin 7 // pin number of the sensor's output
//--- Function: Setup ()
void setup()
{
Serial.begin(9600); // initialize serial communication
}
//--- Function: Setup ()
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches:
long duration, inches;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used
to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = duration / 74 / 2;
// convert the time into a distance
Serial.print(inches);
Serial.print("in, ");
delay(100);
}
After hooking up the Sonar Distance Sensor, you can incorporate LEDs into your circuit board to give visual feedback: