Ultrasonic Sensor
What you'll build¶
Measure the distance to an object using sound pulses and scroll it on the display.
Wiring¶
Pins 13 and 15 are only reachable through an edge connector breakout board. Pick the tab for your module. The HC-SR04P runs on 3 V; the classic HC-SR04 needs 5 V (check the label or the seller's listing).
| Part | Part pin | micro:bit pin |
|---|---|---|
| HC-SR04P (3 V) | Trig | P13 |
| HC-SR04P (3 V) | Echo | P15 |
| HC-SR04P (3 V) | VCC | 3V |
| HC-SR04P (3 V) | GND | GND |
Never connect a 5 V Echo straight to the micro:bit
A 5 V HC-SR04 sends 5 V out of Echo, but micro:bit pins only tolerate about 3.6 V. Wired directly, it can damage the micro:bit. Two resistors, a voltage divider, bring the signal down to 5 V × 2 kΩ / (1 kΩ + 2 kΩ) ≈ 3.3 V. No 2 kΩ resistor? Use two 1 kΩ in series, or 2.2 kΩ (≈ 3.4 V).
The micro:bit has no 5 V pin, so power the sensor from a separate 5 V supply, such as a breakout board with a 5 V rail, or a USB power bank through a USB breakout. Connect the supply's GND to the micro:bit's GND: without a shared ground, the signals have nothing to be measured against. Trig can be wired directly, because 3 V is high enough for the sensor to read.
| Part | Part pin | micro:bit pin |
|---|---|---|
| HC-SR04 (5 V) | VCC | +5V of an external 5 V supply |
| HC-SR04 (5 V) | Trig | P13 |
| HC-SR04 (5 V) | Echo | P15 through 1 kΩ; a 2 kΩ resistor also links P15 to GND |
| HC-SR04 (5 V) | GND | GND |
Code¶
How it works¶
The sensor sends an ultrasonic ping from Trig and holds Echo high until the echo returns.
The library times that pulse with time_pulse_us and converts it to centimetres using the speed of sound.
Pass Ultrasonic(trig=..., echo=...) to use other pins.
If no echo returns (nothing within about 4 m, or a wiring problem), the reading is zero or negative,
so the example shows ? instead of a misleading number. Waiting for a missing echo takes up to one second.
Try this next¶
- Make a parking sensor that beeps faster as objects get closer.
- Show distance as a bar graph on the LED matrix.
- Combine with the servo to build a sweeping radar.