HC-SR04 ultrasonic sensor above a Circuit Playground Express board.

The HC-SR04 is an inexpensive distance sensor based on a pair of ultrasonic transducers with a straightforward TTL level interface and a claimed range of 2cm to 4m. Similar sensors are now commonplace with the widespread use of car parking assistance sensors.

The sensor's 5V Echo output cannot be directly connected to the inputs of a board like the Circuit Playground Express (CPX) but some trivial electronics can remedy this incompatibility.

This project demonstrates distance measurement in Microsoft MakeCode inspired by the BBC micro:bit example starting with a very simple program and then showing more features of MakeCode and the Circuit Playground Express (CPX) board.

If you are new to using MakeCode, check out the guide to getting started first.

Thank-you to George and Edward for the loan of their CPX board.


1 x Circuit Playground Express
A great sensor-packed development board supporting many languages.
1 x HC-SR04 Ultrasound sensor
HC-SR04 Ultrasound sensor with TTL 5V interface and 2x 10K Resistors for 3.3V use.
1 x Half-size Breadboard
A breadboard to place the sensor and resistors on.
1 x Premium Male/Male Jumper Wires - 40 x 6" (150mm)
Four jumper wires for breadboard connections.
1 x Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces
Four alligator (crocodile) clips to connect to pads on CPX.
Adafruit now sell the US-100 Ultrasonic Distance Sensor which is directly compatible with 3.3V levels.
1 x US-100 Ultrasound Sensor
US-100 Ultrasonic Distance Sensor - 3V or 5V Logic. Easier to use alternative, since it does not require output level conversion.

This guide was first published on Sep 17, 2018. It was last updated on Mar 08, 2024.

This page (Overview) was last updated on Mar 08, 2024.

Text editor powered by tinymce.