Follow the black line — Mini
Underneath the Mini there are two little eyes that look at the floor. They cannot see colours, only dark and light. With one eye on each side of a black line, the Mini can drive along the line all by itself: if the line slides under the left eye, turn left; under the right eye, turn right.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Mini.
The program
Section titled “The program”Read the program as text
- MoBot Mini Start
- Print to serial
"Line follower — put the Mini on a black line on white paper." - Initialize both IR sensors
- wait 1 seconds
- repeat forever
- Print to serial (create text with
"left: "Read left IR sensor" right: "Read right IR sensor) - if ((Read left IR sensor = 1) and (Read right IR sensor = 1))
- Set LED 1 ON
- Set LED 2 ON
- set Motor 1 to Direction 1 with speed 55 %
- set Motor 2 to Direction 1 with speed 55 %
- else if (Read left IR sensor = 1)
- Set LED 1 ON
- Set LED 2 OFF
- set Motor 1 to Direction 1 with speed 0 %
- set Motor 2 to Direction 1 with speed 55 %
- else if (Read right IR sensor = 1)
- Set LED 1 OFF
- Set LED 2 ON
- set Motor 1 to Direction 1 with speed 55 %
- set Motor 2 to Direction 1 with speed 0 %
- else
- Set LED 1 OFF
- Set LED 2 OFF
- set Motor 1 to Direction 1 with speed 0 %
- set Motor 2 to Direction 1 with speed 0 %
- wait 0.05 seconds
- Print to serial (create text with
What you need
Section titled “What you need”- A MoBot Mini, charged and switched on.
- A USB cable.
- The two IR floor sensors plugged in — left to GPIO 26, right to GPIO 27.
- A black line on white paper. A thick marker line about 2 cm wide, or a strip of black electrical tape on a white floor. Make it a big loop so the Mini can go round and round.
⛔ Unplug the ultrasonic sensor first. On the Mini the IR sensors and the ultrasonic sensor use the same two pins (26 and 27). Only one of them can be connected at a time. Use this example or 05-ultrasonic-stop, never both at once.
⚠️ First time? Read the “Before you send it” section of Blink and fade.
How to run it
Section titled “How to run it”Step 1 — check the eyes before you let it drive
Section titled “Step 1 — check the eyes before you let it drive”- Plug the IR sensors in, stand the Mini on a book so the wheels are in the air, and press Send to Device.
- Open the serial monitor. You will see a line five times a second:
[MOBOT] left: 1 right: 0 - Slide a piece of black paper under the left eye and watch the numbers and LED 1. Then under the right eye and watch LED 2.
One of the two lights should come on when that eye is over black. This is the whole test, and you can do it on a table with the wheels in the air.
If the lights come on over white instead of black, your sensors read the other way round. That is normal — different sensor boards do it differently. The fix is easy: in the three
ifblocks, change every= 1to= 0. Send it again and it will be right.
Step 2 — let it drive
Section titled “Step 2 — let it drive”Put the Mini down on the line with one eye either side of it. It should crawl along the line, wiggling a little from side to side. When it comes off the end of the line it stops and both lights go out.
What you should see
Section titled “What you should see”| what is under the Mini | lights | wheels |
|---|---|---|
| both eyes on the line | both on | both wheels forward — straight ahead |
| line under the left eye only | LED 1 only | right wheel only — it turns left |
| line under the right eye only | LED 2 only | left wheel only — it turns right |
| no line at all | both off | stopped |
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_init_irs |
Wakes both eyes up. Nothing works without it. |
mobot_read_left_ir / mobot_read_right_ir |
What each eye sees: 1 or 0. |
controls_if with two “otherwise if”s |
Four different situations, four different answers. |
logic_operation (and) |
“left eye and right eye” — both at once. |
mobot_motordirspeed |
Turning is just one wheel slower than the other. |
The Python code
Section titled “The Python code”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.
# Follow the black line — mini (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 06-ir-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-ir-line-follow.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Line follower — put the Mini on a black line on white paper.')robot.enable_line_sensor()time.sleep(1)while True: print("[MOBOT]", (''.join([str(x) for x in ['left: ', robot.line_sensor.read(0), ' right: ', robot.line_sensor.read(1)]]))) if robot.line_sensor.read(0) == 1 and robot.line_sensor.read(1) == 1: robot.led1.set(1) robot.led2.set(1) robot.left_motor.forward(55) robot.right_motor.forward(55) elif robot.line_sensor.read(0) == 1: robot.led1.set(1) robot.led2.set(0) robot.left_motor.forward(0) robot.right_motor.forward(55) elif robot.line_sensor.read(1) == 1: robot.led1.set(0) robot.led2.set(1) robot.left_motor.forward(55) robot.right_motor.forward(0) else: robot.led1.set(0) robot.led2.set(0) robot.left_motor.forward(0) robot.right_motor.forward(0) time.sleep(0.05)# Follow the black line — mini## This is what MoBot puts on the board as main.py when you open# 06-ir-line-follow.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Line follower — put the Mini on a black line on white paper.')mobotMINI.initIRs()time.sleep(1)while True: print("[MOBOT]", (''.join([str(x) for x in ['left: ', mobotMINI.left_ir.value(), ' right: ', mobotMINI.right_ir.value()]]))) if mobotMINI.left_ir.value() == 1 and mobotMINI.right_ir.value() == 1: mobotMINI.led1.setState(1) mobotMINI.led2.setState(1) mobotMINI.left_motor.set_direction_and_speed(0, 55) mobotMINI.right_motor.set_direction_and_speed(0, 55) elif mobotMINI.left_ir.value() == 1: mobotMINI.led1.setState(1) mobotMINI.led2.setState(0) mobotMINI.left_motor.set_direction_and_speed(0, 0) mobotMINI.right_motor.set_direction_and_speed(0, 55) elif mobotMINI.right_ir.value() == 1: mobotMINI.led1.setState(0) mobotMINI.led2.setState(1) mobotMINI.left_motor.set_direction_and_speed(0, 55) mobotMINI.right_motor.set_direction_and_speed(0, 0) else: mobotMINI.led1.setState(0) mobotMINI.led2.setState(0) mobotMINI.left_motor.set_direction_and_speed(0, 0) mobotMINI.right_motor.set_direction_and_speed(0, 0) time.sleep(0.05)Try changing it
Section titled “Try changing it”- Slow it down to 35 %. A slow line follower is a good line follower — it wiggles far less.
- Speed it up to 80 % and watch it fly off the corners.
- Give the wheel that is “stopped” a speed of 20 instead of 0: the turn becomes a gentle curve instead of a spin.