Lego® Power Functions IR Sender for Micro:bit

About: ElecFreaks is an open design house founded in March 2011. Our goal is to create rapid prototypes and give small-batch manufacture solution for makers and startups. With over 50% staff as engineers and expand...

Control your Lego® Power Functions motors using your micro:bit, an infrared LED and MakeCode.


Hardware components:

BBC micro:bit board Infrared LED LEGO® 8884 - Power Function IR Receiver

Software apps and online services:

Microsoft MakeCode pxt-powerfunctions MakeCode Project

Step 2: STORY

LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.

Step 3: The Challenge

We experimented with visual programming languages, and we love colorful interlocking plastic bricks. The desire to control a LEGO® train using a web-based, visual development environment was born.

Step 4: Our Journey

We started with a Scratch and Arduino based solution. We had a working prototype that proved to be error-prone and complicated in the setup.

In our second approach, we used the German sibling of the micro:bit. The Calliope board comes with a motor driver which reduced the required hardware to the board itself and some home-made connection cables. Our first field test with a LEGO® model failed tremendously. The analysis revealed that we want to control at least two engines and lights fully. The more, the better. The Calliope is limited to one motor.

Step 5: Our Solution

Finally, we built a custom package for MakeCode that enables a micro:bit with an attached infrared LED to send commands to a Power Functions IR receiver - like a remote control. Now we were able to control multiple devices with minimal hardware requirements and provide easy-to-use programming blocks additionally.

We published our package on GitHub at Please raise an issue there if you have any problems.

Step 6: Hardware Setup

You need mainly two things: a micro:bit board and an infrared LED that emits IR light at a wavelength of 940 nm. Any IR LED from an old remote control will probably do the job. We tested with SFH 4546 and TSAL 6200. We attached the LED with banana connectors and female jumper cables. Connect the long leg of the LED to an analog pin. We recommend pin 0 because this one is configured by default in the MakeCode package. Plug the LED's short leg into GND.

Connect the LED to pin0 and GND

With only a few bricks you can fasten the micro:bit tightly to your LEGO creation.

micro:bit fits well with LEGO bricks

Step 7: Software Setup

Open MakeCode and add our power-functions package via Add Package in the Advanced menu. You need to enter our project URL hit return and then select the power-functions package.

The repository URL is required to add the package

New blocks will be available via the Power Functions menu and documentation will show up when you hover over the blocks.

Power Functions blocks in Makecode.

Step 8: Transmit Power Functions Commands

Let us plug together our first Power Functions program. First of all, we need to configure the pin of IR LED. You can skip this step if you use pin 0 because it is the default anyway. Reconfigure the direction of motors if needed. It is like switching the direction toggle on LEGO remote controls.

Configure IR LED and motors

Now it is time to start and stop the engine that we attached to the blue output on a LEGO IR receiver. Do not forget to set the receiver's channel appropriately.

Control a motor using the buttons

And, by the way, you can use a LEGO® Power Functions remote control to send additional commands at any time.

Step 9: Example: Train Pauses Automatically at the Station

How to make a train stop at a specific location? We decided to use the micro:bit's capability to measure the light level and designed a light barrier. We installed the board on a train wagon and build a 'lighthouse' using two pairs of LEGO Power Functions LEDs.

The lighthouse

Step 10: Our Train

The brain of the train

Our program was the following:

  • Plot the light level.
  • Stop train for 5 seconds if the light level is above a threshold.
  • Afterwards move forward for 3 seconds till next light level check.

Pause some seconds when passing a light barrier

Train stops at light barrier


Please let us know your ideas. We are looking forward to your comments.

Have fun!

Johan & Philipp

Step 11: CODE

JavaScript code example MakeCodeJavaScript

Use IR LED on P0. Change direction of the motor on output blue on channel 1. Plot light level Stop train for 5 seconds if the light level is above a threshold. Move forward for 3 seconds till next light level check.

powerfunctions.setMotorDirection( PowerFunctionsMotor.Blue1, PowerFunctionsDirection.Backward )

input.onButtonPressed(Button.A, () => { powerfunctions.moveForward(PowerFunctionsMotor.Blue1) })

input.onButtonPressed(Button.B, () => { powerfunctions.float(PowerFunctionsMotor.Blue1) })

basic.forever(() => { led.plotBarGraph(input.lightLevel(), 255)
if (input.lightLevel() > 180) {
    powerfunctions.setSpeed(PowerFunctionsMotor.Blue1, 2)


This article is written by Philipp Henkel and forwarded from

Step 12: Source

This article is written by Philipp Henkel and forwarded from



    • Arduino Contest 2019

      Arduino Contest 2019
    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure


    Hello there!

    We're glad you want to share something with the Instructables community!

    And we’re here to help you out.

    Due to some recent changes on the site, we are no longer able to publish stand-alone videos and other forms of incomplete posts. In order to be published live on the site, Instructables must consist of the following things:

    - Multiple steps showing how you made your project

    - Written instructions in each step

    - Your own original images

    Beyond making your Instructable simply publishable, this guide ( explains what is required to have your Instructables featured by our site editors. It’s very helpful, and definitely worth checking out.

    We would love to review your project again after you have made the necessary edits, and we will publish your project if it is eligible.

    If you have any questions, please feel free to ask right here or send us an email at


    Instructables Community Manager