Ultrasonic range finder using arduino.

Ultrasonic range finder using 8051 mictrocontroller has been already published by me in this website. This time it is an ultrasonic range finder using arduino. HC-SR04 ultrasonic range finder module is used as the sensor here. The display consists of a three digit multiplexed seven segment display. This range finder can measure up to 200 cm and has an accuracy of 1cm. There is an option for displaying the distance in inch also. Typical applications of this range finder are parking sensors, obstacle warning system, level controllers, terrain monitoring devices etc. Lets have a look at the HC-SR04 ultrasonic module first.

HC SR04 ultrasonic module.

HC SR04 is an ultrasonic range finding module with an accuracy of 0.3cm. The sensing range of this module is from 2cm to 5 meter. Working current of this sensor is 15mA and the measuring angle is 15°. The photograph of front and back side of the HC-SR04 sensor is shown in the figure below.

SR04 ultrasonic range finder

HC-SR04 has four pins. Their names and functions are explained below.

  • Vcc: 5V supply voltage is given to this pin.
  • Trigger: A 10uS long pulse is given to this pin for triggering the transmission. Upon receiving a valid trigger pulse, the HR-SR04 issues eight 40KHz pulses. Time taken by these pulses to reflect back is measured and the distance is calculated from it.
  • Echo: At this pin the HC-SR04 outputs a signal whose high time is proportional to the range.
  • Ground : Ground is connected to this pin.
The timing diagram of  HC-SR04 is shown in the figure below.

HC-SR04 timing diagramCircuit diagram.

Full circuit diagram of the ultrasonic range finder using arduino is shown in the figure below.
ultrasonic range finder using arduino

Trigger pin of the ultrasonic range finder module is connected to digital pin 0 of the arduino. Echo pin of the ultrasonic module is connected to the digital pin 13 of the arduino. SPDT switch S1 is used to select the unit of the measurement shown in the display. Pole of the SPDT switch S1 is connected to digital pin 4 of the arduino. If digital pin 4 is held high, the output will be in centimeters and if the digital pin 4 is held low, the output will be in inches. Digit driver transistor Q1, Q2 and Q3 of the arduino are interfaced to digital pins 1, 2 and 3 of the arduino. Multiplexed segments a to dp are interfaced to digital pins 5 to 12 of the arduino. The arduino board can be powered through the +9V jack given on the board. 5V supply needed in some other parts of the circuit can be obtained from the 5V source available in the arduino board. Resistors R9, R10 and R11 limits the base current of the corresponding transistors. 330 ohm resistors R1 to R8 limits the current through the corresponding segments. Pin out of an E1-3056ASR1 three digit MUX seven segment display in shown in the figure below.

multiplexed 7 segment display pinout

Program.

#include <NewPing.h>
#define trig 0
#define echo 13
#define maximum 200
int a;
int unit;
int usec;
int input=4;
int disp1=1;
int disp2=2;
int disp3=3;
int segA=5;
int segB=6;
int segC=7;
int segD=8;
int segE=9;
int segF=10;
int segG=11;
int segDP=12;
NewPing sonar(trig, echo, maximum);
void setup()
{
  pinMode(disp1, OUTPUT);
  pinMode(disp2, OUTPUT);
  pinMode(disp3, OUTPUT);
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(segDP, OUTPUT);
  pinMode(input, INPUT);
}
void loop()
{
 delay(20);
 usec=sonar.ping(); //sends ping and estimates the duration of echo in uS
 unit= digitalRead(input); //reads the status of cm/inch selector switch
 if(unit==1)
 {
 usec=usec/58; // distance in cm
 }
 else
 {
   usec=usec/148; // distance in inch
 }
  a=usec%10;
  digitalWrite(disp1,LOW);
  digitalWrite(disp2,LOW);
  digitalWrite(disp3, HIGH);
  digitalWrite(segDP,HIGH);
  display(a);
  delay(4);
  usec = usec/10;
  a = usec%10;
  digitalWrite(disp3,LOW);
  digitalWrite(disp2,HIGH);
  digitalWrite(segDP,HIGH);
  display(a);
  delay(4);
  usec=usec/10;
  a=usec;
  digitalWrite(disp2,LOW);
  digitalWrite(disp1,HIGH);
  digitalWrite(segDP,HIGH);
  display(a);
  delay(4);
  }
  int display (int a)
  {
  switch (a)
  {
    case 0:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(segG, HIGH);
     break;

    case 1:
    digitalWrite(segA, HIGH);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 2:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, LOW);
    break;

    case 3:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, LOW);
    break;

    case 4:
    digitalWrite(segA, HIGH);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;

    case 5:
    digitalWrite(segA, LOW);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;

    case 6:
    digitalWrite(segA, LOW);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;

    case 7:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 8:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
     break;

    case 9:
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;
  }}

About the program.

For communicating with the ultrasonic range finder module, library function <NewPing.h> is used. The job of sending the 10uS trigger pulse, waiting for the echo and measuring the width of the echo etc are done by the library function. Just one line of code usec=sonar.ping()will make the arduino to do all the jobs said above and  the width of the echo pulse  in micro seconds will be stored in the variable usec. Dividing the pulse width in uS by 58 will give the distance in cm and dividing the pulse width in uS by 148 will give the distance in inch. An “if – else” loop is used for selecting the unit according to the position of the SPDT selector switch(S1). Displaying the distance on the three digit 7 segment display is done by the method used in the earlier project Voltmeter using arduino.

The <NewPing.h> library can be downloaded from here: NewPing_v1.5. Download this zip file, unzip it into a folder, name it NewPing or something and copy it into the …….Program Files/Arduino/Library folder.

Ultrasonic range finder with LCD display.

This is just the LCD version of the above project. The working principle of this circuit is same as that of the  7-segment LED version. Only change is on the display device. In this circuit a 16×2 LCD display is used for displaying the distance. The distance in cm and inch are displayed simultaneously on the LCD screen. Before attempting the LCD version, go through this article: Interfacing LCD to Arduino. Circuit diagram of the LCD range finder is shown below.

Circuit diagram: LCD range finder.

ultrasonic rangefinder LCD

Program: LCD range finder.
#include<NewPing.h>
#include<LiquidCrystal.h>
#define trig 0
#define echo 13
#define maximum 200
int usec;
int cm;
float inch;
NewPing sonar(trig, echo, maximum);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
  lcd.begin(16,2);

}
void loop()
{ lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("Range Finder");
  usec=sonar.ping();
  cm=usec/58;
  inch=usec/58/2.54;
  lcd.setCursor(0,1);
  lcd.print(cm);
  lcd.print("cm");
  lcd.setCursor(7,1);
  lcd.print(inch);
  lcd.print("inch");
  delay(250);

}
Author

7 Comments

  1. why are you using arduino instead of microcontroller in this project..

    • @RJ – Its not possible to extend the range of HCSRO4. What you can do is use a different sensor module with a wider range.

  2. thank you for this project and i’ve one question to you, previously you had added this project using microcontroller and now you’ve added this by using arduino so which module will cost more to do. what about the cost of arduino board.
    can i extend this project for my major project. Thank you.

  3. Hello, thanks allot for your work, helped me allot with my final year year project, i was using seven segments and i didn’t know where to start. helped allot.

    Thumps up!

    • i am also going to use this in my major project fnd..

  4. halo, can this code work in raspberry pi? How can I convert these code to make it suitable for raspberry pi?