Friday, September 2, 2016

Get to know about Ultrasonic Sensor HC-SR04


Ultrasonic Sensor HC-SR04 is used to measure distance. We can find precise distance to an object from sensor, or can used to detect when something is within range of the sensor. It can measure distance with high accuracy from 2cm to 400 cm. Its operation is not affected by sunlight or black material .Because it uses sound to measure distance.

Ultrasonic range finder measure distance by emitting a pulse of ultrasonic sound that travels through the air until it hits an object. It measure how long it takes the sound pulse to travel in its round trip journey back to the sensor when the pulse hits the object. Then it sends an electrical signal to the Arduino with information about how long it took for the sonic pulse to travel.

Ultrasonic range finder has 2 transducers.

  •         Transmitting transducer
  •     Receiving transducer



Transducers convert mechanical forces into electrical signals. The transmitting transducer converts an electrical signal into the ultrasonic pulse using MAX3232 IC. Receiving transducer converts the ultrasonic pulse into an electrical signal using LM324 IC. The length of this high voltage signal is equal to the total time of pulses take to travel from the transmitting transducer.




Ultrasonic sensor has four pins.

  • ·        VCC:  To supply +5VDC current
  • ·        Trig : Trigger - input
  • ·        Echo: Echo -output
  • ·        GND: to supply ground


Mechanism


First micro controller board sends a 5V high signal to the Trig pin for at least 10 µs. When the module receives this signal, it will emit 8 pulses of ultrasonic sound at a frequency of 40 KHz from the transmitting transducer. When this transmitting signal hits on object which is in the range, it reflects the signal back to the receiving transducer.When the pulse hits the receiving transducer, the Echo pin outputs a high voltage signal. The length of this high voltage signal is equal to the how long it takes the sound pulse to travel in its round trip journey back to the sensor when the pulse hits the object.


Source Code to measure distance between an object and the sensor 


int echoPin = 2;  //Define Echo pin
int trigPin = 3;     // Define Trig pin
long  inches, duration, cm;   //Define variables

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);  //Define output pin
pinMode(echoPin, INPUT);  //Define input pin
}
void loop()
{
//Give a short low pulse  to trigPin for ensure a clean high pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(5);

//give high pulse to trigPin 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);


// Read the signal from the sensor
duration = pulseIn(echoPin, HIGH);

// convert time into a distance
// The speed of sound is 29.1 microseconds per centimeter.
cm = (duration/2) / 29.1;

//Print distance
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}


You can see the distance using serial monitor in the Arduino IDE.
Also you can code this easily using  the NewPing library. (#include <NewPing.h>)




If you enjoyed this post,  spread by sharing it on social media. Thank you!



1 comment:

How to send Slack notification using a Python script?

 In this article,  I am focussing on sending Slack notifications periodically based on the records in the database. Suppose we have to monit...