Skip to content

Search the documentation

Kit
Firmware

Follow the line — Smart Car

The robot has five line sensors in a row underneath it, called OUT1 to OUT5. This program watches them and drives forward only while the middle one alone is over the line. Nudge the robot sideways and it stops.

It is deliberately simple: it does not steer. Making it steer is the exercise at the bottom.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Line follower. Put the robot on a black line on white paper."
  • wait 2 seconds
  • repeat forever
    • Print to serial (create text with "middle sensor (OUT3) = " Read IR OUT3)
    • if compare IRs with OUT1: 0 OUT2: 0 OUT3: 1 OUT4: 0 OUT5: 0
      • Set LED 1 ON
      • set Motor 1 to Direction 1 with speed 40 %
      • set Motor 2 to Direction 1 with speed 40 %
    • else
      • Set LED 1 OFF
      • set Motor 1 to Direction 1 with speed 0 %
      • set Motor 2 to Direction 1 with speed 0 %
    • wait 0.2 seconds
  • A Smart Car with the IR line-sensor array fitted underneath.
  • A USB cable.
  • A black line on white paper. Wide electrical tape on a white sheet is perfect. About 2 cm wide works best.
  • Somewhere flat. This one really does drive.
  1. Open 06-line-follow.mobot, plug in, press Send to Device.
  2. Put the robot on the paper, straddling the line, with the middle sensor over the black.
  3. Watch LED 1 and the serial monitor.
  • The serial monitor prints middle sensor (OUT3) = 0 or = 1, several times a second. Slide a finger under the middle sensor and the number must change. If it never changes, the sensor array is not connected — stop here.
  • With the middle sensor over the black line and the other four over white: LED 1 lights and both wheels drive forward.
  • Lift the robot off the line, or slide it sideways: LED 1 goes out and the wheels stop immediately.

The test passes if the wheels start and stop as you move the robot on and off the line, and if the printed number flips between 0 and 1 as you cover the middle sensor.

The compare IRs block asks a very strict question: are the five sensors showing exactly 0, 0, 1, 0, 0? Any other pattern — two sensors on the line, or none — is a no. That strictness is what makes it a good test: there is no doubt about what the robot is thinking.

block what it does
mobot_readir reads one sensor, OUT1 to OUT5, and gives 0 or 1.
mobot_compirs asks whether all five match a pattern you choose.
mobot_motordirspeed drives a wheel. Speed 0 is a stop.
controls_if (if / else) do this when the pattern matches, that when it does not.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

06-line-follow.firmware-3.main.py
# Follow the line — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 06-line-follow.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 06-line-follow.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Line follower. Put the robot on a black line on white paper.')
time.sleep(2)
while True:
print("[MOBOT]", ('middle sensor (OUT3) = ' + str(robot.line_sensor.read(2))))
if robot.line_sensor.matches([0,0,1,0,0]):
robot.led1.set(1)
robot.left_motor.backward(40)
robot.right_motor.backward(40)
else:
robot.led1.set(0)
robot.left_motor.backward(0)
robot.right_motor.backward(0)
time.sleep(0.2)
  • Change the pattern to 0 0 0 0 0 and see the robot drive only when it is off the line entirely — the exact opposite behaviour.
  • Make it steer. Add two more if blocks: if Read IR OUT2 is 1, slow the left wheel; if Read IR OUT4 is 1, slow the right one. Now it hunts along the line instead of stopping.