HC-SR04 is a small wonder that can emit ultrasound and record the time required to echo. With Arduino UNO, the time can be converted to distance and displayed in serial monitor. The range of the gage is 2 to 450 cm and the resolution is 0.3 cm.
Let's make a distance meter using Arduino UNO and HC-SR04. We need 4 female to male jumper wires.
Connect the VCC of the sensor to 5V of UNO.
Connect the GNDs of the sensor and UNO together.
Connect TRIG of sensor to digital pin 13 of UNO.
Connect ECHO of sensor to digital pin 12 of UNO.
Connect the UNO board to the computer and upload the program through Arduino IDE.
//HC-SR04 Obstacle Detection
long distance;
void setup()
{
pinMode(13, OUTPUT);//trigger
pinMode(12,INPUT);//echo
Serial.begin(9600);//Sets the data rate in bits per second (baud) for serial data transmission.
}
void loop()
{
Sonar();
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
void Sonar()
{
digitalWrite(13,LOW);//clear trig
delayMicroseconds(2);
digitalWrite(13,HIGH);//trig
delayMicroseconds(10);
digitalWrite(13,LOW); //claer trig
long duration=pulseIn(12,HIGH);//read echo pulse in micro sec
distance=(duration/2)/29.2;
}
Go to Tools, then Serial Monitor.
We will see the sensor to object distance being measured continuously.
The frequency of the ultrasound generated by HC-SR04 is 40kHz which is beyond the human audible range of 20-20kHz.
Sound travels 343 meters per second in 20 degree C air. It takes only 29.2 micro seconds to travel 1 cm.
The sensor to object distance is half of the distance traveled by the sound wave.
Comments
Please log in or sign up to comment.