The HC-SR04 ultrasonic sensor is a low-cost, high-precision, non-contact distance measuring device. It is widely used in various applications such as robotics, obstacle detection, level measurement, and many more.
Working Principle
The HC-SR04 sensor works on the principle of ultrasonic waves. It consists of an ultrasonic transmitter and receiver. The transmitter emits a burst of ultrasonic waves, which travel through the air and reflect off the target object. The reflected waves are received by the receiver, which measures the time it takes for the waves to travel to and from the target.
Features
Applications
The HC-SR04 sensor has a wide range of applications, including:
Interfacing with Arduino
The HC-SR04 sensor can be easily interfaced with Arduino using the following steps:
Code
The following Arduino code can be used to measure the distance using the HC-SR04 sensor:
const int trigPin = 12;
const int echoPin = 11;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
delay(200);
}
The HC-SR04 ultrasonic sensor is a versatile and affordable device that can be used in a wide range of applications. It is easy to use and interface with Arduino, making it a popular choice for makers and hobbyists.