Welcome to Day 04 of the Arduino Explorer: 30 Days, 30 Sensors journey! Today, we will be exploring the Ultrasonic Sensor (HC-SR04). This sensor is widely used in robotics, obstacle detection, and range finding. It allows us to measure the distance to an object using ultrasonic sound waves.
-
Sensor: Ultrasonic HC-SR04
-
Purpose: To measure the distance to objects
-
Difficulty: Beginner Friendly
-
Topics Covered:
- Working principle of ultrasonic sensors
- Accurate distance measurement in centimeters
- Displaying sensor output via Serial Monitor
Day04_Ultrasonic_Sensor/
βββ ultrasonic_sensor.ino
βββ Code_Explanation.md
βββ requirements.md
βββ README.md
βββ Schematic View.pdf
βββ wiring_diagram.png
βββ Screeshot 1.png
βββ Tinkercad_Simulation_Guide.md
| Component | Quantity |
|---|---|
| Arduino Uno/Nano | 1 |
| Ultrasonic Sensor HC-SR04 | 1 |
| Jumper Wires | 4 |
| Breadboard | 1 |
| USB Cable | 1 |
| HC-SR04 Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| TRIG | Digital 9 |
| ECHO | Digital 10 |
β οΈ Make sure to connect the Echo pin to a digital pin that supportspulseIn().
The sensor sends out an ultrasonic pulse and waits for it to bounce back from an object. The time taken for the echo to return is used to calculate the distance.
Formula:
Distance (cm) = Duration Γ 0.0343 / 2
- Fundamentals of ultrasonic wave-based distance measurement
- Interfacing HC-SR04 with Arduino
- Using
digitalWrite(),pulseIn(), and Serial Monitor - How to avoid common pitfalls like noise and misreads
- Open the
ultrasonic_sensor.inofile in the Arduino IDE. - Connect your Arduino board.
- Upload the sketch.
- Set baud rate to
9600 - Observe real-time distance measurements in cm
- No Output: Check VCC and GND wiring
- Always 0 cm: Reverse ECHO/TRIG pins
- Fluctuating Readings: Add a delay and avoid noisy environments
Use this project even if you don't have physical components!
-
Visit Tinkercad Circuits
-
Create new circuit
-
Add:
- Arduino Uno
- HC-SR04 sensor
- Connect as per the above table
-
Copy-paste the
ultrasonic_sensor.inocode -
Start Simulation & open Serial Monitor
More help: See Tinkercad_Simulation_Guide.md
- Arduino IDE
- Libraries: None (built-in functions only)
Check requirements.md for details.
- Real-time distance readings on Serial Monitor (in cm)
- Accuracy: Β±3 mm typically
We welcome suggestions and improvements! If you find a bug or have a feature request, feel free to open an issue or PR.
Happy Building! π
β Arduino Explorer Team