Distance measurement with arduino and ultrasonic sensor

Application of arduino and ultrasonic sensor

The HCSR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with arduino and ultrasonic sensor and how to use ultrasonic sensor with arduino.

The following things are needed to measure the distance with arduino and ultrasonic sensor :

  • Arduino board
  • Ultrasonic sensor
  • Breadboard
  • Connecting wires
  • LED

ARDUINO and Ultrasonic sensor

Step 1

Begin the connections by inserting the ultrasonic sensor to the breadboard and connecting the Vcc power lines and ground to the arduino +5v Vcc supply and ground respectively. The remaining pins are Trigger and Echo, connect the trigger pin to the arduino 12 pin and Echo to the arduino 11 pin as per the arduino program prepared.

Step 2

The ultrasonic sensor and the arduino board is configured perfectly. Now the 9 pin of arduino is connected to the LED positive and the negative terminal to the ground of the arduino board.

Step 3

The connection part is done and now the arduino is connected to the pc in order to upload the arduino program in it using the cable

Program

int trigPin = 12; //triggor pin
int echoPin = 11; // echo pin
long timeperiod, cm, inches;


void setup()
{
Serial.begin(9600); //serial port communication
pinMode(trigPin, OUTPUT); // defining pinmode for trig
pinMode(echoPin, INPUT);  // defining pinmode for echo pin
}

void loop()
{
digitalWrite(trigPin, LOW);// sending 10 us pulse
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
timeperiod = pulseIn(echoPin, HIGH);  // integrating pulse
inches = microsecondsToInches(timeperiod);
cm = microsecondsToCentimeters(timeperiod);
Serial.print("distcance in inches=");
Serial.print(inches);
Serial.print("   distance in centimeters=");
Serial.print(cm);
Serial.println();
delay(10);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

You can download the above program.

Distance measurement with ARDUINO and Ultrasonic sensor

The ultrasonic sensor sends the ultrasound wave and if any object is being detected, it will receive the ultrasound back and process the signal according to our program. The data can be converted into centimeters as well as inches. When the distance between ultrasonic sensor and the object is less, the brightness value will be low and vice versa for if the distance is more.

Do share with friends and do tell us in comment below how you feel about.


Summary
Distance measurement with arduino and ultrasonic sensor
Article Name
Distance measurement with arduino and ultrasonic sensor
Description
Here we have brought the simple method to measure the distance with arduino and ultrasonic sensor and how to use ultrasonic sensor with arduino.
Author
Publisher Name
EEE Projects
Publisher Logo
  •  
  •  
  •  
  •  
  •  
  •  

This article has 4 comments

  1. jonathan Reply

    well it seems to me much simple than it really is ! do appreciate your effort though




    4
  2. Garry Reply

    i want to make a distance measurement using ultrasonic sensor and 8051 microcontroller




    0

Leave a Comment

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