How I built a Line Following Arduino Robot

Sitting at a teacher conference one day, my booth partner asked me how long it would take me to build a line following arduino robot. I looked around at the parts we had on hand: an arduino UNO, a pile of 10k resistors and photovoltaic resistors, continuous servos, coffee cups, wire, tape, and a nine volt battery.

I said “an hour.”

He said “Prove it.”

To build the robot I knew I needed two eyes (photoresistors) that would change the speed of motors (continuous servos) to make the machine zigzag down a line on the floor. If I could get the robot to talk to me and tell me what was going on inside then I could find and fix problems faster than blind guessing. I already had Arduino software on my computer and a USB cable, so I plugged in the Arduino board and wrote the first sketch.

setup() {
  Serial.begin(57600);
  Serial.println("** READY **");
}

loop() {}

When I open the serial window in Arduino and I change the Baud rate to 57600 I saw ** READY **. Success!

Next step was to attach and test each eye. I wired 5v power through the 10k resistor resistor, which then split into two directions: one went to A0, the other went through the photoresistor and into GND. In theory when a light shone in the photoresistor more power would flow to GND than A0 and the value read at A0 would drop. So a high value from A0 would mean black under the sensor. I build a second “eye” connected to A1 and then changed the code.

setup() {
  Serial.begin(57600);
  Serial.println("** READY **");
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
}

loop() {
  Serial.print(analogRead(A0));
  Serial.print('\t');
  Serial.println(analogRead(A1));
}

The serial window now looks like this:

0    4
1    3
1    3 
1    2
2    2

Now when I put my finger over an eyeball the numbers go up.

1023    4
1022    3
1019    3 
1019    2
1012    2

Success number 2! Also I know now the numbers go from 0 to 1023.

Then I tried writing some code to move wheels. I set the speed of each wheel based on the value of the photoresistor on the same side of the machine. If the left eye sees black, slow the left wheel. If the right eye sees black, slow the right wheel. Servos in Arduino use a standard library with two important calls: attach() lets the program know where I hooked up the servo, and write() sets the speed of a continuous servo. 0 means full backward speed, 180 is full forward.

#include <Servo.h>

Servo s0,s1;

setup() {
  Serial.begin(57600);
  Serial.println("** READY **");
  digitalWrite(A0,HIGH);
  digitalWrite(A1,HIGH);
  s0.attach(6);
  s1.attach(7);
}

loop() {
  int left = analogRead(A0);
  int right = analogRead(A1);
  Serial.print(left);
  Serial.print('\t');
  Serial.println(right);
  s0.write(90+(left*90.0/1024.0));
  s1.write(90-(right*90.0/1024.0();
}

Notice that sneaky “-” in the second last line? that makes the s1 servo turn the opposite direction. The two servos are mounted facing opposite directions, so when one goes forward, the other goes backward. I cut the bottoms off some coffee cups to use as wheels, put everything on a piece of cardboard, wrapped it up in masking tape, and added the 9v battery to power the robot. I put it down on the floor, plugged it in, and…

[products skus=’ELEC-0086, ELEC-0029, ELEC-0004, MOTO-0008, ELEC-0002, ELEC-0018′]