BBC micro:bit
Bitkit - Following Lines


There are 5 line sensors on the Bitkit. This not only gives you width but also the fine control to allow you to move more or less depending on how far off the line the car is.

The line sensors are accessed via i2c, using a different address to the one used for driving the motors.

micro:bit circuit


The program makes use of the Drive procedure from earlier. I like to follow lines using a procedure. That gives me a little control in my main loop and works out easier to integrate into programs where I want to step in and out of line-following mode.

To detect a line, you first write 0x02 to the i2c device at address 0x27. Then you read back a byte. This gives a value from 1 to 6, with those numbers corresponding to,

  1. Middle
  2. No line
  3. Left
  4. Far Left
  5. Right
  6. Far Right

In the FollowLine() procedure, I use a list of motor speeds. Each one is a tuple, with one value for the left and one for the right motor. The values are determined by the match of the list position to the reading. That gives an efficient way to determine the speed to drive the motors and saves a handful of IF statements.

from microbit import *

def Drive(lft,rgt):
    ld, rd = 0, 0
    if lft<0:
        ld = 0xff
        lft += 255
    if rgt<0:
        rd = 0xff
        rgt += 255

def Follow_line():
    ln =,1)[0]
    drv = [(0,0),(255,255),(255,255),
    lt,rt = drv[ln]

while True:

The FollowLine() procedure has to be called repeatedly. It works for 20 milliseconds at a time, making a single decision about motor speed each time it is called. I found that this worked nicely on the test tracks that I used.


One quick extension to the code on this page would be to combine some light and sound effects with the decision making. The neopixels can be quite helpful in showing you the sensor readings you are getting if you are finding it hard to make the car follow a line on a track you have made.

Another extension would be to combine remote control of the car with the ability to put it into and out of line sensing mode. This would allow you to drive the car into the correct position on a track and then put it on autopilot until you want to drive away again.