Introduction
An Ultrasonic Sensor is a module that is used to find the distance between the nearest object and the module itself.
Figure 1 Ultrasonic Sensor
So, it has two heads where one sends the ultrasound signal and the other receives one. And its range is from 2cm to 3m (with nominal errors).
The HC-SR04 module sends an ultrasonic short pulse signal from the trigger and listens to the signal from the echo when it bounces back from the object.
Figure 2 HC-SR04
If it doesn't find an error then it will either a large value or a very short. While coding, you must take care of the range and some exceptions.
As a default condition, we make an exception when it can't find any object above 200cm.
Background: As you can see, there are three pins for Vcc, Trigger, Echo and GND. Vcc and GND is quite explanatory. And, Trigger sends the ultrasonic signal and Echo listens for that signal after returning.
And, its connection would be like:
Figure 3 Signal Socit
Every Ultrasonic module has four legs and two are pre-defined. And, those are Vcc (5volt) and GND. Apart from these two, we have Trigger and Echo. Trigger is always a Digital PIN whereas Echo can be a PWM or any Digital PIN.
As an add-on we have added a Red and a Green LED to notify whether it's in range or not. When the module detects nearby objects it then it will turn on Red else it'll constantly turn on Green.
Sketch
-
- int RED = 4;
- int GREEN = 8;
- int TRIGGER = 12;
- int ECHO = 11;
- int max_distance = 200;
- int min_distance = 0;
- int duration;
- int distance;
- void setup() {
- Serial.begin(9600);
- pinMode(TRIGGER, OUTPUT);
- pinMode(ECHO, INPUT);
- pinMode(RED, OUTPUT);
- pinMode(GREEN, OUTPUT);
- }
- void loop() {
- distance = 0;
- duration = 0;
- digitalWrite(TRIGGER, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGGER, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER, LOW);
- duration = pulseIn(ECHO, HIGH);
-
- distance = (duration / 2) / 29.1;
- digitalWrite(GREEN, HIGH);
- if (distance >= max_distance || distance <= min_distance) {
- Serial.println("# Out Of Rage");
- } else {
- Serial.print(distance);
- Serial.println(" cm");
- digitalWrite(RED, HIGH);
- digitalWrite(GREEN, LOW);
- delay(1000);
- digitalWrite(RED, LOW);
- }
- delay(1000);
- }
Before I explain its future perspective, you must understand the code.
First, we have declared the PINs. Since we have four pins in the module (one is Vcc and another is GND, then trigger and echo).
Then, there are another two PINs for the Red and Green LEDs.
Next, we assigned maximum and minimum distance values for the module. Factually, it's not necessary to do but, we keep a range until when it will respond. Then we have variables like duration and distance that we'll use in our further code.
Using pinMode() set all the PINs as per their Input/Output behavior.
Trigger is an output Lead since it sends the ultrasonic signal and Echo listens for that signal after getting a bounce back.
After successful completion, we calculate the duration first then the distance.
The following is how it will work.
- digitalWrite(TRIGGER, LOW);
- delayMicroseconds(2);
So, it is writing a LOW value to the Ultrasonic module. That means that the module is not sending any signal or LOW signal. After sending the LOW signal it will wait for 2 microseconds.
After waiting for 2 microseconds:
- digitalWrite(TRIGGER,HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER, LOW);
Write a HIGH value, in other words send an ultrasonic signal and wait for 10 microseconds. Then make it low.
What that actually means is that it sends a signal using the trigger PIN and then makes it low after 10 microseconds.
And now we'll try to listen to that signal that we have sent and that can be done with the ECHO pin.
- duration= pulseIn(ECHO, HIGH);
And it returns the time-duration. Depending on the duration value, we'll calculate the distance in centimeters or in inches.
-
- distance= (duration/2)/29.1;
Next, we will check whether it's in range or not.
- if( distance >=max_distance || distance <= min_distance)
- {
- Serial.println("# Out Of Rage");
- }
And if the object is in range then:
- Serial.print(distance);
- Serial.println(" cm");
- digitalWrite(RED, HIGH);
- digitalWrite(GREEN, LOW);
- delay(1000);
- digitalWrite(RED,LOW);
It will then show a value in the Serial Monitor and at the same time turn on the Red LED. Each time the loop() method will show the measured value.
Figure 4 Serial Monitor
Troubleshoot
If your serial monitor constantly shows Out Of Range then you must check the connection or provide some time to calculate the distance.
ConclusionThis module is one of the most-used modules in any physical rotatory or automated device. It assumes exact distance and processes accordingly.
You can use more than one module to judge more precisely. Until then, enjoy your work.