Robotics
Build an Arduino line follower: eyes, knob, kill switch
To build an Arduino line follower with a speed knob and a kill switch, wire two downward-facing reflectance sensors either side of the line, a potentiometer for speed and a push-button for stop and go, then write one loop() with no delay() that reads all four inputs every pass and decides, in order: stopped, line on the left, line on the right, or straight on.
The wiring
- Eyes: two IR reflectance sensors on A0 (left) and A1 (right), reading about 937 over a pale floor and 250 over a black line
- Speed knob: a potentiometer across 5 V, its wiper on A2
- Kill switch: a push-button from pin 2 to GND, with
INPUT_PULLUP - Wheels: a motor driver taking PWM from pins 9 (left) and 10 (right)
Mount the eyes a few millimetres above the floor, a little more than the line's width apart, so that with the robot centred both see floor and the line runs between them. Print their readings over floor and line before writing any rules: the threshold has to sit between the two.
Plan the program
- The toggle: flip
stoppedon the change from HIGH to LOW, and remember the reading inlast. - The speed:
map(analogRead(A2), 0, 1023, 0, 255). - The eyes: read both, every pass.
- The decision, in order: stopped first, then each line rule, then straight.
The code
bool now = digitalRead(2);
if (now == LOW && last == HIGH) stopped = !stopped;
last = now;
int speed = map(analogRead(A2), 0, 1023, 0, 255);
int left = analogRead(A0);
int right = analogRead(A1);
if (stopped) { // the kill switch beats everything
analogWrite(9, 0); analogWrite(10, 0);
} else if (left < 512) { // line under the left eye: swing left
analogWrite(9, 0); analogWrite(10, speed);
} else if (right < 512) { // line under the right eye: swing right
analogWrite(9, speed); analogWrite(10, 0);
} else { // centred: straight on
analogWrite(9, speed); analogWrite(10, speed);
}
To swing left, stop the left wheel and run the right: the rover pivots round its stopped wheel and its nose swings back toward the line. Every running wheel uses speed, so half a turn of the knob gives half-speed straights and half-speed turns.
Live experiment
Drive the finished runner
This is the finished line runner on its course. Press KILL to set it off, turn the speed knob, and tick off the four checks: full speed with floor under both eyes, a swing left, a swing right at half speed, and a press of KILL that stops both wheels.
“Project: Line runner” is the project that ends Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Test it like a checklist
- Floor under both eyes, knob full: both wheels at full.
- Left eye on the line: swings left.
- Right eye on the line, knob at half: swings right at half speed.
- Press KILL: both wheels stop, and stay stopped until the next press.
Once it runs, the three rules will zigzag down the track. Steering in proportion to the difference between the eyes is the upgrade that makes it glide.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Project: Line runner” is the project that ends Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Left 0, right at the knob's speed. Swinging left means the left wheel stops and the right runs, and in this rover a running wheel runs at speed, the knob's value, not a fixed 255.
- Why not “Both 0”? Both wheels off is the kill switch's job. The rover should be turning back toward the line.
- Why not “Both at the knob's speed”? Both wheels running means straight ahead. With the line under the left eye the rover has to swing left.
- Why not “Left 0, right 255”? In this rover a running wheel runs at the knob's speed. A fixed 255 would ignore the knob in every turn.
Show the answer
Answer: First, so a stop beats every line rule. The first matching branch runs. With the kill test first, stopped means both wheels off whatever the eyes see.
- Why not “In setup()”? setup() runs once at power-up. The kill switch has to be checked on every pass.
- Why not “Inside the straight branch”? Inside the straight branch it would only stop the rover when it's centred on the line, not while it's turning.
- Why not “Last”? Last, it would only run when no line rule matched, so a stopped rover would still follow the line.
Show the answer
Answer: Left wheel 0, right wheel 255. The stopped wheel is the one the rover pivots around. Stop the left and the rover's nose swings left.
- Why not “Left wheel 255, right wheel 0”? Stopping the right wheel and running the left swings the nose right.
- Why not “Both 0”? Both wheels stopped doesn't turn the rover at all.
- Why not “Both 255”? Both wheels running drives straight ahead.
“Project: Line runner” is the project that ends Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.
Questions people ask
What parts do I need for an Arduino line follower?
An Arduino, two infrared reflectance sensors such as the TCRT5000, two small gear motors with wheels, a motor driver that takes PWM, a potentiometer for speed, a push-button, a battery pack and a caster or skid at the front. Black electrical tape on a pale floor makes a good track.
Why use the knob's speed in the turns, not 255?
So the whole robot slows down together. If the turns always ran a wheel at 255, turning the knob down would slow the straights but not the swings, and the robot would lurch on every correction.
How do I pick the threshold of 512?
Print both sensors' readings on the serial monitor over the floor and over the line, then set the threshold about halfway between. 512 suits sensors that read about 937 on the floor and 250 on the line.
How do I make it follow the line more smoothly?
Replace the three rules with proportional steering: work out how far off the line it is from the difference between the two eyes, and slow one wheel and speed the other in proportion. It glides instead of zigzagging.
Related topics
- How a line follower robot worksTwo eyes, a threshold and three rules, then smoother steering in proportion.
- How to make an Arduino toggle button (a robot kill switch)One press stops, the next press goes: an edge, a bool, and a stop that always wins.
- How the Arduino map() function worksRescale a reading from one range to another, and the traps that come with it.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.