About

The HCSR04 is a type of ultrasonic sensor, which is used to measure distances or detect objects. It is a low-cost, easy-to-use sensor that can be used in a wide range of applications, such as distance measurement, object detection, and level sensing.

The HCSR04 ultrasonic sensor consists of two main parts: a transmitter and a receiver. The transmitter sends out a high-frequency sound wave, and the receiver listens for the sound wave to be reflected back. By measuring the time it takes for the sound wave to be reflected back, the sensor can calculate the distance to the object.

The HCSR04 ultrasonic sensor has four pins: Vcc, Trig, Echo, and GND. The Vcc and GND pins are used to power the sensor with a voltage between +5V and +5.5V. The Trig pin is used to trigger the transmitter to send out a sound wave, and the Echo pin is used to receive the reflected sound wave.

To use the HCSR04 ultrasonic sensor, you will need to send a trigger signal to the Trig pin, and then measure the time it takes for the Echo pin to receive the reflected sound wave. You can then use this time to calculate the distance to the object. The sensor has a range of 2cm to 400cm and an accuracy of 3mm.

The HCSR04 sensor is widely supported by microcontroller platforms like Arduino, Raspberry Pi, and other boards. They can be easily integrated into a wide range of projects and are

How HCSR04 ultrasonic sensor works?

The HCSR04 ultrasonic sensor works by emitting a high frequency sound wave (ultrasonic sound) and then measuring the time it takes for the sound wave to be reflected back to the sensor. The sensor uses the time delay between the emitted and received sound waves to calculate the distance to an object.

The sensor has two main components: a transmitter and a receiver. The transmitter, which is typically a piezoelectric crystal, generates a high-frequency sound wave when a trigger signal is applied to the Trig pin. The sound wave travels through the air and bounces off any nearby objects.

The receiver, which is typically a microphone, listens for the reflected sound wave to be received back. The time delay between the trigger signal and the received signal is used to calculate the distance to the object. The distance is calculated using the speed of sound and the time delay, using the formula: distance = (time delay x speed of sound) / 2

The HCSR04 sensor has a range of 2cm to 400cm and an accuracy of 3mm. It can be used in a wide range of applications, such as distance measurement, object detection, and level sensing.

It is also important to keep in mind that the sensor can be affected by the surrounding temperature, humidity, sound waves, and other elements in the environment, which may cause errors in the distance measurement.


How to integrate HCSR04 ultrasonic sensor with Arduino Uno?

Integrating the HCSR04 ultrasonic sensor with an Arduino Uno is a simple process, here are the steps to follow:

  1. Gather the necessary tools and materials: You will need an HCSR04 ultrasonic sensor, an Arduino Uno board, a breadboard, jumper wires, and a USB cable.
  2. Connect the HCSR04 to the Arduino Uno: Connect the Vcc and GND pins of the HCSR04 to the 5V and GND pins of the Arduino Uno, respectively. Connect the Trig pin of the HCSR04 to digital pin 8 of the Arduino Uno, and the Echo pin of the HCSR04 to digital pin 7 of the Arduino Uno.
  3. Write the code: In the Arduino IDE, write the following code to read the distance from the sensor:

Basic Circuit Diagram with Arduino UNO


Basic Code for Arduino UNO

const int trigPin = 2;

const int echoPin = 3;

void setup() {

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  Serial.begin(9600);

}

void loop() {

  long duration, distance;

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration/2) / 29.1;

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");

  delay(1000);

}

5. Upload the code to the Arduino Uno: Connect the Arduino Uno to your computer using the USB cable, select the correct board and serial port in the Arduino IDE, and then click the Upload button to upload the code to the Arduino Uno.

6. Measure distance: The distance in cm will be printed on the serial monitor.

It's important to keep in mind that this is a basic example and you might need to adjust the code depending on the specific requirements of your project. Also, the sensor might have a different range, and accuracy, and might be affected by the surrounding temperature, humidity, sound waves, and other elements in the environment, which may cause errors in the distance measurement.