[2021] Running a Lego Compatible Train With Micro:bit and Tablet (iPad or IPhone)

Introduction: [2021] Running a Lego Compatible Train With Micro:bit and Tablet (iPad or IPhone)

About: We design engineering programs and kits for educators and makers. We support students' self-directed and mutually engaging learning experiences.

Are you interested in running a BBC micro:bit powered train? Micro:bit based robotics platform can be transformed into trains, and you can play it with Lego train and rail ecosystem.

In this tutorial, we will use Valenta Zero robotics controller to develop a micro:bit based Lego compatible locomotive running on Lego rails.

Valenta Loco is a powerful and smooth running micro:bit controlled Lego compatible locomotive. It is equipped with Valenta Zero robotics controller that can be controlled by a micro:bit for playing with Lego trains and rails. The locomotive has two (x2) micro gear motors in the bogies. You can use Lego Technic bricks to build your unique body shell on the locomotive.

3D printing Lego Compatible Rails via Thingiverse

Check Thingiverse and you can 3D print your Lego compatible rails based on your layout plan.

Sample Codes

In this tutorial, we will present how to set up micro:bit app on your iPad (iPhone), how to pair it with your micro:bit, and how to use Gamepad in the app for controlling the locomotive. Pressing Gamepad button A or B will let the locomotive go forward or backward. Releasing Gamepad button A or B will let the locomotive stop.

You can download ready-to-play MakeCode sample files and play immediately in this tutorial. This tutorial does not require coding at all. However, what's really fun is to reverse-engineer how it's running under the hood. If you want to discover more, you can further continue reading the following steps to go deeper in the knowledge.

If you want to control your locomotive using two (x2) micro:bits, please review this tutorial instead.

Supplies

iPad (iPhone)

We will use an iOS device (iPad or iPhone) in this tutorial. Make sure you've downloaded micro:bit app in the App store. Please check your device compatibility with the app for using Bluetooth connection.

Micro:bit

We will use one (x1) micro:bit for the receiver. We've tested the sample code in this tutorial and both micro:bit version (V1) and version 2 (V2) are working correctly.

Batteries
We will use new, single-use six (x6) 1.5V AA batteries for the locomotive.

Kit

You can obtain Valenta Loco micro:bit powered locomotive engine via Projects4Kiz.

You can also refer to the instruction for assembling the locomotive.

Step 1: Checking If Wiring Is Correct

We will make sure if the locomotive is set up properly. Especially we will check the wiring connection.

The locomotive is equipped with micro:bit based motor controller called Valenta Zero. It contains a small motor controller (DRV8833) for controlling a DC motor on each bogie. There are four (x4) GPIO (general purpose input and output) pins P0, P1, P2 and P8 for connecting devices like LED headlights. Each of P0, P1, P2, P8 pins have three pins, (S)Signal (yellow wire cable), (V)Voltage (red wire cable) and (G)Ground (black wire cable). These pins can operate at 3.3V or 5V (selectable) and please choose 5V for connecting LED headlights.

We can find red and black wires coming from each of the bogie on the locomotive. They are motor cables.

  • Pick up a motor cable and put its red wire to the left pin on M1 and its black wire to the right pin on M1 as shown in the figure.
  • Pick up another motor cable and put its red wire to the left pin on M2 and its black wire to the right pin on M2 as shown in the figure.

If you have a train body shell equipped with front LED headlights, you can light them up! Say let's choose P8 for connecting these LED headlights.

Put its red wire to (V) Voltage pin at P8 and its black wire to (G) Ground pin at P8 as shown in the figure. LED headlights are ready to light up when the power is on.

Step 2: Copying the Sample File to Micro:bit

You can download MakeCode sample file (below) to your computer. This sample file is ready to play, and you can start playing immediately. This sample file can work with both micro:bit version 1 (V1) and version 2 (V2).

Please download Receiver.hex file below. Then, connect your computer and a micro:bit via USB cable. Drag and drop Receiver.hex file to the micro:bit and use this as "receiver" micro:bit. Once you've copied the sample file to the micro:bit, disconnect it from your computer.

Mount "receiver" micro:bit on the locomotive and turn on the power switch on Valenta Zero motor controller. Now try pressing button A or B. The locomotive will run forward or backward.

Step 3: Downloading Micro:bit App

Let's download micro:bit app to your iPad (iPhone) in the App store.

Open the app and tap choose micro:bit button for pairing your micro:bit.

Step 4: Pairing a Micro:bit

So far you have not paired any micro:bit to your iPad (iPhone) yet. Click Pair a micro:bit button.

Step 5: Pressing Button A, B and Reset Buttons

You will need to hold pressing button A and button B, and simultaneously press and release Reset button.

When all the dots in the 5x5 LED matrix flash altogether, click Next button.

Step 6: Tapping the LED Dots

In the app, you will need to tap each LED dot just the way exactly shown on your micro:bit 5x5 LED matrix. This LED dot pattern is unique and different for everyone.

Step 7: Pairing to the Device

When you are ready to pair, let's click Next button.

Step 8: Searching Via Bluetooth

Please make sure that your iPad (iPhone) has Bluetooth ON. Micro:bit app is trying to search and pair with your micro:bit.

Step 9: Checking If Pairing Is Successful

Once your micro:bit and iPad (iPhone) have been paired successfully, please click OK button.

Step 10: Setting Up Gamepad

Let's go back to the top menu and click Monitor and Control button. Click Add (+) button and choose Gamepad in the list.

Step 11: Playing With Gamepad

Please click Start button to activate Gamepad with your micro:bit. Now you can control your micro:bit using Gamepad. Pressing button A or B will let the locomotive run forward or backward.

If you want to discover how it's running, you can further continue reading the following steps to go deeper in the knowledge. We will explain the anatomy of the sample code.

Step 12: Removing Micro:bit Paired in the App

The app allows you to set up multiple micro:bits simultaneously.

If you've paired too many micro:bits and if you want to remove one of them, go to top menu and click Choose micro:bit button. You can find the list of micro:bits you've paired previously. Click Trash button to remove the micro:bit you want.

Step 13: Removing Micro:bit Paired in Bluetooth Setting

We also recommend that you clean up Bluetooth setting occasionally by removing micro:bits you've paired previously. To clean up already paired micro:bits in Bluetooth setting on your iPad (iPhone), please follow these steps.

Go to Setting.

Go to Bluetooth.

Click the micro:bit you want to remove.

Click Forget this device button.

Click OK button.

Step 14: Adding Extensions to MakeCode Editor

Does your MakeCode editor include following extensions? Please open the editor and check if it includes these extensions in the left menu. You will use these extensions in Receiver.hex sample file. If you can't find these extensions, click Extensions in the bottom of the menu. Then, click to add these extensions to the menu one by one.

Bluetooth extension

You will need this function to control "receiver" micro:bit's digital and analog pins via Bluetooth from your iPad (iPhone).

Devices extension

You will need this extension to define the actions assigned to each Gamepad button in micro:bit app on your iPad (iPhone).

Step 15: Reviewing Receiver.hex Sample File

Now we will examine Receiver.hex sample file. Open MakeCode editor and click Import button. Open Receiver.hex file you've copied to "receiver" micro:bit.

The locomotive has a micro gear motor M1 and M2 on each bogie.

on start block

This block is called initially at once when "receiver" micro:bit is powered on.

bluetooth io pin service

In Bluetooth extension, drag and drop bluetooth io pin service block. With this block, you can access to "receiver" micro:bit's digital and analog pins via Bluetooth.

function goForward block

digital write pin P13 to 0

P13 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P13 to 0, so that M1 goes forward.

digital write pin P15 to 0
P15 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P15 to 0, so that M2 goes forward.

analog write pin P12 to 1023

P12 pin is used for speed (max speed is 1023) of M1 motor. In Pins extension, drag and drop analog write pin P12 to 1023, so that M1 goes forward at max speed.

analog write pin P14 to 1023
P14 pin is used for speed (max speed is 1023) of M2 motor. In Pins extension, drag and drop analog write pin P14 to 1023, so that M2 goes forward at max speed.

function goBackward block

digital write pin P12 to 0

P12 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P12 to 0, so that M1 goes backward.

digital write pin P14 to 0

P14 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P14 to 0, so that M2 goes backward.

analog write pin P13 to 1023

P13 pin is used for speed (max speed is 1023) of M1 motor. In Pins extension, drag and drop analog write pin P13 to 1023, so that M1 goes backward at max speed.

analog write pin P15 to 1023

P15 pin is used for speed (max speed is 1023) of M2 motor. In Pins extension, drag and drop analog write pin P15 to 1023, so that M2 goes backward at max speed.

function stop block

digital write pin P13 to 0

P13 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P13 to 0, so that M1 is set for forward direction.

digital write pin P15 to 0

P15 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P15 to 0, so that M2 is set for forward direction.

analog write pin P12 to 0

P12 pin is used for speed (0 means no speed) of M1 motor. In Pins extension, drag and drop analog write pin P12 to 0, so that M1 stops.

analog write pin P14 to 0

P14 pin is used for speed (0 means no speed) of M2 motor. In Pins extension, drag and drop analog write pin P14 to 0, so that M2 stops.

on gamepad button A down block

In Devices extension, you can find the block on gamepad button A down block. This block is executed whenever you press Gamepad button A on your iPad (iPhone). When pressed, the block will callgoForward function and the locomotive will go forward.

on gamepad button A up block
In Devices extension, you can find the block on gamepad button A up block. This block is executed whenever you release Gamepad button A on your iPad (iPhone). When released, the block will call stop function and the locomotive will stop.

on gamepad button B down block
In Devices extension, you can find the block on gamepad button B down block. This block is executed whenever you press Gamepad button B on your iPad (iPhone). When pressed, the block will call goBackward function and the locomotive will go backward.

on gamepad button B up block

In Devices extension, you can find the block on gamepad button B up block. This block is executed whenever you release Gamepad button B on your iPad (iPhone). When released, the block will call stop function and the locomotive will stop.

Toys & Games Contest

Participated in the
Toys & Games Contest

Be the First to Share

    Recommendations

    • Tinkercad to Fusion 360 Challenge

      Tinkercad to Fusion 360 Challenge
    • Cardboard Speed Challenge

      Cardboard Speed Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest

    Comments