![]() |
| HC-SR04 ultrasonic sensor - $3 on eBay! |
Hooking up HC-SR04 Ultrasonic Sensors, a i2c serial LCD, and an LED to a robot
Adding 4 HC-SR04 sensors, a serial LCD display, and another box to a robot
HC-SR04 Ultrasonic Sensor with TI Launchpad MSP430 and Energia
HC-SR04 Ultrasonic sensor - object avoiding Arduino robot. - ClusterBot
I've got my HC-SR04 ultrasonic sensors out again, and I was having a hard time getting the Ultrasonic.h libraries to work with Arduino 1.0.1. The compiler was saying it couldn't find the file 'WProgram.h'.
In versions of Arduino later than 1.0, WProgram.h was renamed to Arduino.h
Getting the libraries to work is a simple matter of editing the Ultrasonic.h and Ultrasonic.cpp files and replacing WProgram.h with Arduino.h
If you don't feel like doing this, you can just download the HC-SR04 library below. I've already edited the library to work with new versions of Arduino. Download the file, then copy the Ultrasonic folder into your Arduino library.
HC-SR04 Ultrasonic Sensor Arduino Library - 2k ZIP file
I did some rudimentary testing with my HC-SR04, and I found it is pretty stable out to 40 inches. Past 40 inches, you start to get some flaky readings. I will try and make some accuracy measurements later, but so far it looks pretty close to what it is reading. I'll publish my test setup, in case you want to try it out.
Plug your HC-SR04 into a breadboard then make the following connections to the Arduino:
1. 5V on Arduino to VCC of HC-SR04
2. Dig pin 12 on Arduino to Trig of HC-SR04
3. Dig pin 13 on Arduino to Echo of HC-SR04
4. GND on Arduino to GND of HC-SR04
Now, copy/paste and upload this code:
#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,13);
void setup() {
Serial.begin(9600);
}
void loop()
{
Serial.print(ultrasonic.Ranging(INC));
Serial.println("in");
delay(100);
}
Once you have uploaded the code, go to tools and select 'Serial Monitor'. The serial monitor window should open and you will get a constantly updating distance from the HC-SR04 to whatever is in front of it.
If you want metric, change the INC in the code above to CM and change the "in" in the print statement to "cm".
How well did this work for you? I think these HC-SR04's are pretty damn good for 3 dollars apiece.
HC-SR04 Ultrasonic Sensor Arduino Library - 2k ZIP file
I did some rudimentary testing with my HC-SR04, and I found it is pretty stable out to 40 inches. Past 40 inches, you start to get some flaky readings. I will try and make some accuracy measurements later, but so far it looks pretty close to what it is reading. I'll publish my test setup, in case you want to try it out.
![]() |
| HC-SR04 on breadboard , connected to Arduino Nano. |
Plug your HC-SR04 into a breadboard then make the following connections to the Arduino:
1. 5V on Arduino to VCC of HC-SR04
2. Dig pin 12 on Arduino to Trig of HC-SR04
3. Dig pin 13 on Arduino to Echo of HC-SR04
4. GND on Arduino to GND of HC-SR04
Now, copy/paste and upload this code:
#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,13);
void setup() {
Serial.begin(9600);
}
void loop()
{
Serial.print(ultrasonic.Ranging(INC));
Serial.println("in");
delay(100);
}
Once you have uploaded the code, go to tools and select 'Serial Monitor'. The serial monitor window should open and you will get a constantly updating distance from the HC-SR04 to whatever is in front of it.
If you want metric, change the INC in the code above to CM and change the "in" in the print statement to "cm".
How well did this work for you? I think these HC-SR04's are pretty damn good for 3 dollars apiece.

