top of page

CodeRover X Micro:Bit Sample Codes

Updated: Sep 22, 2023

We have made a CodeRover extension for Micro:Bit. You can add the extension in MakeCode and take advantage of the functions within the extension to control and program your CodeRover.

Adding the extension

  1. open MakeCode for Micro:Bit software on your computer or go to

  2. Click on the "New Project" button

  3. Click on Extensions under the gearwheel menu

  4. Search for and import.

When you have successfully imported the CodeRover extension. You should be able to see the extension added to the list on the left.

<---------------------- right here.

In the CodeRover extension, we created block-style drag-and-drop codes that you can use to control motors, read sensors, and control onboard RGB LEDs.

If you are planning to use other sensors or modules with Micro:Bit for CodeRover, you can add the corresponding extensions for those sensors and modules the same way you added the

CodeRover extension.


Drive CodeRover forward & backward 1.1

The following programs are three different ways you can control CodeRover to drive forward and backward using Micro:Bit.

Function 1 is called drive1, it uses the Drive block in the extension to control CodeRover. The drive block has two inputs, one controls the direction of the CodeRover and the second number input is the speed of the CodeRover. A speed of 100 means drive at full speed and a speed of 0 means stop the motor.

Between each drive block, there is a pause block. The first pause block under the first drive block will keep the drive block running for 1000 ms, therefore, CodeRover will drive forward at 100% of the speed for 1 full second. Immediately after the pause block, the drive direction is switched to backward, therefore CodeRover will now drive backward at 100% of the speed for another 1 full second. At the end of the drive1 function, both CodeRover's motors will stop spinning.

Function 2 is called drive2. Similar to drive1, drive2's drive block has a third input that controls how many milliseconds this function will perform. As a result, the drive2 function will move the CodeRover forward at 50% of the speed for 1 second and then move the CodeRover backward at 100% of the speed for 1 second. These types of drive blocks have a timer function and a function that stops CodeRover built-it.

Function 3 is a more complicated way to control CodeRover, however, it is also more useful when it comes to controlling each of the CodeRover's motors independently.

In the drive3 function, rotating the right motor clockwise at 50% of the speed and rotating the left motor counter-clockwise at 50% of the speed will drive the CodeRover forward at 50% of the speed. When you want to drive backward, all you have to do is to flip the rotating direction of both motors at the same time.


RGB LEDs color 2.1

There are a couple of RGB LEDs on the bottom of the CodeRover Core. Due to pin limitations, when controlling these RGB LEDs with Micro:Bit, we can only use 3 digital pins. The resulting colors are also limited to blue, green, red, cyan, yellow, magenta, and white.

Run the above program, you will see RGB LEDs cycle through 7 colors.


Reading sensors input 3.1

In this sample code, we will use a light sensor module and a led module to create an automated headlight for CodeRover. The idea is to use Micro:Bit to automatically turn on LEDs when the CodeRover is in a dark environment.

We can use the sensor blocks in the CodeRover extension to achieve this. However, we want to show you how you can write this program without using the extensions.

In order for the above code to work on your CodeRover without modifications, you have to connect the signal pin of the light sensor to P2 on CodeRover and connect the VCC pin of the LED module to P3.

In the On Start function, we used a "led enable ( false )" block to turn off the Micro:Bit LED matrix. Since the LED matrix shares several pins that are used to control CodeRover. We recommend adding this block for all your Micro:Bit programs designed for CodeRover. You can read more about the LED Matrix pin setup HERE. In both V1 & V2 versions of Micro:Bit P3, P4, P6, P7, P9(V1), and P10 are used for the LED matrix unless you disable the matrix.

Also in the On Start function, set P3 to a LOW, aka 0, voltage to make sure the LED is turned off before the program starts to run.

In the Forever Loop we are using a simple If Statement to read digital signals from the light sensor using Pin 2. When the light sensor detects bright light, P2 will have a LOW voltage. However, when the ambient light is not bright enough, the light sensor will send a HIGH voltage to P2. In this case, we will turn on the LED light by setting a HIGH voltage to P3.


Controller 4.1

This sample code will help you set up your Micro:Bit for CodeRover Controller. We will talk more about how to use the signals in later programs. For now, you just need to load this code into the Micro:Bit you are planning to use with the controller.

The simplest way to explain this code is that we are sending out all button and joystick movement signals. Micro:Bit on CodeRover can receive these signals using radio group 1. Since the controller can create a ton of data, we will use the simplest logic on the controller side to make sure all of our control signals are captured and sent to CodeRover without any significant delays.


Receive controller signals 5.1

In this sample code, we will use the data sent from the controller to drive around CodeRover. In the previous example, we are sending joy stick data as a pair of "name" and "value". By checking the "value" of the correct "name" we can retrieve data from joysticks and buttons.

In this example, we will use the left joystick to control CodeRover's forward, backward, left spin, and right spin movements. The left joystick's up-down movement is connected to Micro: Bit's P3 and the left-right movement is connected to Micro: Bit's P4. Therefore, the name "joy3" is used to obtain the fwdBwdValue, and "joy4" is used to obtain the leftRightValue.

In the onRadioReceive function, the first two if statements are very important. In the first If Statement, we are using "joy3" to obtain the joystick up-down movement value. Since joystick up and down values are analog values between 0 and 1023, we need to process the incoming data a bit so we can determine which range of value drives CodeRover forward, which drives backward, and which stops CodeRover.

After some testing, we decided to divide analog values into three ranges, less than 500, between 500 and 900, and greater than 900.

When the values from "joy3" and "joy4" is between 500 and 900, we will assign an arbitrary value of 700 to variables fwdBwdValue and leftRightValue. Otherwise, we will store actual analog values to variables fwdBwdValue and leftRightValue.

Finally, all we have to do is to compare the fwdBwdValue and leftRightValue variables to determine whether CodeRover should move forward, backward, or stop.

You can also add servo control codes to control CodeRover CodeArm, Rubber Band Launcher, or BB-GUN with the controller buttons and joysticks. Leave a comment below if you want to see a sample code for that.


Line tracing 6.1

The line tracing robot algorithm is very simple. This sample code is a basic implementation of a line-tracing robot using SUMO and 2 IR modules. For more in-depth reading, feel free to check out here, here, and here.

In this sample code, we are using two IR sensors to detect black lines and white surfaces. The logic is simple:

  1. When left and right IR sensors both detect a white surface, drive straight.

  2. When left and right IR sensors both detect a black surface, drive straight.

  3. When left IR sensor detects white and the right one detects a black surface, turn right.

  4. When left IR sensor detects black and the right one detects a white surface, turn left.

As we have mentioned before, remember to set led enable to false to turn off the LED matrix on Micro:Bit.


SUMO 7.1

Finally, Robot SUMO wrestling sample code. In order to perform the Robot SUMO wrestling algorithm, we need to use an ultrasonic sensor module along with 2 IR sensor modules.

Since the SUMO wrestling map is a circuit with a black border, there are a total of 4 possible combinations of the two IR sensor states: both detect a white surface, both detect a black border, the left sensor detects a black border, and the right sensor detects a black border.

  • When both detect a white surface

    • Drive forward

  • When both detect a black border

    • Drive backward and turn 180 degrees

  • When the left sensor detects a black border

    • Drive backward and turn 90 degrees to the right

  • When the right sensor detects a black border

    • Drive backward and turn 90 degrees to the left

Whenever the robot detects a black border we want to drive backward a bit to always keep our IR sensors within the SUMO wrestling map.

Don't forget we need to check constantly if our opponent is in front of us using the ultrasonic module. Therefore the pseudo-code becomes something like this:

  • Check the distance of any objects in front of SUMO using an ultrasonic sensor

  • if the distance is greater than 5 cm

    • When both detect a white surface

      • Drive forward

    • When both detect a black border

      • Drive backward and turn 180 degrees

    • When the left sensor detects a black border

      • Drive backward and turn 90 degrees to the right

    • When the right sensor detects a black border

      • Drive backward and turn 90 degrees to the left

  • else if the distance is less than 5cm

    • Drive full speed forward to push the opponent out of the SUMO arena.


Controlling CodeArm with Controller 8.1

.As requested by one of our backers, here is how to control CodeArm & CodeArm MAX using Controller and two Micro:Bits. As we have mentioned in 4.1, We are broadcasting all joystick signals to radio group 1. As we can see in the code below.

Now all we need for the other Micro:Bit is to read those data and move CodeRover as the data from the controller changes.

It may seem like quite a lot of code. However, it can be divided into three simple parts.

  1. read and process joystick data.

  2. move CodeRover or Servos.

  3. servo movement functions.

First, connect three servos to the P2, P3, and P10 headers on the CodeRover Core. In the start function. servo positions are all initialized to 90 degrees. So it's best to connect servos and run the program before installing servos on the CodeArm add-on. You can also see how each servo behaves and avoid putting them on the add-ons the wrong way and damaging servos or add-ons.

In the program above, the servo that controls the claw is connected to P10, the servo on the left side of CodeArm is connected to the nearest servo header, SER4, and the right servo is connected to SER2.

277 views3 comments

Recent Posts

See All


Thanks for the sample Microbit codes, really useful to get going, I've had a few of them running with Sumo. Would be great if you can post the codes for controlling servos from the controller mentioned in 5.1, am keen to try out the accessories controlled from the controller! Thanks!

Replying to

thanks, looking forward to trying it out!

bottom of page