TINKER PiEDESIGN IN INDIA

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.

TypeTiming sensors
Connect toTRIG and ECHO on two pins (or one pin for 3-pin modules)
Givescm (2-400), -1 = no echo
SimulatorYes: 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

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.