How to measure distance using Arduino?
In this post we will look at measuring distances using an Ultrasonic sensor with the Arduino. For this purpose, I have chosen a commonly available sensor module called the HC-SR04 Ultrasonic Ranging Module. There are other Ultrasonic sensor as well, that give better field performance. However, we will start off with the HC-SR04. Given below is a picture of the HC-SR04 module. The HC-SR04 is an ultrasonic ranging module capable of measuring distances using Ultrasonic sound waves and it can be interfaced with not only the Arduino, but also various micro-controllers.
In one of my future posts, I will explain how we can hook up the HC0SR04 with an ESP8266 Wifi Module and make it behave like a local web server, thereby reducing the costs of having a costly edge device for data acquisition.
Figure 1 - The HC-SR04 Ultasonic Module |
There are various applications that may be developed using this Ultrasonic module with the Arduino. In these series of posts, we will analyze this module with the Arduino from various angles: Basic programming, Specifications Testing and then look at some Automation Solutions. For this post we will only look at basic distance measurement.
Basic Programming and Interfacing
The basic programming is straight forward and has two steps: setting up the circuit and writing the sketch/program. Build the circuit as shown below. This is a schematic provided to help you in wiring up.
Figure 1 - Wiring Schematic Diagram |
Connect the PINS as described below:
- Connect Vcc (on HC-SR04) to 5V (on Arduino)
- Connect Gnd (on HC-SR04) to Gnd (on Arduino)
- Connect Trig (on HC-SR04) to Pin 8 (on Arduino)
- Connect Echo (on HC-SR04) to Pin 12 (on Arduino)
Next use the following sketch. Compile and load the following into your Arduino.
In order to test the system, keep your Arduino plugged in with your computer, and open your Arduino IDE's Serial Monitor window (Tools > Serial Monitor) - the serial monitor will show you the output from the print() and println() function calls in the running sketch. The running sketch will keep reading the sensor data, do some calculations and print the distance of the object in front of the sensor at regular intervals. Place/move some objects in front of the sensor and keep watching the serial monitor window.
See for yourself what gets printed on the Serial Monitor window, as you move the object(s) away from the sensor and bring them near the sensor. I will be eager to read and learn from your experiences, so please put your comments below.
In the next post we will see how to detect an obstacle and react using this Ultrasonic module.
Cheers,
KBRC
No comments:
Post a Comment