## How to Make a Line Following Robot

So you’ve just finished your Arduino Starter Kit and now you’re wondering “what next?”  Let’s take what you’ve learned and put it into a line following robot.

## Get the kit

Want all the bits & bob to follow along? Get the kit here.

Got the tools to make the parts? Here are the open hardware laser cutting plans.

## Top down design

I’m going to start with some high level goals about what this robot should do (A).  Then I’ll work with what I’ve got (B) and try to build a map from B to A.  When I know how far I have to go I can make a time estimate, a cost projection, and so on.

The goal is build an inexpensive robot that can follow a black line on a white floor.  I’d like to use the same robot later to solve a maze.  The robot should stop if both eyeballs see black.

I see robots as a kind of living thing.  They all have a brain, skeleton, muscles, sensors, nerves, and a power source.  I’m pretty sure I’ll need two wheels (muscle) and two eyeballs (sensors).  Normally both wheels will roll forward.  The brain will know that if the left eye sees black, slow down the left wheel.  If the right eye sees black, slow down the right wheel.   The difference in the wheel speeds is called differential steering.

## Bottom up design

Now that I know where I want to go, I can look at what I have and figure out a route.  Working from the bottom-up I’m going to experiment with each part of the robot so that I’m sure I understand how it works, and then I can put all the pieces together.

### Brain & Power

For the brain I’m going to an Arduino UNO I have on hand.  I’m going to use a 9v battery for the power supply.

### Sensors

For the eyes I’m going to use some photoresistors.  Photoresistors are resistors that block the flow of electricity less when light is shining on the built-in sensor.  When there is black in front of the sensor it will resist a lot of current.  When there is white in front the sensor won’t resist and should allow more current.  We can test this pretty easily by putting one end of a photoresistor in the 5v pin of the UNO and the other end in A0.

[code]void setup() {
Serial.begin(57600);
Serial.println("START");
}

void loop() {
delay(50);
}[/code]

If you upload this code to your arduino and open the serial window, you should see numbers crawling up the screen.  When you cover the sensor the numbers will drop towards zero, and when you shine a light on the sensor the numbers should climb towards 1024.  That number is a measure of the amount of current coming from the 5v pin, through the sensor, and into the A0 pin.

I’m going to build my circuit with five eyes so that later I can make my robot solve a maze.

### Muscles

The arduino starter kit comes with 180 degree servos.  I want wheels that keep turning, so I’m going to get a pair of 5v continuous rotation servos.  From an arduino you can move a servo with ease.  Each servo has three pins: ground, power, and signal.  Try this code on your Arduino:

[code]#include <Servo.h>

#define LED 13

Servo s;

void setup() {
Serial.begin(57600);
Serial.println("START");
pinMode(LED,OUTPUT);  // the led built into the Arduino
s.attach(6);
}

void loop() {
int i;

digitalWrite(LED,HIGH);  // on
for(i=0;i<180;++i) {
s.write(i);
delay(10);
}

digitalWrite(LED,HIGH);  // off
for(i=180;i>0;–i) {
s.write(i);
delay(10);
}

Serial.println("done");
}[/code]

Connect your servo power to 5v, your servo ground to GND, and your servo signal to digital pin 6.  Always change wiring with the power OFF.

This code will make a 180 servo sweep back and forth.  The same code will make a continuous servo go from full speed backward to full speed forward and back again.

Try changing loop() to make the continuous servo hold steady in the middle.

[code]#include <Servo.h>

#define NO_MOVE 90

Servo s;

void setup() {
s.attach(6);
}

void loop() {
s.write(NO_MOVE);
}[/code]

### Skeleton

I’m very lucky to have access to a laser cutter.  I’ve designed some parts like so:

The lines of grey dashes are 5cm apart.  The blue parts are etched so that I don’t attach all the bits on the wrong side of the wood.  I cut them all from 1/8″ birch ply.

The five photoresistors go through the thin strips on the left and then through the big plate.

I used zip ties to hold almost everything together in this model.  Glue is no fun if you have to take something apart again.  Screws are expensive.

The same zip ties that go through the big plate and the thin strips also go around the ends of the servos to hold them in place.

The wheels I made by gluing one of each type of disc together so that the holes lined up.  Then I put an X shaped servo horn – one of the connector pieces that came with the servo – in the x shaped hole of the wheel and tied it down with two zip ties.  If the zip ties aren’t good enough I’ll try glue, too.

The tail is the triangular piece.  I glued it in, but you can also use zip ties to hold it onto the back of the robot on the bottom (blank side) of the big plate.  The tail means less robot dragging on the ground, which is probably too much work for the motors.

### Nerves

Here’s a fritzing picture I made of the wiring.  Click for larger versions.

Correction: J1 goes to the right motor and J2 goes to the left.

If you look at the very top photo in this post you can see I used rainbow-colored ribbon cable to keep the wires more organized.  I also used 15mm male-male headers (the kind with long pins on both sides) to secure the servo wires and the ribbon cable to the breadboard.  The 9v battery has a male plug and I have a female plug that goes into the breadboard.

You might notice there’s a power plug on the Arduino and yet nothing is connected.  The Arduino is getting power through vin and returning through GND.  I haven’t tried to flip it around and plug the battery into the arduino, then use 5v/GND to the breadboard.  It might be one less part!

When the motors start to move they try to suck too much power from the circuit, which makes the photoresistor values go down when they shouldn’t.  The capacitor battery stores up just enough to help the servos without interfering.  On the part of the circuit for each servo there’s a blue tube.  that’s a 1 uf (microfarad) capacitor.  It’s a tiny battery.  There’s also and a 10 ohm resistor.  When the motors don’t need a lot of power the current flows through the resistor and the battery charges up.  When the motor starts to move the power needs spike for a fraction of a second and the battery helps out.

## Next step

Now our robot is completely assembled.  The next step is to write the program that will teach the robot how to follow the line.

As this post is getting really long I’m going to put the code in the next post.  See if you can use the code samples above to figure it out on your own.

### 3 Responses to “How to Make a Line Following Robot”

1. Johanna says:

Hey Dan,

Could you share the completed program for the line following robot?

Thanks!

• Dan says:

The code will be shared (and explained!) in tomorrow’s blog post.

2. […] « How to Make a Line Following Robot […]