Today you will teach your robot
to stay on the line.
By the end of this lesson, your bot will follow a curvy black line on its own using two IR sensors that see the difference between black and white.
Grab these from your kit.
All shipped with your Pro planTwo eyes. One simple rule.
Your robot has two IR sensors pointing down at the ground. Each one sends back a number.
LOW when it sees black on the line.
HIGH when it sees white on the floor.
If the left eye sees black, the line drifted left, so the bot turns left. If the right eye sees black, it turns right. If both see white, it drives straight.
Follow along, tick as you go.
Stick the sensors under the chassis.
Peel the double-sided tape from each IR sensor. Stick them under the front of the chassis, about 1 cm apart and 5 mm above the ground.
- The two little LEDs on each sensor should face downward.
- Don't worry about being perfect. You will fine-tune in Step 4.
Wire it up.
Use the jumper wires to connect the parts. Match the colors to the diagram above. Keep the battery unplugged while you wire.
- Left IR sensor -> A0 on Arduino
- Right IR sensor -> A1
- Left motor -> D5 (PWM)
- Right motor -> D6 (PWM)
- Both sensors and motors share GND and 5V
Drop in the code.
Use the code editor below. Start with the Blocks view if you are newer, then peek at the Python version to see what the blocks become.
- If you get a red error, check that the right COM port is selected.
- Buddy AI can explain syntax errors in kid-friendly language.
Calibrate the sensors.
Place the bot on a white surface and adjust each sensor until the readout is near 200. Then put it on the black line. The readout should jump toward 900.
- Turn the screw clockwise to increase the reading.
- Both sensors should hit similar numbers on the same color.
Race it.
Place your bot at the start of the test track. Plug in the battery, watch it follow the curves, then time it and try to beat 12 seconds.
- Too jittery? Lower the motor speed in the code.
- Losing the line? Move the sensors closer to the ground.
- Going backwards? Swap the motor wires.
One file. Two ways to read it.
line_follower.ino · saved 14s agoNo kit yet? Test it virtually first.
Auto-runs your code above🏁 Test track · Snake
00:08.42Make sure it stuck.
+30 XP if you get it first tryFilm it. Submit it. Earn the badge.
Record a 15-second clip of your bot finishing the test track. Your coach can review the clip and unlock the next robotics challenge.