HC-SR04 Ultrasonic sensor

From ElectroDragon Wiki

Product Features:

Ultrasonic Ranging Module HC-SR04 provides 2cm-400cm non-contact distance sensing capabilities,Ranging accuracy up to 3mm; module comprises an ultrasonic transmitter, a receiver and a control circuit.
Basic operating principle:
(1) IO port TRIG trigger ranging to at least 10us high level signal;
(2) the module automatically sends eight 40khz square wave, automatically detects whether a signal return;
(3) a signal to return to a high output through the IO port ECHO high duration of ultrasound wave from the transmitter to the time of the return. Test distance = (high level time * sound velocity (340M / S)) / 2;

Connection

See the wiring below, VCC for 5V power supply, GND to ground, TRIG trigger control for Signal input, ECHO back Sound for signal output. Ultrasonic sensor..jpg

Frequency

Frequency..jpg
The above timing diagram that you only need to provide more than a 10uS pulse trigger signal, the module internally issued eight 40kHz cycle level and detect the echo. Once the echo wave signal is detected then output the echo signal. The pulse width of the echo signal is proportional to the measured distance. Thus transmit signals to the received echo signal The time interval can be calculated distance.
Formula: uS/58 = cm or uS/148 = inches; or: Distance = High level time * sound velocity (340M / S) / 2;
The recommended measurement period more than 60ms, so as to prevent the emission signal influence echo signal.


Note: This module should not be charged to connect to electrical connections, and then let the GND terminal to connect the module, otherwise it will affect the module working properly.
Ranging area of the object is not less than 0.5 square meters and the plane try to be smooth, otherwise the impact of measurement result

Demo Code - Use A0 and A1 for analog read

 
/* Define pins for HC-SR04 ultrasonic sensor */

#define echoPin A1 // Echo Pin = Analog Pin 0
#define trigPin A0 // Trigger Pin = Analog Pin 1

#define LEDPin 13 // Onboard LED

long duration; // Duration used to calculate distance
long HR_dist=0; // Calculated Distance

int minimumRange=5; //Minimum Sonar range
int maximumRange=200; //Maximum Sonar Range

/*--------------------SETUP()------------------------*/
void setup() {
 //Begin Serial communication using a 9600 baud rate
 Serial.begin (9600);
 
 //Setup the trigger and Echo pins of the HC-SR04 sensor
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

/*----------------------LOOP()--------------------------*/
void loop() {
 getDistance();
}

/*--------------------getDistance() FUNCTION ---------------*/
void getDistance(){ 
 
 /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 HR_dist = duration/58.2;
 
 /*Send the reading from the ultrasonic sensor to the computer */
 if (HR_dist >= maximumRange || HR_dist <= minimumRange){
 /* Send a 0 to computer and Turn LED ON to indicate "out of range" */
 Serial.println("0");
 digitalWrite(LEDPin, HIGH); 
 } else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(HR_dist);
 digitalWrite(LEDPin, LOW);
 }
}