HC-SR04 Micro:bit

Measure the distance between an obstacle and the HC-SR04 sensor with Micro:bit

The HC-SR04 ultrasonic sensor can be used to measure the distance between an obstacle and the sensor using the Micro:bit. The sensor uses ultrasound to measure the distance, by sending out a sound wave and measuring the time it takes for the sound wave to bounce back.

To use the HC-SR04 sensor with the Micro:bit, you will need to connect the sensor to the Micro:bit‘s pins. The sensor has four pins: VCC, GND, Trig and Echo. VCC should be connected to 3V on the Micro:bit, GND to GND, Trig to one of the digital output pins (such as pin 0 or pin 1) and Echo to one of the digital input pins (such as pin 2 or pin 3).

Purpose of this tutorial:

In this tutorial we will learn how to use a reference ultrasonic distance sensor HC-SR04 with Micro:bit.

Components required

Micro:bit board

Micro:bit is a small, low-cost microcontroller board that was developed by the Micro:bit Educational Foundation for use in computer education. It is designed to be easy to use and accessible to a wide range of users, including children and beginners. The Micro:bit board measures about 4cm by 5cm and features a 25-pin edge connector, a 5×5 LED matrix, two programmable buttons, an accelerometer, a compass, a Bluetooth Low Energy (BLE) module, and a USB connector.

The Micro:bit is programmed using a variety of programming languages such as Python, JavaScript, and Microsoft Block Editor. It can be used to control a wide range of devices and projects, including robots, drones, lights, and more. The board can be powered by a USB cable or a 3V coin cell battery and can be programmed using a computer or mobile device.

The Micro:bit is widely used in education, providing a hands-on learning experience for students of all ages to learn computer programming, logic, and problem-solving. It’s also used by hobbyist and makers to create fun and interactive projects.

Microbit GPIO board

A Micro:bit GPIO (General Purpose Input/Output) Expansion Board is an accessory that can be used to extend the capabilities of a Micro:bit by providing additional input/output (I/O) pins. These pins can be used to connect sensors and actuators, such as LEDs, buttons, motors, and other electronic components.

The expansion board typically connects to the Micro:bit using a standard edge connector and provides a set of male headers that can be used to connect external components.

HC-SR04 sensor 

The HC-SR04 is an ultrasonic distance sensor that uses sound waves to determine the distance to an object. It is a low-cost, easy-to-use sensor that can be used in a wide range of applications. The sensor has four pins: VCC, GND, Trig and Echo.

  • VCC: Connected to a power supply, typically +5V
  • GND: Connected to the ground
  • Trig: A digital output pin that is used to trigger the sensor to send out an ultrasonic pulse.
  • Echo: A digital input pin that receives the returning ultrasonic pulse and provides the duration of the pulse.

The sensor works by sending out a short ultrasonic pulse from the Trig pin and measuring the time it takes for the echo of the pulse to return to the Echo pin. The distance to an object can then be calculated using the speed of sound and the duration of the echo pulse.

The HC-SR04 sensor can measure distances from 2cm to 400cm with a precision of 3mm. However, the accuracy of the sensor can be affected by the environment, such as temperature and humidity, as well as the properties of the object being measured, such as its size, shape, and reflectivity.

connecting wires

Connecting wires are used to connect various components in an electronic circuit. They allow for the transfer of electricity, data, or signals between different devices and components.

When connecting wires to an Arduino or other microcontroller, it is important to pay attention to the correct pinout. The pinout refers to the arrangement of pins on the microcontroller and the corresponding function of each pin. The Arduino pinout can be found in the documentation provided by the manufacturer, or in various resources available online.

test plate

A test plate, also known as a test jig, is a device used to test electronic circuits and components. It is a board or plate that has been designed to hold and connect various components and devices in a specific configuration, allowing for the easy testing and measurement of their performance.

A test plate can be used to test various types of electronic circuits and components, such as microcontrollers, sensors, and actuators. It typically includes connectors and sockets for connecting wires, power supply and measurement devices such as multimeters, oscilloscopes, and power supplies.


The assembly is disconcertingly simple:

  • The 3.3V power supply from the Micro:bit card goes to the VCC pin of the sensor.
  • The GND pin of the Micro:bit card goes to the GND pin of the sensor.
  • The P1 pin of the micro: bit card goes to the TRIGGER pin of the sensor.
  • The P0 pin of the micro: bit card goes to the ECHO pin of the sensor.

There are several mounting methods.

Montage (1)

Montage (2)

Makecode program

Here is the makecode program which displays the distance in cm on the micro: bit card which separates the ultrasonic sensor from an obstacle. Note: You must go to Extensions to import the Sonar extension.

0 commentaire

Leave a comment

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Scroll to Top