logo


Ultrasonic sensor HC-SR04 HCSR04 to world Ultrasonic Wave Detector Ranging Module HC SR04 HCSR04 Distance Sensor For Arduino


 

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

  • Measuring range: 2cm to 4m
  • Accuracy: 3mm
  • Operating voltage: 5V
  • Operating current: 20mA
  • Working frequency: 40KHz
  • Beam angle: 30 degrees
  • Dimensions: 45mm x 20mm x 15mm

Applications

The HC-SR04 sensor has a wide range of applications, including:

  • Robotics: Obstacle detection, navigation, and mapping
  • Industrial automation: Level measurement, presence detection, and collision avoidance
  • Security systems: Intrusion detection and perimeter protection
  • Automotive applications: Parking assistance, blind spot detection, and adaptive cruise control
  • Home appliances: Object detection for smart devices, such as vacuum cleaners and refrigerators

Interfacing with Arduino

The HC-SR04 sensor can be easily interfaced with Arduino using the following steps:

  1. Connect the VCC pin of the sensor to the 5V pin of the Arduino.
  2. Connect the GND pin of the sensor to the GND pin of the Arduino.
  3. Connect the Trig pin of the sensor to a digital output pin of the Arduino (e.g., pin 12).
  4. Connect the Echo pin of the sensor to a digital input pin of the Arduino (e.g., pin 11).

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.