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 program
Section titled “The program”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
- Print to serial (create text with
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Open 06-line-follow.mobot, plug in, press Send to Device.
- Put the robot on the paper, straddling the line, with the middle sensor over the black.
- Watch LED 1 and the serial monitor.
What you should see
Section titled “What you should see”- The serial monitor prints
middle sensor (OUT3) = 0or= 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.
Why does it stop so easily?
Section titled “Why does it stop so easily?”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.
The blocks
Section titled “The blocks”| 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 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 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 Pinimport 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)# Follow the line — robotics## This is what MoBot puts on the board 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.
from machine import Pinimport 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(mobotROBOTICS.irs.readSingleIR(2)))) if mobotROBOTICS.irs.compIRs([0,0,1,0,0]): mobotROBOTICS.led1.setState(1) mobotROBOTICS.left_motor.setDirectionAndSpeed(0, 40) mobotROBOTICS.right_motor.setDirectionAndSpeed(0, 40) else: mobotROBOTICS.led1.setState(0) mobotROBOTICS.left_motor.setDirectionAndSpeed(0, 0) mobotROBOTICS.right_motor.setDirectionAndSpeed(0, 0) time.sleep(0.2)Try changing it
Section titled “Try changing it”- 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
ifblocks: ifRead IR OUT2is 1, slow the left wheel; ifRead IR OUT4is 1, slow the right one. Now it hunts along the line instead of stopping.