Ultrasonic HC-SR04 Sensor

I’ve stumbled by this neat little sonar sensor on eBay a few days ago, I’ve had some bad experiences with other similiar ultrasonic sensors so I was kind of skeptic to this one. But for the low price I couldn’t resist. Anyhow, it turned out to be a great sensor. It’s accurate down to 1 cm, which is perfect and in most cases more than enough. You get it for around 5$ on eBay.

 

Only problem I ran into when using it with arduino was the library, got some nasty compiling errors. So I changed a few lines in the library and made it work with Arduino 1.0 and older arduino IDEs.

Download HC-SR04 Library for Arduino 1.0

Also wrote a little example for using it with a serial display:

 

[codesyntax lang=”cpp” lines=”no” blockstate=”collapsed”]

#include <SoftwareSerial.h>
#include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13);

SoftwareSerial mySerial(5,4);

void setup()
{
mySerial.begin(9600);
}

void loop()
{
mySerial.print("Distance: ");
mySerial.print(ultrasonic.Ranging(CM));
mySerial.println(" Cm.");
delay(250);
mySerial.print("?f"); // Clear display.
}

[/codesyntax]

Leave a Reply

Your email address will not be published. Required fields are marked *