Ultrasonic distance HC-SR04 with TinkerPie
Bat-style sonar. Use the 3.3 V HC-SR04P, or a divider on ECHO for the 5 V version.
| Type | Timing sensors |
|---|---|
| Connect to | TRIG and ECHO on two pins (or one pin for 3-pin modules) |
| Gives | cm (2-400), -1 = no echo |
| Simulator | Yes: a control appears under the simulated board |
How it works
The HC-SR04 sends a short burst of ultrasound (40 kHz, too high for us to hear) and listens for the echo, like a bat. Sound travels about 343 metres per second, so the time until the echo returns tells the distance.
How to wire it
One or two signal pins on S1-S4 or P10-P13. Studio reads them with precise timing in the board firmware.
Code
In blocks: Accessories category in TinkerPie Studio. As code:
// JavaScript
sensors.ultrasonicCm(Pin.S1, Pin.S2)
// Arduino C++
sensors::ultrasonicCm(Pin::S1, Pin::S2) Good to know
- Range about 2-400 cm in a cone of about 15°.
- Soft, fuzzy or angled objects return weak echoes.
- Use the 3.3 V version (HC-SR04P), or a voltage divider on ECHO for the 5 V version.
Projects with the Ultrasonic distance HC-SR04
Ultrasonic rulerMeasures how far your hand is, in centimetres, with an HC-SR04 sonar.Sensor projectsSmart dustbin lidWave over the bin: a servo opens the lid for two seconds, then closes it.Sensor projects
Similar modules
More projects to try
Questions
Can I try the Ultrasonic distance HC-SR04 without buying one?
Yes. In TinkerPie Studio the simulator shows a control for it under the board, so you can test your program before you build it.
Why does my distance sometimes show -1?
-1 means no echo came back: the object is too far, too small, soft or at an angle. Point the sensor straight at a flat, hard surface.