Connecting the Keyestudios Ultrasonic Sensor to the Microbit

Tinkerer: Lory Livezey

The Keyestudios Ultrasonic Sensor uses sound waves to determine the distance between it and the nearest object. You can use this sensor to keep your robot from hitting the wall or tumbling down the stairs!

Previous Step

Using the Keyestudios 37 Sensor Kit with the Microbit

What you will need

(Or Longer)
(or a monitor/keyboard/mouse plugged directly into the Pi)

Plug the Microbit into the Breakout

The breakout board gives you an easy way to connect the smaller pins on the Microbit. You could use alligator clips, but connecting them to the sensor can be a bit of a challenge. I am using a Kitronik Motor Board as a breakout. There are several others that will basically work the same way.

Plug Microbit into breakout board

Plug the Microbit into the Raspberry Pi

You can also use a Windows PC or Mac and the instructions are essentially the same. Once you plug it in, the Microbit should show up as a drive in Windows Explorer or Finder.

Hook Up the Sensor

First, we'll hook the lead wires to the sensor and the Microbit. Hold the sensor with the back toward you and move from right to left:

  • Black
  • Blue
  • Green
  • Red

Next, plug the other end into these pins:

  • Black to Ground
  • Red to 3V
  • Blue to Pin 15
  • Green to Pin 16

Open MakeCode

Open the browser to

Install the Sonar Plugin

Open a new project, and click the gear icon in the upper right. Type sonar and click the search icon. Click on the box to install.

Copy/Paste the Code or Build the Blocks

Either copy/paste the following code into the window after selecting Javascript...

input.onButtonPressed(Button.A, () => {
basic.forever(() => {

Or build the blocks like so:

Make Code Blocks

Their location:

Input > On Button A Pressed
Basic > Show Number 0
Sonar > Ping

Give the sketch a name and click Download.

Open your Downloads directory and drag the file (it will have a hex extension) over to the drive that was created when you plugged in the Microbit.

On the Rasbperry Pi, it's located here:

Media > Pi > Microbit

Once the Microbit reboots, the Microbit should display the number of inches between it and the nearest object when you click the A button!

You have successfully programmed the Microbit to work with the Ultrasonic Sensor. Subscribe to our channel if you'd like to be notified when we post a subsequent video about how to hook it up to a wheeled robot.

Be sure to subscribe to our channel!