micro:bot Kit Experiment Guide

Contributors: D___Run___, TheDarkSaint
Favorited Favorite 1

Experiment 2: Staying in a Box


Robots are smart! They are smart because they use sensors to detect what the world around them and then respond to those conditions. One of the conditions our robot can respond to is the darkness of the surface that it is driving on. It can detect lines, a gradient of darkness or a blacked out area. In this experiment you will program your robot to stay inside of a box that you have drawn on a surface.

Parts Needed

  • 1x micro:bit board (Not Included)
  • 1x Micro B USB Cable (Not Included)
  • 1x moto:bit carrier board
  • 3x Line following Sensors
  • 1x micro-B USB Cable
  • 1x 4xAA Battery Holder
  • 4x AA Batteries (Not Included)

Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment…

micro:bit Board

Hobby Gearmotor - 140 RPM (Pair)

SparkFun moto:bit

Battery Holder - 4xAA to Barrel Jack Connector

Wheel - 65mm (Rubber Tire, Pair)

SparkFun RedBot Sensor - Line Follower

Jumper Wire - 0.1", 3-pin, 6"

Shadow Chassis

1500 mAh Alkaline Battery - AA


Suggested Reading

Getting Started with the micro:bit

The micro:bit is a compact, powerful programming tool that requires no software installation. Read on to learn how to use it YOUR way!

Introduction to the Line Sensors

Line sensor

The Line Follower sensor is an add-on for your shadow chassis that gives your robot the ability to detect lines or nearby objects. The sensor works by detecting reflected light coming from its own infrared LED. By measuring the amount of reflected infrared light, it can detect transitions from light to dark (lines) or even objects directly in front of it.

The sensor has a 3-pin header which connects directly to the moto:bit carrier board via female to female jumper wires. A mounting hole lets you easily connect one or more of these to the front or back of your robot chassis.

Hardware Hookup

Like the motors, you should have already hooked up the line sensors during the assembly portion of this guide. You can go there now for the full assembly instructions. Double check to make sure that the wires are hooked up to your line sensors correctly!

Line Sensor Wire Hookup

The line sensors hookup to your moto:bit via female / female jumper wires that snake through the chassis of your robot up to the moto:bit. The sensors hookup to the moto:bit in the following order:

  • LEFT => P0
  • CENTER => P1
  • RIGHT => P2

Double check to make sure they are hooked up correctly and in the proper orientation

Attached to moto:bit

Running Your Script

Be sure to add the moto:bit package as instructed in the Installing the moto:bit Package in MakeCode section of this tutorial.

Now, you can either download the following example script below and drag and drop it onto your micro:bit, or use it as an example and build it from scratch in MakeCode.

Calibration is very important with the line sensors to work accurately. Your environment will greatly affect the P0 analog readings and thresholds for the surface value so you might have to customize these numbers to suit your application.

Code to Note

Experiment 2 Code Screenshot

Creating a Variable

In Microsoft MakeCode you need to be able to create variables to store information for your robot to compare against, or to just remember. You can use the built in variables under the Variables drawer as shown below.

variable drawer

But, you can also make your own custom variables. To do this click on the Variables drawer and select Make New Variable. You can then name it and use it in your program.

naming variable

Set To

To store a value or piece of information in a variable you use the Set to block. This allows you to select a variable and set it to a value block of your choice. In this case the line sensor value on pin A0. We do this once in the On Start block to get a base reading from the sensor.

Analog Read Pin

To get that base reading from the line sensor we use the Analog Read Pin block to get an analog value (0 - 1023) from the line sensor. 0 is totally black and 1023 is totally saturated white. Your surface will be something probably in between.


To compare the current reading of the line sensor to its known baseline value that was captured and stored in the On Start block we use an if statement block. The if block accepts a logical statement. If that statement (Sensor Value < Surface - 30) is true then the if block runs then “then” section of code. If that statement is false then the “else” section of the block is run.

You may be asking yourself why subtract 30 from the surface value. Well, that number is a sensitivity value. The sensor reading fluctuates as the robot moves around, by subtracting 30 from the original surface variable we give the change in lighting a “wiggle room” of 30. If you want your sensor to be more sensitive you make the number smaller, if you need it less sensitive make it bigger.

What You Should See

Once your code is loaded find a light surface, preferably white and add a line of black tape to the surface and press the RESET button on your micro:bit. In fact, if you have a large enough space you can create a 3' square you will be perfect! Change the motor switch from “STOP MOTORS” to “RUN MOTORS.”

Stop Motors

Run Motoes

Your robot will drive forward until the black tape is directly under the center line sensors. When that happens your robot will stop, reverse, pivot a bit and try to drive forward again, hitting a line once more. If it works correctly your robot should stay inside of the box forever!

Go Further: So, you have built a robot corral! Use what you now know to build a maze for your robot to solve on its own!


  • Moto:Bit blocks not showing up - Make sure you have a network connection and you have added the package to your makeCode environment.

  • Micro:Bit not showing up on my machine - Try unplugging the USB cable and plugging it back in. Also, be sure that you have the cable inserted all the way into your micro:bit

  • Robot doesn’t detect line - If changing the width of your line doesn’t help, remember line sensor calibration settings can be very sensitive depending on your environment.