
(Adapted from: https://www.hackster.io/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380)
After completing the circuit connection, we shall look at the source code on getting the reading of the HC-SR04 Ultrasonic Sensor.
First, we define the pins that Trig and Echo are connected to.
const int trigPin = 12;
const int echoPin = 13;
Then we declare long duration and int distance, which will hold the length of the sound wave and how far away the object is.
long duration;
int distance;
Next, in the setup, we declare the Trig pin as an output, the Echo pin as an input, and start Serial communications.
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200);
}
Now, in the loop, what we do is first set the trigPin low for 2 microseconds just to make sure that the pin in low first. Then, we set it high for 10 microseconds, which sends out an 8 cycle sonic burst from the transmitter, which then bounces of an object and hits the receiver (which is connected to the Echo Pin).
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
When the sound waves hit the receiver, it turns the Echo pin high for however long the waves were traveling for. To get that, we can use a handy Arduino function called pulseIn(). It takes 2 arguments, the pin you are listening to (in our case, the Echo pin), and a state (HIGH or LOW). What the function does is waits for the pin to go whichever state you put in, starts timing, and then stops timing when it switches to the other state. In our case we would put HIGH since we want to start timing when the Echo pin goes high. We will store the time in the duration variable. (It returns the time in microseconds)
duration = pulseIn(echoPin, HIGH);
Now that we have the Time, we can use the equation
Speed = Distance/Time ,
but we will make it Distance = Speed x Time
because we have the speed. What speed do we have? The speed of sound, of course! The speed of sound is approximately 340 meters per second, but since the pulseIn() function returns the time in microseconds, we will need to have a speed in microseconds also, which is easy to get. A quick Google search for "speed of sound in centimeters per microsecond" will say that it is 0.0343 c/μS. You could do the math, but searching it is easier. Anyway, with that information, we can calculate the distance! Just multiply the duration by 0.0343 and the divide it by 2 (because the sound waves travel to the object and back). We will store that in the distance variable.
distance = (duration*.0343)/2;
The rest is just printing out the results to the Serial Monitor.
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
Test the code... Start the Arduino IDE up.
Then create a new sketch and paste the code below in the Arduino IDE followed by upload:
/*
* Ultrasonic Sensor HC-SR04 and ESP8266
*/
// defines pins numbers
const int trigPin = 12;
const int echoPin = 13;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance (in cm) based on the speed of sound
distance = duration*0.0343/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}