Sunday, June 12, 2016

Ultrasonic Distance Measurement with Arduino

We can use ultrasonic sensors to measure distance with Arduino. There are several types of ultrasonic sensors available in the electronic market. In this example, I am using the famous HC-SR04 type ultrasonic sensor. This sensor is cheap, easy to connect to Arduino and highly used in robotic projects to identify obstacles. I will also use a LCD screen to display the distance in both Inches and Centimeters.




How Ultrasonic Sensors Work!

Active ultrasonic sensors generate high-frequency sound waves and evaluate the echo which is received back by the sensor, measuring the time interval between sending the signal and receiving the echo to determine the distance to an object. 

Here is a very good article to read more information

Since we are using LCD screen in our project. you need to import the LiquidCrystal library first. You can also use the serial monitor window to display the distance without using LCD. I will give you both code examples. To read the distance, our code sends a pulse to the sensor to initiate a reading, then it listens for a pulse to return. The length of the returning pulse is proportional to the distance of the of the object from the sensor.

The HC-SR04 sensor has four pins. First and last pins for +5V and Ground. You can connect them to power pins in your Arduino board (Please refer the schematic). Other pins are trigger and echo pins which we use to get data from the sensor.

Arduino connectivity

You can follow the below schematic to connect your ultrasonic sensor to the Arduino board.


If you are going to use the serial monitor instead of LCD screen to display the distance, just connect the ultrasonic sensor only.

Now lets move to the source code.

Below code example will show you the distance on the LCD screen

#include <LiquidCrystal.h>

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

int pingPin = 4; 
int inPin = 5; 
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147; 
int cmconv = 59;
String s1, s2;

void setup() {
  lcd.begin(16, 2);
  pinMode(pingPin, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop()
{
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);

  duration = pulseIn(inPin, HIGH);

  inches = microsecondsToInches(duration);
  indec = (duration - inches * inchconv) * 10 / inchconv;
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  s1 = "Distance: " + String(inches) + "." + String(indec) + "in";
  s2 = "Distance: " + String(cm) + "." + String(cmdec) + "cm";
  lcd.setCursor(0, 0); 
  lcd.print(s1);
  lcd.setCursor(0,1);
  lcd.print(s2);
  
  delay(1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}

Below code example will show the distance on Serial monitor (Without the LCD screen)

int pingPin = 4; 
int inPin = 5; 
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147; 
int cmconv = 59;
String s1, s2;

void setup() {
  pinMode(pingPin, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop()
{
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);

  duration = pulseIn(inPin, HIGH);

  inches = microsecondsToInches(duration);
  indec = (duration - inches * inchconv) * 10 / inchconv;
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  s1 = "Distance: " + String(inches) + "." + String(indec) + "in";
  s2 = "Distance: " + String(cm) + "." + String(cmdec) + "cm";
  Serial.print(s1);
  Serial.println();
  Serial.print(s2);
  
  delay(1000);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / inchconv;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / cmconv;
}

Here is a picture of my completed project


Hope you enjoyed the tutorial. If you found any issue during the project, please comment.



1 comment: