Cables & Connectors

Electronics Components

Motors & Controllers

On Sale items!

Get Professional Support

BBC Micro:bit


On sale items!


Check out our latest news to find out more about events, opportunities and partnerships.

Esmes' Electronics

Esmes' Electronics is a simple introduction to using the micro:bit written by a year 5 student.

Troys' tutorials

Learn with Troy by asking for support and, he will post a tutorial here to benefit all of our visitors. Free advice!

Feature teacher

Be inspired by some of our favorite teachers using our products.

Get professional support

Contact us now to request your own tutorial or learning resource.

This section doesn’t currently include any content. Add content to this section using the sidebar.

Image caption appears here

Add your deal, information or promotional text

Distance Sensing with a HC-SR04 and micro:bit - Basic

In this lesson, you will learn how to display a bar graph that is proportional to the distance of an object using an HC-SR04 ultrasonic sensor.

This module includes an ultrasonic transmitter, receiver and all of the control circuity to make it simple to use.  When you look at the device you will see two round cans, one with a T for Transmitter and one with an R for Receiver.

Parts List:

  • micro:bit
  • USB Cable
  • 2xAAA Switched Battery Pack
  • 3xAA Switched Battery Pack
  • Breadboard
  • Edge Connector
  • Jumper Wires
  • HC-SR04


The first thing to note is the HC-SR04 requires 5Volts DC to operate. This is important as the micro:bit only need 3Volts.

The HC-SR04 only has 4 pins, so it is very easy to connect up. VCC is connected to the 5 Volts supply and GND is connected to the 0 Volt or Ground.

Now be sure to connect both grounds from your power supplies together!

This is so there is a reference between the 3 Volts on the micro:bit and the 5 Volts on the sensor.  Luckily the micro:bit pins can tolerate the 5 Volt signal coming into it.

Now because the HC-SR04 only has 4 pins, you can use the banana plug default pins 0, 1, 2 and GND.  In the code example, the trigger pin is connected to the micro:bit P1 and the Echo pin is connected to the micro:bit P2

Now depending on which breakout board you use the way it is assembled on a breadboard may differ.  In this example, the Elecfreaks breadboard adapter is used.

micro:bit HC-SR04 Wiring



To program your HC-SR04 using the micro:bit it has been made super easy for you through the use of the sonar extension.

Start by making a new program.  Then click on the advanced tabmicro:bit makecode advanced tab for installing extensions

and then click on extensions...

micro:bit add extension

Sonar should be one of the blocks that come up, but if it does not just type sonar into the search box.  To install it, just click on it!

micro:bit makecode sonar tab for HC-SR04


Now you will be able to use the code below!



Leave a comment (all fields required)

Comments will be approved before showing up.