Arduino Project – The distance calculator
I have been playing with Arduino (micro-controller) for about 2 years now and I am becoming more and more passionate about circuits. For those who are not aware of Arduino, check out my article “Arduino – A Guide for Beginners“. Recently, I was testing some new components that I ordered, which included an ultrasonic sensor and an LCD. I found an article online that give an explanation on how to use the ultrasonic sensor (HC-SR04) with the arduino to find a certain distance. I decided to add something new to the Arduino project to make it my own. Therefore, what I did was I added an LCD display that would show the distance between the ultrasonic sensor and the object. The coding was really simple because most of it was used online in other Arduino projects and I just had to modify it a little. I call it the Arduino Distance Calculator.
How it Works?
Basically, the main components used in this Arduino Project are ultrasonic sensor, LCD display and of course the Arduino to program and control the circuit. The ultrasonic sensor uses echo to measure the time it takes for the echo to bounce back to the sensor after hitting the target. The distance between the sensor and the object is then found by just doing some basic calculations to convert the time and speed of sound. Watch the video below to see a quick demo of the finished project.
Components Used:
(1) Arduino Nano (Arduino UNO will also work)
(2) Ultrasonic sensor (HC-SR04)
(3) Liquid Crystal Display (16 x 2)
(4) One 220 ohm resistor
(5) One potentiometer
Note: Buying components separately will cost more money. It’s better to buy a kit if you are planing on playing with arduino. The links are just provided for you to know which components I used. You don’t necessarily have to buy them from Amazon.
Circuit Schematic
The circuit is really simple! There not much to explain, just connect all your component together as shown in the diagram below
Coding
After you’re done making the circuit, comes the fun part: coding! I love coding, especially when coding electronic components. The code is short and simple. So, go ahead and copy the code below and paste it in the Arduino software. (Try to understand the code! Follow me on Google+ to ask any questions)
NOTE: In order for the code to work, you need to have the Liquid Crystal Library in your Arduino’s Library folder. You can download the library from here: http://www.arduino.cc/en/Reference/LiquidCrystal/
Watch the video in the following link if you don’t know how to put libraries in the Arduino Folder.
https://www.youtube.com/watch?v=ALqgQ32tu3c
CODE
// Distance Calculator (ultrasonic version)
// Circuit also available at http://www.arduinocoder.blogspot.com
/*
Components Required:
(1) Liquid Crystal Display
(2) Ultrasonic Sensor (HC-SR04)
(3) 1 220 ohm resistor
(4) 1 potentiometer
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 7;
const int echoPin = 8;
void setup() {
lcd.begin(16, 2);
}
void loop() {
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
lcd.print(“Distance (cm):”);
lcd.setCursor(0, 1);
lcd.print(cm);
delay(100);
lcd.clear();
}
long microsecondsToCentimeters(long microseconds){
return microseconds / 29 / 2;
}
Uploading the Code
Now, all you have to do is connect your arduino to your PC and upload the code. Your Arduino Project is finished and you just made a distance calculator. The range of the sensor is about 300 cm – 500 cm, so you won’t be able to measure longer distances. Still, it’s is a pretty fun Arduino project to do! Do check out my personally blog http://arduinocoder.blogspot.com, where I will be doing most of these Arduino Projects.
For more detail: Arduino Project # 1 – Make an Ultrasonic Distance Calculator