An Ultrasonic Distance Sensor such as the HC-SR04 is an easy module to add to an Arduino Uno or other Arduino board. In this example video we see different distances lighting up different LEDs:
This HC-SR04 board (Amazon.com) is similar to the PING)))) ultrasonic sensor. This was easy to use, plug the pins into:
- VCC = 5 volt rail power
- trig = Digital Out
- echo = Digital In
- GND = Negative (ground) rail
The HC-SR04 seems to be rather accurate at close range (5-30 inches, 12-75cm) but after about 70 inches (~175cm) it really loses the ability to 'see' my hand or my body. It does better with larger objects such as a wall or a submarine. Clicking on the Serial Monitor icon on the top-right of the Arduino software shows us the distances that the HC-SR04 is detecting.
Code below was adapted from the built-in Arduino eamples, by David A. Mellis and Tom Igoe:
/* HC-SR04 Distance Sensor
This sketch reads an HC-SR04 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 HC-SR04 attached to +5V
This sketch reads an HC-SR04 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 HC-SR04 attached to +5V
* trig connection of the HC-SR04 attached to Digital 3
* echo connection of the HC-SR04 attached to Digital 4
* GND connection of the HC-SR04 attached to ground
* Green LED is attached with a 560 Ohm resistor to pin 11
* Red LED 1 is attached with a 560 Ohm resistor to pin 10
* Red LED 2 is attached with a 560 Ohm resistor to pin 9
Code created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
* GND connection of the HC-SR04 attached to ground
* Green LED is attached with a 560 Ohm resistor to pin 11
* Red LED 1 is attached with a 560 Ohm resistor to pin 10
* Red LED 2 is attached with a 560 Ohm resistor to pin 9
Code created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
Modified 2012 08 14 lefty.crupps,
http://gnuski.blogspot.com/2012/08/arduino-ultrasonic-sensor-pings.html
This example code is in the public domain.
*/
// Define our variables (int)
// Distances for the various levels of Closeness (inches)
http://gnuski.blogspot.com/2012/08/arduino-ultrasonic-sensor-pings.html
This example code is in the public domain.
*/
// Define our variables (int)
// Distances for the various levels of Closeness (inches)
int l3 = 20; // Level 3 is very close, 20 inches or closer
int l2 = 40;// Level 2 is close
int l1 = 42; // Level 1 is nearby
int pingPin = 3; // Out trig
int echoPin = 4; // In echo
int grPin = 11; //green LED
int r1Pin = 10; //red1 LED
int r2Pin = 9; //red2 LED
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(grPin, OUTPUT); //sets the Green led pin to output
pinMode(r1Pin, OUTPUT); //sets the Red1 led pin to output
pinMode(r2Pin, OUTPUT); //sets the Red2 led pin to output
}
// Function to turn off all LEDs
int grPin = 11; //green LED
int r1Pin = 10; //red1 LED
int r2Pin = 9; //red2 LED
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(grPin, OUTPUT); //sets the Green led pin to output
pinMode(r1Pin, OUTPUT); //sets the Red1 led pin to output
pinMode(r2Pin, OUTPUT); //sets the Red2 led pin to output
}
// Function to turn off all LEDs
void off(){
digitalWrite(r1Pin, LOW);
digitalWrite(r2Pin, LOW);
digitalWrite(grPin, LOW);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
// long duration, inches, cm;
long duration, inches, cm;
off();
// The HC-SR04 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 echoPin reads the returning Ultrasonic 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
digitalWrite(r1Pin, LOW);
digitalWrite(r2Pin, LOW);
digitalWrite(grPin, LOW);
if (inches < 1500 && inches > l2)
{
digitalWrite(grPin, HIGH);
delay(500);
digitalWrite(grPin, LOW);
delay(1500);
}
else off();
if (inches < l2 && inches > l3)
{
digitalWrite(r1Pin, HIGH);
delay(500);
digitalWrite(r1Pin, LOW);
delay(1500);
}
else off();
if (inches < l3)
{
digitalWrite(r2Pin, HIGH);
delay(150);
digitalWrite(grPin, LOW);
delay(150);
digitalWrite(r2Pin, HIGH);
delay(150);
digitalWrite(grPin, LOW);
delay(150);
}
else off();
off();
delay(100);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
// We'll use the same info for the HC-SR04
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
// We'll use the same info for the HC-SR04
return microseconds / 29 / 2;
}
return microseconds / 29 / 2;
}