HOW TO USE HC-SR04 WITH ARDUINO
Sunday, April 25, 2021
Hello. In this article, you will find answers to your calls such as arduino hc-sr04, arduino ultrasonic sensor, arduino hc-sr04 distance measuring, arduino hc-sr04 example code.
In order for our ears to perceive any sound, that sound must have at least 20 (20 Hz) vibrations per second, and a maximum of 20,000 (20 Khz) vibrations. Ultrasonic sensors, on the other hand, produce sound waves higher than these values when the correct encoding is performed and they start to listen to the reflected waves. HC-SR04 ultrasonic distance sensor also operates at a frequency of 40Khz and the distances of objects between 2cm and 400cm can be measured when correct coding is achieved by creating accurate results from the data obtained from the sensor.
HC-SR04 ultrasonic sensor has trigger, echo, VCC and GND pins. In order for the sensor to generate sound waves, it is necessary to trigger the trigger pin for 10us (microseconds). When the trigger pin is triggered sufficiently, our sensor generates 8 sound pulse. This sound wave that is produced travels at the speed of sound and when an obstacle arises, it hits it and comes back to the sensor at the same speed. When our sensor, which starts to listen after sending the sound wave, catches the first beat it creates, when we calculate based on the time that that stroke was generated and the time it captured, the distance between the sensor and the obstacle in front of it is measured correctly.
This sound wave produced travels at a speed of 340 m/sec, i.e. 0.034 cm/us under normal conditions. Considering that there is an object 20 cm away from our sensor, (time = distance / speed) => (time = 20 / 0.034 = 588us.). But this result is the time the sound travels 20cm. The time it takes until the created wave leaves the sensor and reaches the obstacle. As the signal reflects from the object and comes back to the sensor, it will travel 20cm distance again, so 588us will have passed. Therefore, when the distance between the sensor and the obstacle is desired to be calculated, the elapsed time must be divided by two. If you want to calculate the distance with Arduino HC-SR04, the following formula should be used:
Distance = (time / 2) * 0.034
ARDUINO HC-SR04 DISTANCE MEASUREMENT PROJECT
Arduino HC-SR04 Materials
- Arduino
- HC-SR04 ultrasonic sensor
ARDUINO HC-SR04 CONNECTION
Since the HC-SR04 draws a maximum of 15mA, it can be fed with the 5V output on the arduino. The other pins of the sensor can be connected as follows.
ARDUINO HC-SR04 CODE
int trigPin = 10;
int echoPin = 9;
long passingTime, cm;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); // for a clean measurement
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); // trigger sensor
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // complete the triggering process
// listen to the incoming voice
passingTime = pulseIn(echoPin, HIGH); // get sound return time
// find distance from elapsed time
cm = (passingTime/2) *0.0343;
// Print the distance to the serial port screen
Serial.print("Distance: ");
Serial.print(cm);
Serial.print(" cm");
Serial.println();
delay(500);
}