micro:bot Kit Experiment Guide

Favorited Favorite 2

Experiment 3: Following a Line


OK, you have your robot staying inside of box drawn on the floor, but that still seems a little odd and random. You want your robot to go somewhere, do something and then keep going! In this experiment you will elaborate what you learned from Experiment 2 to get your robot to follow a line.

Parts Needed

You will need the following parts:

  • 1x micro:bit board (Not Included with Kit)
  • 1x Micro-B USB cable (Not Included with Kit)
  • 1x moto:bit Carrier Board
  • 2x Wheels
  • 1x Assembled Shadow Chassis
  • 2x Hobby Gear Motors
  • 1x 4xAA Battery Holder
  • 4x AA Batteries (Not Included with Kit)
  • 3x Analog Line Following Sensors
  • 3x 3-Pin Jumper Wires

Didn’t get the kit? Have no fear! Here are the parts you will need to complete this experiment. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.

Hobby Gearmotor - 140 RPM (Pair)

Hobby Gearmotor - 140 RPM (Pair)

SparkFun moto:bit

SparkFun moto:bit

micro:bit Board

micro:bit Board

Wheel - 65mm (Rubber Tire, Pair)

Wheel - 65mm (Rubber Tire, Pair)

USB micro-B Cable - 6 Foot

USB micro-B Cable - 6 Foot

Shadow Chassis

Shadow Chassis

Battery Holder - 4xAA to Barrel Jack Connector

Battery Holder - 4xAA to Barrel Jack Connector

SparkFun RedBot Sensor - Line Follower

SparkFun RedBot Sensor - Line Follower

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

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


1500 mAh Alkaline Battery - AA


Suggested Reading

Getting Started with the micro:bit

The BBC 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 Using Multiple Line Sensors

In the previous experiment you used a single line sensor (the middle sensor) to detect the line on the floor. That is great for staying inside of a line, but now, you need to follow a line. That is where the other two line sensors come in.

Multiple Lines Sensors

Essentially, you want the center sensor to detect the line, but not the other two, meaning that the robot is centered on the line. If one of the side sensors detect a line it means that you are veering to one side or another and your robot should correct itself. We will use the information from multiple sensors combined with an if/else statement block to build a decision tree for your robot to follow. For simplicity, we will start with using just two of the line sensors (the left and right sensors).

Hardware Hookup

Note: If you already hooked up your sensors in Experiment 2, please skip this section.

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!

Hooking Up Line Sensors

Line Sensor Connections to Wire

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 => PO
  • CENTER => P1
  • RIGHT => P2

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

Line Sensor Connections 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 and P2 analog readings and thresholds for the surface value so you might have to customize these numbers to suit your application.

Code to Note

Let’s take a look at the code and what to expect.

Experiment 3 Screenshot

LCal / RCal

Like in the previous experiment, you need to set a baseline value for the surface that your robot is driving on. This is actually called a calibration value. Like the previous experiment, we need to do this, but for two sensors; right and left. We go through the same routine we did for the single sensor previously, but for the right and left sensors.

On Button Press

As in the first experiment, we use the On Button Press block to start the program. This is so you can can get a good base reading and calibrate your sensors without having to wrestle with a robot that is trying to move around.


The While block is a logic block that is similar to the loop block, but a bit smarter. The While block accepts a TRUE/FALSE statement, if that statement is true the block will loop whatever code is placed inside of it. If that value is false the While block is simply skipped over. We hardcode a true value into the While block so that it constantly loops when the A button is pressed. That way we have the benefits of both the event block and the loop block without needing complicated programming.

What You Should See

Once you have loaded your script place your robot on a dark line on a light / white background. Make sure you have it centered with the line just underneath the center line sensor. Make sure the motor switch is changed from "STOP MOTORS" to "RUN MOTORS" and press the A button to start.

Stop Motors

Run Motors

Pressing Button A

Your robot should drive forward until one of the side line sensors detects the line and then it will turn in that direction to correct itself. Depending on your line shape and thickness your robot may "waddle" more or less.

Go Further: Your robot can go where you tell it by following a line. Try adding more code to utilize the center line following sensor. Instead of having the robot follow one straight line, try building an obstacle course for your robot using the line sensor! Adjust the motor speed and remove the LED array to see how fast your line following robot can move along the line without veering off the course.


  • Robot Drives In a Circle - Double check your wiring and your code, your robot is not seeing the line. Also, double check that there isn't something like dust blocking your sensor.

  • Robot Isn't Moving - Pesky motor switch! Make sure that is set to "run" and you have fresh batteries

  • Moving in Reverse?! - There are two options if you notice the micro:bot moving in the wrong direction. As explained above, you can flip the wiring of your motors or use the set ____ motor invert to ____ block in your on start block to change what is forward vs. reverse.

  • Robot Waddles a Lot! - Change the width of your line.

  • 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. Also, the code calibrates when the micro:bot starts up. Make sure to have the sensors over a black line.