< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Saturday, February 22, 2014

Quickly make an ultrasound range detector with led display [HC-SR04 quickstart]

Seven-segment Led Range Sensor with Arduino

Measuring distance is an unavoidable stuff for DIYers. Often it is convenient to measure the distance to the ceiling or the water level in an over head tank in a non contact fashion. A simple solution is to use an ultrasound based range detector. Here is a simple and quick introduction to set up HC-SR04 module with an arduino and a seven segment led display. Purpose is to make a basic setup and can further improved to fit different needs. For instance adding an rf transceiver (nrf24l) and a solar cell with rechargeable battery can make it a remote water level measuring device for an overhead tank. After the break there is a simple video demo of the sensor and it seems very accurate. I used this one to quickly measure the dimensions of an apartment without the hurdle of using a tape (surprisingly missing in at my end!!)

Non contact range sensor prototype
To make it simple, i used a library for the sensor called new-ping which seems to support wide range of sensors and provides the distance measurements in inches and centimeters. Further to  provide a simple readout i used a 4X7 segment led module from hobby component and an arduino uno. The complete source code is given below. See the attached figures for easy wiring. The number of pins used can be drastically reduced with led display drivers. But here to make it simple i just used direct drive with couple of resistors and arduino pins 3-13.

Connecting the seven segment led module to arduino

The led module has a total of  12 pins. The connection to the Digital pins 3-13 on the arduino as shown in figure. Do not forget the 220 ohm resistors. The numbers written on are the digital pin number on arduino uno. Also see the sketch (source) if an other arduino (for eg Mega ) is used.
Connecting seven segment led to arduino [click to zoom]

Connecting the ultrasound module HC-SR04

The module has a vcc and ground which goes to 5v and ground pin on arduino. Connect trigger to A0 pin (14) and echo pin to A1 (15).

HC-SR04 Pin connection

If you get an error on compiling, get the library here and unzip it in arduino/library folder. HC7segment library can be obtained here (unzip and copy to arduinos library folder. Rename HC7segment-master folder to HC7segment) . Timed action library can be downloaded at here (http://playground.arduino.cc/Code/TimedAction)

 Runs HC-04 and SRF-06 and other Ultrasonic Modules
 Credits: http://playground.arduino.cc/Code/NewPing
 Also uses HC-SR04 ultrasound sensor from hobbycomponets

/*-----( Import needed libraries )-----*/
#include <NewPing.h>
#include <HC7Segment.h>
#include <TimedAction.h>
TimedAction timedAction = TimedAction(1000,blink);

/* For the Led display */
const byte u8PinOut_Digit[] = {13,6,5,2};
/* Pin order for segment DIO. The segment order is A,B,C,D,E,F,G,DP */
const byte u8PinOut_Segment[] = {3,7,11,9,8,4,12,10}; 
/* Create an instance of HC7Segment(). In this example we will be using a 4 digit 
   common cathode display (CAI5461AH)  */
HC7Segment HC7Segment(4, LOW);

/*-----Pin connection for Ultrasound sensor-----*/
#define  TRIGGER_PIN  14
#define  ECHO_PIN     15
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
  //just for debug 
  Serial.println("Debug:arduino and ultrasound sensor");
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/

}//--(end main loop )---

/*-----timed action which measures the distance-----*/
void blink(){  
  //delay(50);// Wait 50ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  //DistanceIn = sonar.ping_in();// use if you need Inches
  //Serial.print("Ping: "); 
  delay(50);// Wait 50ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  sonar.ping_median(10);//calculate median of 10 , to minimise error
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.println(" cm"); 


  1. Thanks for presenting such a simple guide for how to make a distance detector using an Arduino, HC-SRO4 Ultrasonic Sensor and LED Modules , etc.Its really very useful to make an outstanding ultrasonic range detector.

  2. I don't have library TimeAction

    1. Library Timed action can be downloaded at http://playground.arduino.cc/Code/TimedAction

  3. This comment has been removed by the author.