Ultrasonic distance sensor for Arduino, ESP32, Wemos, and Raspberry Pi projects, such as robot cars and parking assistance.
SKU: OT703-C86 Article number: AB043 EAN: 8720589817135 Location: C08.06
Alternative
Ultrasonic distance sensor flush-mount black SR04M-2 splash-proof
€ 11,95
View this alternative
How to connect the HC-SR04 ultrasonic distance sensor to an Arduino UNO or ESP32, with an example sketch you can upload straight away.
This project measures distance using the HC-SR04 ultrasonic sensor. An ultrasonic pulse is emitted and the return echo time is measured to calculate distance in centimeters, which is then printed to the Serial Monitor every 500 ms.
/*
* ==================================================================
* Generated by Codey.online — https://www.codey.online
* ==================================================================
* Project : HC-SR04 Ultrasonic Distance Sensor Demo
* Board : Arduino UNO (arduino:avr:uno)
* Parts : HC-SR04
* Libraries : none (built-in)
*
* Codey Online is an AI-powered browser IDE for Arduino and ESP32.
* Describe your project and Codey writes the code, draws the wiring
* diagram and uploads it to your board, straight from the browser.
* This code is free to use, modify and share, without warranty.
* ==================================================================
*/
// HC-SR04 Ultrasonic Distance Sensor Demo
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
Serial.println(F("HC-SR04 Ultrasonic Sensor Ready"));
}
void loop() {
// Trigger a 10-microsecond high pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the incoming echo pulse (timeout ~30ms / ~5m)
unsigned long duration = pulseIn(echoPin, HIGH, 30000UL);
if (duration == 0) {
Serial.println(F("Out of range"));
} else {
// Speed of sound is ~343 m/s -> ~29.15 us per cm (round-trip 58.3 us/cm)
float distanceCm = duration / 58.2;
Serial.print(F("Distance: "));
Serial.print(distanceCm, 1);
Serial.println(F(" cm"));
}
delay(500);
}
Wiring diagram and example code generated by Codey.online — https://www.codey.online
This project measures distance using an HC-SR04 ultrasonic distance sensor. Because the HC-SR04 runs on 5V and outputs a 5V Echo pulse, a simple 1kΩ / 2kΩ resistor voltage divider safely scales the Echo signal down to ~3.3V for the ESP32. Open the Serial Monitor at 115200 baud to view real-time distance measurements in centimeters.
/*
* ==================================================================
* Generated by Codey.online — https://www.codey.online
* ==================================================================
* Project : HC-SR04 Ultrasonic Distance Sensor with ESP32
* Board : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
* Parts : HC-SR04
* Libraries : none (built-in)
*
* Codey Online is an AI-powered browser IDE for Arduino and ESP32.
* Describe your project and Codey writes the code, draws the wiring
* diagram and uploads it to your board, straight from the browser.
* This code is free to use, modify and share, without warranty.
* ==================================================================
*/
// HC-SR04 Ultrasonic Distance Sensor with ESP32
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("HC-SR04 Distance Sensor Initialized");
}
void loop() {
// Send a 10-microsecond HIGH pulse to trigger the sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the round-trip echo pulse duration in microseconds
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout (~5m range)
if (duration == 0) {
Serial.println("Out of range / no echo received");
} else {
// Speed of sound is ~0.0343 cm/us (divide by 2 for one-way distance)
float distanceCm = (duration * 0.0343) / 2.0;
Serial.print("Distance: ");
Serial.print(distanceCm, 1);
Serial.println(" cm");
}
delay(500);
}
Wiring diagram and example code generated by Codey.online — https://www.codey.online
This sensor is ideal for making a robot car that needs to keep track of the distance to objects. Or you can make your own parking aid with it. Unlimited possibilities with this ultrasonic distance sensor. Ideal for your Arduino / ESP32 / Wemos or Raspberry Pi project.
View the Wiring diagram and example code >>>
Sensor angle: <15o
Dimensions: 45x20mm
Resolution: 3mm
Detection range: 2-450cm
Voltage: 5V
Current: <2mA
The HC-SR04 ultrasonic distance sensor is a device that uses ultrasonic waves to measure distance. The sensor consists of a transmitter and a receiver connected to a microcontroller, such as an Arduino.
The sensor works by having the transmitter emit an ultrasonic pulse, which is then reflected by objects in the environment. The receiver then detects the reflection and sends the time back to the microcontroller, which can then calculate the distance to the object.
The HC-SR04 ultrasonic distance sensor is suitable for measuring distances up to 3 meters with an accuracy of about 3 mm. The sensor is easy to use with an Arduino microcontroller and is suitable for a wide range of applications, such as making automatic parking systems or detecting obstacles in a certain environment.
The sensor has a small size and low power consumption, making it suitable for projects where space and power management are important. The sensor is also easy to install thanks to the four holes on the back, which make it easy to attach the sensor to a surface.
All in all, the HC-SR04 ultrasonic distance sensor is a practical and versatile sensor that can be an excellent addition to any Arduino project.
Dimensions
Length: 4.55cm
Width: 2.05cm
Height: 1.53cm
| Width | 2.05 cm |
|---|---|
| Resolution | 3 mm |
| Length | 4.55 cm |
| Height | 1.53 cm |
seems to be good
great thing.
HC-SR04 ultrasonic distance sensor