HC-SR04 ultrasonic sensor with Arduino, how does it work?

HC-SR04 ultrasonic sensor and Arduino

What is an ultrasonic sensor?

An ultrasonic sensor is a device that uses ultrasonic waves to measure distances without contact. It emits sound pulses, calculates the time it takes to bounce back, and returns that information, allowing precise measurement of the distance between the sensor and an object.

HC-SR04 Ultrasonic Sensor Applications

The HC-SR04 ultrasonic sensor is essential in robotics projects for obstacle detection, allowing robots to navigate autonomously. Additionally, its ability to measure distances accurately makes it valuable in projects that require information about the location of objects.

In more practical applications, such as automatic parking systems in cars, and in the field of home automation, where devices are activated based on proximity, the HC-SR04 demonstrates its versatility and contributes to the creation of smarter and more efficient environments.

HC-SR04 Sensor Technical Specifications

The HC-SR04 operates at a voltage of 5V DC, a working frequency of 40 kHz and has a measurement range of 2 cm to 400 cm, with a resolution of 0.3 cm and an accuracy of +/- 0.5 cm. Its four crucial pins for operation with development boards like Arduino are VCC (5V), Trig (Trigger), Echo and GND (Ground).

VCC: is connected to the positive power of the board (5V).
Trig (Trigger): emits ultrasonic pulses and connects to a digital pin on the board.
Echo: receives the echoes of the ultrasonic pulses and connects to a digital pin on the board.
GND: is connected to the negative power supply of the board (GND).

These specifications are common for most HC-SR04 models on the market. However, it is always advisable to check the manufacturer’s specific data sheet for accurate information on the model in question.

How does the HC-SR04 ultrasonic sensor work

The sensor operates by emitting ultrasonic waves through its transmitter. It starts the process by receiving a signal of at least 10 microseconds through its Trig terminal. Simultaneously, the sensor enables the Echo terminal and verifies the presence of a return signal. If it detects the return, it emits a HIGH signal through the Echo terminal, the duration of which is proportional to the time it takes for the wave to travel from the transmitter to the receiver.

When integrated with an Arduino, the latter sends the signal and uses the pulseIn() function to measure the duration of the return pulse, facilitating the precise measurement of the round trip time of the ultrasonic waves and allowing the distance to be calculated.

How to connect a HC-SR04 sensor to Arduino

To connect the HC-SR04 sensor to Arduino, first, connect the VCC pin of the sensor to the 5V pin of Arduino and the GND pin of the sensor to the GND pin of Arduino. Then, connect the Trig pin of the sensor to digital pin 8 of the Arduino and the Echo pin to digital pin 9.

Finally, connect your Arduino to the PC using the USB cable to load the sketch that allows distance measurement using the ultrasonic sensor.

Code to use the HC-SR04 ultrasonic sensor with Arduino

int trig=9;
int echo=10;
float distance;
float duration;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
pinMode(echo, INPUT);
delay(3000);
}
void loop() {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration=pulseIn(echo, HIGH);
distance=duration/58;
Serial.print("Distancia: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top