Ultrasonic ranging module HC-SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules include ultrasonic transmitters, receiver and control circuit. If you are sourcing for an ultrasonic ranging module, the HC-SR04 is a good choose. Its stable performance and high ranging accuracy make it a popular module in electronic market. Compared to the Shap IR ranging module, HC-SR04 is more inexpensive than it. But it has the same ranging accuracy and longer ranging distance.
The basic principle of work:
- Using IO trigger for at least 10us high-level signal,
- The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
- IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning. Test distance = (high level time×velocity of sound (340M/S) /2
Specifications:
There are 4 pins out of the module : VCC , Trig, Echo, GND. So it's a very easy interface for controller is to use it ranging. The process is: pull the Trig pin to high-level for more than 10us impulse , the module will start ranging ; finish ranging , If you find an object in front , Echo pin will be high level , and based on the different distance,it will take the different duration of high-level. So we can calculate the distance easily :
The ultrasonic sensor uses sonar to determine the distance to an object. Here’s what happens:
The time between the transmission and reception of the signal allows us to know the distance to an object. This is possible because we know the sound’s velocity in the air.
Pins
This sensor is really cool and popular among the Arduino tinkerers. So, here we provide an example on how to use the HC-SR04 ultrasonic sensor with the Arduino. In this project the ultrasonic sensor reads and writes the distance to an object in the serial monitor.
The goal of this project is to help you understand how this sensor works. Then, you can use this example in your own projects.
Follow the next schematic diagram to wire the HC-SR04 ultrasonic sensor to the Arduino.
The following table shows the connections you need to make:
Ultrasonic Sensor HC-SR04 | Arduino |
VCC | 5V |
Trig | Pin 11 |
Echo | Pin 12 |
GND | GND |
/* * created by Rui Santos, http://randomnerdtutorials.com * * Complete Guide for Ultrasonic Sensor HC-SR04 * Ultrasonic sensor Pins: VCC: +5VDC Trig : Trigger (INPUT) - Pin11 Echo: Echo (OUTPUT) - Pin 12 GND: GND */ int trigPin = 11; //Trig - green Jumper int echoPin = 12; //Echo - yellow Jumper long duration, cm, inches; void setup() { //Serial Port begin Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); // convert the time into a distance cm = (duration/2) / 29.1; inches = (duration/2) / 74; Serial.print(inches); Serial.print(''in, ''); Serial.print(cm); Serial.print(''cm''); Serial.println(); delay(250); }