HC-SR04 Ultrasonic range finder

The HC-SR04 is a fairly straightforward rangefinder, and we hope to show here how to use it.
The device has 4 pins, VCC, Trigger, Echo and ground. You send a high to trigger, and it sends a high back to echo when it gets a pulse back. All you have to do is time how long it took and convert that to distance.

There are several example Arduino sketches around, though they all output the distance to the Arduino serial monitor which we felt was a shame, so we wrote our own to output to a 16X02 LCD from an example here by ScottC
Below is a Fritzing diagram of our layout which is a lot more complicated because it has an LCD in it.

HC-SR04 Ultrasonic rangefinder with LCD screen and Arduino

You don't need to use an Arduino, a compatable will do, it also need not be an Uno as long as it works at 5V. If not you may need to do some level shifting.

 

/*
 HC-SR04 Ping distance sensor:
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
 on 10 Nov 2012.
 
 Sketch modified by ChilliTronix to add LCD 1602 output on the 21st of July 2014.
 */
 
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD R/W to GND
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 10
* LCD D5 pin to digital pin 9
* LCD D6 pin to digital pin 8
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K Variable resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*HC-SR04 positive to 5V, ground to ground.
*HC-SR04 Trigger to pin 5
*HC-SR04 Echo to pin 6.
*/

 

#include <LiquidCrystal.h>

//Initialise LCD Library

LiquidCrystal lcd(12,11,10,9,8,7);

#define echoPin 6 // Echo Pin
#define trigPin 5 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600); //Allows serial output to serial monitor
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
 lcd.begin(16,2); //Say what format of LCD we are using.
 lcd.clear();
 lcd.setCursor(0,0); //Goto line 0 char 0
 lcd.print("ChilliTronix");
 lcd.setCursor(0,1); //Goto second line
 lcd.print("distance measure");
 delay(1500);
 lcd.clear();
 lcd.setCursor(0,0); //Goto line 0 char 0
 }

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Dist: ");
 lcd.print(distance);
 lcd.setCursor(12,0);
 lcd.print("CM"); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Dist: ");
 lcd.print(distance);
 lcd.setCursor(12,0);
 lcd.print("CM"); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

The sketch for this layout is here if you want to download it.