# Need Some help for a line follower algorithm

Hello everyone. This June I am participating in a robotics competition(1st time). I am willing to play 3 events. Thus I need an algorithm for a advance line follower for this kind of path. And please tell me how many IR sensor should I use for a 2.5cm (width) line. The problem statement is
1> Follow a white line on black background.
2> Stop for 10 seconds at the white strips on the line.
3> Light up an LED after reaching finishing block.
The example path is in the attachment.

minimal number of sensors is 3 but you better use 5 or 7 .

robtillaart: minimal number of sensors is 3 but you better use 5 or 7 .

Can u tell me the algorithm to be used to follow the path ????

This is a pretty common robotics task... there must be stuff out there on the net. Search around first.

I would use two photo diodes or photo resister sensors on the bottom pointed toward the canter. robtillaart says use more but I,m not sure what that would gain you, but I never built a line follower.

You will need a central light source to illuminate the line, but shielded from the sensors. Connect the sensors to the analog inputs and then program the device to turn right or left trying to minimize the difference between the sensors... This is where more sensors might be better, but tougher logic. You have plenty of analog inputs so that is not a factor.

When you come to a perpendicular white line the reading on both sensors will jump. you have to figure out how much. program the device to stop if it sees a big jump in both sensors, and wait 10 seconds. Then you must tell it that the the next thing it must do is go forward a certain amount so it can find the line again, or go till both sensors see a big drop, then just repeat till you hit the last white space.

If the larger white space is surrounded by black space, you can tell the device to turn on the LED if both sensors drop abruptly, and they can't find the white line again. If it is not, or it is against a wall you will have to count the time it takes to cross it and use that to tell you if you are done.

One way to do this is to count the time it takes to cross the other white spaces and store it. If the last white space is > twice the small spaces then stop & light up the LED.

The code is pretty straight forward logic, even though there is a lot of it.

I would hope they allow you to set up a prototype track so you can try out different approaches. Sounds like fun.

Is there a time limit on how fast the race must be run? if not, go slow and add in a lot of averaging code to smooth out the ride and not lose the line.

RPCoyle: This is a pretty common robotics task... there must be stuff out there on the net. Search around first.

I would use two photo diodes or photo resister sensors on the bottom pointed toward the canter. robtillaart says use more but I,m not sure what that would gain you, but I never built a line follower.

You will need a central light source to illuminate the line, but shielded from the sensors. Connect the sensors to the analog inputs and then program the device to turn right or left trying to minimize the difference between the sensors... This is where more sensors might be better, but tougher logic. You have plenty of analog inputs so that is not a factor.

When you come to a perpendicular white line the reading on both sensors will jump. you have to figure out how much. program the device to stop if it sees a big jump in both sensors, and wait 10 seconds. Then you must tell it that the the next thing it must do is go forward a certain amount so it can find the line again, or go till both sensors see a big drop, then just repeat till you hit the last white space.

If the larger white space is surrounded by black space, you can tell the device to turn on the LED if both sensors drop abruptly, and they can't find the white line again. If it is not, or it is against a wall you will have to count the time it takes to cross it and use that to tell you if you are done.

One way to do this is to count the time it takes to cross the other white spaces and store it. If the last white space is > twice the small spaces then stop & light up the LED.

The code is pretty straight forward logic, even though there is a lot of it.

I would hope they allow you to set up a prototype track so you can try out different approaches. Sounds like fun.

Is there a time limit on how fast the race must be run? if not, go slow and add in a lot of averaging code to smooth out the ride and not lose the line.

ya i am getting it. I think the time counting will be good to differentiate between the white strips and the end of line. Thanks for the idea. yes there is time limit. The bot completes the line in less time wins.

If u have any other idea then please tell me.

you need to be able to see the line (white) and the left (black) and right (black) ==> that makes three

a possible algorithm in plain English:

``````If you have 5 sensors the task is to kee the white spot in sensor 3
if the white spot moves away to:
- 2 you must turn to the left
- 1 you must turn harder to the left
- 4 turn to the right
- 5 turn to the right harder

If white spot is nowhere seen, start a search for the line
- Archimedes curve - relative easy
- Hilbert curve - more difficult but serious fun
- Random - easy

If you need to be recognize crossings
- multiple sensors will go white ==> that is where 5 is the minimum you need to detect that there is a lane to the left and right at the same time
``````

Your turn to convert this algorithm in Arduinese

Thanks for the replies. I will make another thread after finishing the robot. Meanwhile if u have any other ideas please post.
Thank you

if you need more ideas please search the forum ;)

robtillaart: you need to be able to see the line (white) and the left (black) and right (black) ==> that makes three

Hello Rob,

I started working with Arduino and line following last weekend, so my amount of experience isn't very large yet. But so far, I have already managed to create a line-following sensor out of three normal red LEDs. The middle one illuminates the line, and the left and right one detect how much light comes to the left and right (LEDs are light-sensitive themselves). I thus have only two sensors. I can detect a black line of 15 mm width from a distance of 5 cm (optimum distance: 1-3 cm), with a horizontal resolution of better than 1 mm. (going 1 mm to the left or right from the center will give me a clear proportional signal). I haven't tested in direct sunlight yet; I think that may give some trouble. But light from lamps or windows is no problem at all.

The sensors are spaced 30 mm apart, so I will not lose the line very easily. Minimum line width that I was able to detect reliably was 5 mm. The sensor works best with a black line on white background, but I have also tested successfully with yellowish tape (schilderstape) on a more or less dark wooden floor. Last weekend I haven't found time to connect the motors and construct a cart (I use servo motors that I modify for continuous rotation). Hopefully, I will get to this in the coming weekends.

Would the 30 mm dstance between the sensors be adequate for sharper turns at somewhat higher speed? I know that, with these motors, speed will not be very high yet (+/- 1 RPM max, 6 cm wheels), but I'd like to be prepared. I expect that, with proper PID control, following smooth turns will go smoothly (as I'm able to keep the line exactly centered between the sensors with only small corrections)

All in all I think that, when you use analog sensors with a wide dynamic range, you should be able to create a line follower with only two sensors.

Hey tomriddle!

i got this code just lying around in my laptop maybe you can use it or derive some lines from here:

#define lights 9
int LDR1, LDR2, LDR3; // sensor values
// calibration offsets
int leftOffset = 0, rightOffset = 0, centre = 0;
// pins for motor speed and direction
int speed1 = 3, speed2 = 11, direction1 = 12, direction2 = 13;
// starting speed and rotation offset
int startSpeed = 70, rotate = 30;
// sensor threshold
int threshhold = 5;
// initial speeds of left and right motors
int left = startSpeed, right = startSpeed;
// Sensor calibration routine
void calibrate() {
for (int x=0; x<10; x++) { // run this 10 times to obtain average
digitalWrite(lights, HIGH); // lights on
delay(100);

leftOffset = leftOffset + LDR1; // add value of left sensor to total
centre = centre + LDR2; // add value of centre sensor to total
rightOffset = rightOffset + LDR3; // add value of right sensor to total
delay(100);
digitalWrite(lights, LOW); // lights off
delay(100);
}
// obtain average for each sensor
leftOffset = leftOffset / 10;
rightOffset = rightOffset / 10;
centre = centre /10;
// calculate offsets for left and right sensors
leftOffset = centre - leftOffset;
rightOffset = centre - rightOffset;
}
void setup()
{
// set the motor pins to outputs
pinMode(lights, OUTPUT); // lights
pinMode(speed1, OUTPUT);
pinMode(speed2, OUTPUT);
pinMode(direction1, OUTPUT);
pinMode(direction2, OUTPUT);
// calibrate the sensors
calibrate();
delay(3000);
digitalWrite(lights, HIGH); //lights on
delay(100);
// set motor direction to forward
digitalWrite(direction1, HIGH);
digitalWrite(direction2, HIGH);
// set speed of both motors
analogWrite(speed1,left);
analogWrite(speed2,right);
}
void loop() {
// make both motors same speed
left = startSpeed;
right = startSpeed;
// if LDR1 is greater than the centre sensor + threshold turn right
if (LDR1 > (LDR2+threshhold)) {
left = startSpeed + rotate;
right = startSpeed - rotate;
}
// if LDR3 is greater than the centre sensor + threshold turn left
if (LDR3 > (LDR2+threshhold)) {
left = startSpeed - rotate;
right = startSpeed + rotate;
}
// send the speed values to the motors
analogWrite(speed1,left);
analogWrite(speed2,right);
}

Hope this helps!