[2021] Running a Lego Compatible Train With Two (x2) Micro:bits

Introduction: [2021] Running a Lego Compatible Train With Two (x2) Micro:bits

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 based 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 two (x2) micro:bits, one as the transmitter and another one as the receiver on the locomotive. On your transmitter micro:bit, pressing button A or B will let the locomotive go forward or backward. Pressing button A and B simultaneously 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 iPad or iPhone, please review this tutorial instead.

Supplies

Micro:bits

We will need two (x2) micro:bits. One (x1) micro:bit is used for the transmitter. Another (x1) micro:bit is used for the receiver. We've tested the sample codes 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 this locomotive.

Step 1: Copying the Sample Files to Micro:Bit

You can download MakeCode sample files (below) to your computer. These sample files are ready to play, and you can start playing immediately. These sample files can work with both micro:bit version 1 (V1) and version 2 (V2). Connect your computer and a micro:bit via USB cable, and copy each file to each of your micro:bit one at a time.

First, drag and drop Transmitter.hex file to a micro:bit and use this as "transmitter" micro:bit.

Second, drag and drop Receiver.hex file to another micro:bit and use this as "receiver" micro:bit.

Once you've copied the sample files, disconnect micro:bits from your computer. Connect the battery case to "transmitter" micro:bit and turn on. Mount "receiver" micro:bit on your locomotive and turn on the power switch on the motor controller. Once "transmitter" micro:bit and "receiver" micro:bit are turned on properly, they will start communicating and you can control the locomotive. Pressing button A or B on your "transmitter" micro:bit will let the locomotive go forward and backward. To stop the locomotive, press button A and B simultaneously.

Step 2: 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 3: Reviewing Transmitter.hex Sample File

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

on start block

radio set group 1

This block is called initially at once when "transmitter" micro:bit is turned on. In Radio extension, you can find the block radio set group and 1 is set for example. This number must be the same between "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.

on button A pressed block

radio send string "goForward"

In Input extension, you can find the block on button A pressed. This block is executed whenever you press micro:bit button A. In the block, you can also find radio send string "goForward" that will send the radio string "goForward" over the air when "transmitter" micro:bit button A is pressed. When "receiver" micro:bit receives this string, the locomotive will go forward.

on button B pressed block

radio send string "goBackward"

In Input extension, you can find the block on button B pressed. This block is executed whenever you press micro:bit button B. In the block, you can also find radio send string "goBackward" that will send the radio string "goBackward" over the air when "transmitter" micro:bit button B is pressed. When "receiver" micro:bit receives this string, the locomotive will go backward.

on button A+B pressed block

radio send string "stop"

In Input extension, you can find the block on button A+B pressed. This block is executed whenever you press micro:bit button A and B simultaneously. In the block, you can also find radio send string "stop" that will send the radio string "stop" over the air when "transmitter" micro:bit button A and B are pressed simultaneously. When "receiver" micro:bit receives this string, the locomotive will stop.

Step 4: Reviewing Receiver.hex Sample File

Now let's look at Receiver.hex sample file. Open MakeCode editor and click Import button. Open Receiver.hex file you've copied to "receiver" micro:bit.

Let's look at the following function blocks that define the direction and the speed of your locomotive. The locomotive has a micro gear motor M1 and M2 on each bogie.

on start block

radio set group 1

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

In Radio extension, you can find the block radio set group and 1 is set for example. This number must be the same between "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.

on radio received receivedString block

Whenever "receiver" micro:bit catches the radio string sent from "transmitter" micro:bit over the air, "receiver" micro:bit will sort out and call the relevant function. In Radio extension, find on radio received receivedString block and use it for sorting out and call the function. In Logic extension, use if then block and click + sign to expand it as necessary. This block will call the function depending on the received string being sorted out.

if receivedString = goForward then

call goForward

If the received string is "goForward" then drag and drop call goForward block in Functions extension to call the function.

else if receivedString = goBackward then

call goBackward

If the received string is "goBackward" then drag and drop call goBackward block in Functions extension to call the function.

else if receivedString = stop then

call stop

If the received string is "stop" then, drag and drop call stop block in Functions extension to call the function.

function goForward block

call stop

pause (ms) 200

Before the locomotive will go forward, it must have been stopped safely. In Functions extension, drag and drop call stop block to execute function stop before running. In Basic extension, drag and drop pause (ms) 200 to prepare the locomotive for running.

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

call stop

pause (ms) 200

Before the locomotive will go backward, it must have been stopped safely. In Functions extension, drag and drop call stop block to execute function stop before running. In Basic extension, drag and drop pause (ms) 200 to prepare the locomotive for running.

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.

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
    • Chocolate Challenge

      Chocolate Challenge
    • Home and Garden Contest

      Home and Garden Contest

    Comments