Arduino projects: Ultrasonic distance sensor

In one of our previous Arduino project, we used the sensor HC-SR04 to allow a robot to see obstacles while moving and to act accordingly. The HC-SR04 is a very popular ultrasonic sensor and you should definitely learn how to make use of it.

This project will be subdivided into two parts. We want to build a device that will sense the distance from a moving object, send that distance to the serial port and display the distance on an LCD screen. And it is important that the distance is in meter so we can easily apprehend and comprehend it.

To make the project simpler we will use an LCD with an I2C converter so we will need just 4 cables to connect it to Arduino. We will also need to add an additional library to the sketch, but worry not! it is easy to download.

Ultrasonic distance sensor

Here are the needed part:

Let’s start with something simple. How do we display a distance on the serial port?

You first have to make the connections as shown on the schematic below. We do not need to add the LCD yet.

This is how it should look like, very simple right? just 4 cables, the Arduino Uno board and the ultrasonic sensor.

Code

//trig of ultrasonic sensor
int trig = 2;
//echo of ultrasonic sensor
int echo = 3;

void setup() {
 //seting up serial
 Serial.begin(9600);
//set trig as output and echo as input for ultrasonic sensor
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);
}

void loop() {
//thats the sequence for ultrasonic sensor to start reading 
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

//here we got travel time of sonic wave
int duration = pulseIn(echo, HIGH);
//and gere we calculate distance from it
int distance = (duration/2) / 29.1;
//print distance on Serial port
Serial.println(distance);
delay(100);
}

And this is what you should see on the serial port

Ok, half of the project is ready, let’s now add an LCD, here is how you should connect it.

Then we have to download the library. You can download it from the Arduino IDE. Simply open Sketch >> Include Library >> Manage Libraries and then find LCD I2C.

The code we have to upload is somewhat similar to the first one at the difference that this time instead of displaying on the serial, we display the values on the LCD.


#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

//trig of ultrasonic sensor
int trig = 2;
//echo of ultrasonic sensor
int echo = 3; 
//Set the LCD address to 0x27 for 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
	// initialize the LCD
	lcd.begin();
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);

//Printing some text on startup
  lcd.setCursor(1, 0);
	lcd.print("Tutorial45.com");
  lcd.setCursor(4, 1);
  lcd.print("Presents");
  delay(2000);
}

void loop()
{
//that is the sequence for the sensor to start reading 
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

//here we got travel time of sonic wave
int duration = pulseIn(echo, HIGH);
//and here we calculate distance from it
int distance = (duration/2) / 29.1;
//before displaying new value on LCD we have to clear it
lcd.clear();
//here we display the value on the LCD
lcd.print(distance);
//delay 100 so we have around 10Hz of frequency of readings
delay(100);
}

And There you have it!

Very fun, simple and useful project. Hope you have become a better maker after reading this tutorial. Do not hesitate to show me what you have been able to build in the comment section below.

tchouken2

Related Posts