Measure distance with ultrasound
Trigger the HC-SR04 on D7 and time ECHO on D6. Convert the round-trip time to centimetres and print cm at 115200 baud.
Trigger the HC-SR04 on D7 and time ECHO on D6. Convert the round-trip time to centimetres and print cm at 115200 baud.
Use the Uno R3 Learning Kit. Build with power disconnected, check the labelled connections, then upload the supplied sketch. Drag or zoom the 3D model to inspect it from any angle.
Also needed
Use suitable female-ended jumpers on the sensor header. The included female-to-female leads can extend male breadboard jumpers.
Step 1 — Watch the complete build
Press Play for the narrated build. You can rotate and zoom the model while it plays, or follow the individual steps below.
Loading assembly…
Keep U S B disconnected while you build. On this breadboard, each group of five holes shares a connection; the centre channel separates the two halves. You can drag the 3D view to look around. Connect the Arduino’s five volt output to the positive rail. Connect Arduino ground to the ground rail. Place the HC-SR04 with its two round transducers facing the target. Identify the four pins using the printed labels. Connect the distance sensor’s supply pin to the positive rail. Connect the distance sensor’s ground pin to the ground rail. Connect Arduino digital pin 7 to the distance sensor’s trigger pin. Connect the distance sensor’s ECHO pin to Arduino digital pin 6. Check all wiring. Connect the square U S B-B plug to the Uno and the U S B-A plug to your computer. Use Verify and Upload in the code editor below.
Step 2 — Understand the breadboard
Keep USB disconnected while you build. On this breadboard, each group of five holes shares a connection; the centre channel separates the two halves. You can drag the 3D view to look around.
Loading assembly…
Keep U S B disconnected while you build. On this breadboard, each group of five holes shares a connection; the centre channel separates the two halves. You can drag the 3D view to look around.
Step 3 — Connect Uno 5V → T+ rail 3
Connect the Arduino’s five volt output to the positive rail.
| Connect | To |
|---|---|
| Uno 5V | T+ rail 3 |
Loading assembly…
Connect the Arduino’s five volt output to the positive rail.
Step 4 — Connect Uno GND → T- rail 1
Connect Arduino ground to the ground rail.
| Connect | To |
|---|---|
| Uno GND | T- rail 1 |
Loading assembly…
Connect Arduino ground to the ground rail.
Step 5 — Place the distance sensor
Place the HC-SR04 with its two round transducers facing the target. Identify the four pins using the printed labels.
Loading assembly…
Place the HC-SR04 with its two round transducers facing the target. Identify the four pins using the printed labels.
Step 6 — Connect HC-SR04 ultrasonic distance sensor VCC → T+ rail 14
Connect the distance sensor’s supply pin to the positive rail.
| Connect | To |
|---|---|
| HC-SR04 ultrasonic distance sensor VCC | T+ rail 14 |
Loading assembly…
Connect the distance sensor’s supply pin to the positive rail.
Step 7 — Connect HC-SR04 ultrasonic distance sensor GND → T- rail 17
Connect the distance sensor’s ground pin to the ground rail.
| Connect | To |
|---|---|
| HC-SR04 ultrasonic distance sensor GND | T- rail 17 |
Loading assembly…
Connect the distance sensor’s ground pin to the ground rail.
Step 8 — Connect Uno D7 → HC-SR04 ultrasonic distance sensor TRIG
Connect Arduino digital pin 7 to the distance sensor’s TRIG pin.
| Connect | To |
|---|---|
| Uno D7 | HC-SR04 ultrasonic distance sensor TRIG |
Loading assembly…
Connect Arduino digital pin 7 to the distance sensor’s trigger pin.
Step 9 — Connect HC-SR04 ultrasonic distance sensor ECHO → Uno D6 PWM
Connect the distance sensor’s ECHO pin to Arduino digital pin 6.
| Connect | To |
|---|---|
| HC-SR04 ultrasonic distance sensor ECHO | Uno D6 PWM |
Loading assembly…
Connect the distance sensor’s ECHO pin to Arduino digital pin 6.
Step 10 — Place USB-A to USB-B cable · Uno
Check all wiring. Connect the square USB-B plug to the Uno and the USB-A plug to your computer. Use Verify and Upload in the code editor below.
Loading assembly…
Check all wiring. Connect the square U S B-B plug to the Uno and the U S B-A plug to your computer. Use Verify and Upload in the code editor below.
Step 11 — Program your Uno
Choose Arduino Uno. Verify, pair the board and Upload this sketch. Keep USB connected. Trigger the HC-SR04 on D7 and time ECHO on D6. Convert the round-trip time to centimetres and print cm at 115200 baud.
Step 12 — Explore the live readings
Move a flat object in front of the transducers. Leave time between readings. Very close, soft or angled targets may not return a valid echo.
Step 13 — Troubleshooting
No echo is not zero centimetres. Check TRIG and ECHO, ground continuity and the object’s angle.
Parts List
Required Parts (1)
Use the components named in this tutorial. A computer and any additional power supply are not included.
x1
$64.70
In stockProject Summary
1 part total
Unavailable items will be skipped