Test #3 of my Arduino Uno controlled line following bot for the upcoming competition. This test was focused on checking how the bot handles 90 degree turns with sharp corners, line intersections and line breaks.
The robot uses 6 TCRT5000 IR reflector sensors positioned in a line.
The distance between the sensors is slightly less than the width of the line, so more than one sensor can detect the line at any given time. This helps to increase the “resolution” and to make more granular steering adjustments, limiting overshooting.
For sharp turns (when the robot “loses” the line temporarily) the robot checks the last reading of the sensors, before it lost the line. If the last reading showed that the line was to the left of the bot, it will spin to the left when it looses the line and vice versa.
Similar approach is is taken for dealing with line breaks – if the robot “lost” the line, but the last sensor reading indicated it was more or less centered on the line before it lost it, the bot assumes there is a line break and continues moving in the same direction.
If 5+ sensors (and a few cases of 4 sensors) detect the line at the same time, the robot assumes it has reached an intersection (the start-finish line, based on the competition rules). If this happens, it will keep going straight.
Initially, I was thinking of using a PID controller, but I quite like how the bot handles right now and will likely stick to the current, less elegant logic. Next steps will be to optimize the layout of the components and tidy up the wiring. Here is a copy of the code:
/* \** Line Follower Basic v. 0.5 ** Last Update: 2013-05-21 */ /* Define motor controll inputs */ const int motorRPin1 = 2; // signal pin 1 for the right motor, connect to IN1 const int motorRPin2 = 3; // signal pin 2 for the right motor, connect to IN2 const int motorREnable = 5; // enable pin for the right motor (needs to be PWM enabled) const int motorLPin1 = 4; // signal pin 1 for the left motor, connect to IN3 (was 5 - need to change) const int motorLPin2 = 7; // signal pin 2 for the left motor, connect to IN4 const int motorLEnable = 6; // enable pin for the left motor (needs to be PWM enabled) /* Define the pins for the IR receivers */ const int irPins[6] = {A0, A1, A2, A3, A4, A5}; /* Define values for the IR Sensor readings */ // an array to hold values from analogRead on the ir sensor (0-1023) int irSensorAnalog[6] = {0,0,0,0,0,0}; // an array to hold boolean values (1/0) for the ir sensors, based on the analog read and the predefined treashold int irSensorDigital[6] = {0,0,0,0,0,0}; // the value above which we determine an IR sensor reading indicates the sensor is over a line int treashold = 700; // binary representation of the sensor reading from left to right int irSensors = B000000; // sensors detecting the line int count = 0; // a score to determine deviation from the line [-180 ; +180]. Negative means the robot is left of the line. int error = 0; // store the last value of error int errorLast = 0; // a coorection value, based on the error that is used to change motor speed with PWM int correction = 0; // keep track of the laps int lap = 0; /* Set up maximum speed and speed for turning (to be used with PWM) */ // PWM to control motor speed [0 - 255] int maxSpeed = 255; /* variables to keep track of current speed of motors */ int motorLSpeed = 0; int motorRSpeed = 0; void setup() { /* Set up motor controll pins as output */ pinMode(motorLPin1,OUTPUT); pinMode(motorLPin2,OUTPUT); pinMode(motorLEnable,OUTPUT); pinMode(motorRPin1,OUTPUT); pinMode(motorRPin2,OUTPUT); pinMode(motorREnable,OUTPUT); /* Set-up IR sensor pins as input */ for (int i = 0; i < 6; i++) { pinMode(irPins[i], INPUT); } /* Change the PWM frequency of digital pins 5 and 6 (timer0) to Phase-correct PWM of 31.250 kHz from the default of ~500Hz. Using code from Adjusting PWM Frequencies http://playground.arduino.cc/Main/TimerPWMCheatsheet This requires a separate change in the wiring.c function in the Arduino program files hardware\arduino\cores\arduino\wiring.c from: #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) to: #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(1 * 510)) Without the change to wiring.c time functions (millis, delay, as well as libraries using them will not work corectly. */ TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00); TCCR0B = _BV(CS00); } void loop() { Scan(); UpdateError(); UpdateCorrection(); Drive(); } void Scan() { // Initialize counters, sums etc. count = 0; irSensors = B000000; for (int i = 0; i < 6; i++) { irSensorAnalog[i] = analogRead(irPins[i]); if (irSensorAnalog[i] >= treashold) { irSensorDigital[i] = 1; } else {irSensorDigital[i] = 0;} Serial.print(irSensorAnalog[i]); Serial.print("|"); count = count + irSensorDigital[i]; int b = 5-i; irSensors = irSensors + (irSensorDigital[i]«b); } } void UpdateError() { errorLast = error; switch (irSensors) { case B000000: if (errorLast < 0) { error = -180;} else if (errorLast > 0) {error = 180;} break; case B100000: // leftmost sensor on the line error = -150; break; case B010000: error = -90; break; case B001000: error = -30; break; case B000100: error = 30; break; case B000010: error = 90; break; case B000001: // rightmost sensor on the line error = 150; break; /* 2 Sensors on the line */ case B110000: error = -120; break; case B011000: error = -60; break; case B001100: error = 0; break; case B000110: error = 60; break; case B000011: error = 120; break; /* 3 Sensors on the line */ case B111000: case B011100: error = -150; break; case B000111: case B001110: error = 150; break; /* 4 Sensors on the line */ case B111100: error = -150; break; case B111010: error = -150; break; case B001111: error = 150; break; case B010111: error = 150; break; /* 5 Sensors on the line */ case B111110: error = -150; break; case B011111: error = +150; break; case B111111: lap = lap + 1; // increment laps when start/finish line is crossed error = 0; break; default: error = errorLast; } } void UpdateCorrection() { if (error >= 0 && error < 30) { correction = 0; } else if (error >=30 && error < 60) { correction = 15; } else if (error >=60 && error < 90) { correction = 40; } else if (error >=90 && error < 120) { correction = 55; } else if (error >=120 && error < 150) { correction = 75; } else if (error >=150 && error < 180) { correction = 255; } else if (error >=180) { correction = 305; } if (error <= 0 && error > -30) { correction = 0; } else if (error <= -30 && error > -60) { correction = -15; } else if (error <= -60 && error > -90) { correction = -40; } else if (error <= -90 && error > -120) { correction = -55; } else if (error <= -120 && error > -150) { correction = -75; } else if (error <= -150 && error > -180) { correction = -255; } else if (error <= -180) { correction = -305; } if (correction >= 0) { motorRSpeed = maxSpeed - correction; motorLSpeed = maxSpeed; } else if (correction < 0) { motorRSpeed = maxSpeed; motorLSpeed = maxSpeed + correction; } } void Drive() { if (motorRSpeed > 255) {motorRSpeed = 255;} else if (motorRSpeed < -255) {motorRSpeed = -255;} if (motorLSpeed > 255) {motorLSpeed = 255;} else if (motorLSpeed < -255) {motorLSpeed = -255;} if (motorRSpeed > 0) { // right motor forward (using PWM) analogWrite(motorREnable, motorRSpeed); digitalWrite(motorRPin1, HIGH); digitalWrite(motorRPin2, LOW); } else if (motorRSpeed < 0) {// right motor reverse (using PWM) analogWrite(motorREnable, abs(motorRSpeed)); digitalWrite(motorRPin1, LOW); digitalWrite(motorRPin2, HIGH); } else if (motorRSpeed == 0) { // right motor fast stop digitalWrite(motorREnable, HIGH); digitalWrite(motorRPin1, LOW); digitalWrite(motorRPin2, LOW); } if (motorLSpeed > 0) { // right motor forward (using PWM) analogWrite(motorLEnable, motorLSpeed); digitalWrite(motorLPin1, HIGH); digitalWrite(motorLPin2, LOW); } else if (motorLSpeed < 0) { // right motor reverse (using PWM) analogWrite(motorLEnable, abs(motorLSpeed)); digitalWrite(motorLPin1, LOW); digitalWrite(motorLPin2, HIGH); } else if (motorLSpeed == 0) { // left motor fast stop digitalWrite(motorLEnable, HIGH); digitalWrite(motorLPin1, LOW); digitalWrite(motorLPin2, LOW); } }
Comments
Comment by Wagner on 2013-05-23 08:12:15 -0700
It’s running very smoothly and fast! I’m in catch up mode now 🙂
I’m also dropping the PID, it seems too much complication for a not so great gain…
Nice work
Comment by paijo londo on 2014-02-06 18:11:14 -0700
i’ts great.. i’m done compiling
i’m using arduino ERW 1.0.5 and i can’t change the core? can you give me any sugestion?
Comment by Stan on 2014-02-07 17:48:33 -0700
Sorry, not sure I understand what you mean by “I can’t change the core”.
Comment by tapan on 2014-03-06 12:07:55 -0700
which motors of what rpm did u use….and wouldn’t it be better to use the digitalised output of sensors via comparators
Comment by Stan on 2014-03-10 14:55:45 -0700
I used motors like these: 6V DC 300 RPM rated motors from eBay.
Comment by JD on 2014-07-10 02:45:36 -0700
how do you change it into white line follower? I just couldn’t figure out what to do..
Comment by Stan on 2014-07-12 12:23:53 -0700
If you follow a white line on a dark background, you will get zeroes from the sensors over the line, and ones from the sensors over the dark background. Then you need to change the code in the switch / case statements accordingly. Hope that helps!
Comment by Janaradhan on 2014-07-14 16:34:26 -0700
yeah, that helped a lot.
thank you.
Comment by Abhi on 2014-07-14 16:59:23 -0700
If I use a 5 pair TCRT sensor array instead of six sensors, how much will it affect the speed of the bot. The line to be followed is 2.5 cm wide
I’ve already got a 5 pair TCRT5000 sensor array with digital output and want to use the same for the bot.
Comment by Rahul on 2014-08-05 05:21:27 -0700
Hello, Abhi, I modified the above code to work with 5 pair TCRT5000 sensor ( digital)….Instead of using the
‘irsensorAnalog[]’ array, i used just the ‘irsensorDigital[]’ array to store the values…..but unfortunately, my bot is unable to follow line correctly….i slowed down my bot enough, but still the problem persists….I checked everything….dont know whats the matter with that..
Comment by JD on 2014-09-11 01:18:22 -0700
Hello Stan,
I want to build it to follow a 2.5cm wide line.How much gap or distance should I have between each sensor to make it work best….
Comment by Stan on 2014-09-14 09:39:58 -0700
With digital reads, I find that having two sensors on the line helps to improve the “resolution”. So I would put them 1.5 – 2 cm apart. Then you can have a case where only a single sensor is on the line, or when two sensors are over the line.
Comment by Andrew on 2014-09-22 04:59:54 -0700
Hey there, thats an amazing post you’ve put up here 🙂 Genius work!
Can you pls help me with this? I have Pololu 8RC Sensors.. How can I fit that in here with the code that you’ve given?
pls help. Competition tomorrow.
Thanks in advance.
Comment by JD on 2014-09-22 20:13:40 -0700
Can you tell me what value of resistors did you use across the ir led and the photo transistor.
I can’t get good sets of analog readings when I used 10k resistor across the photo transistor and 270R resistor across the ir led.
Also, what should be the appropriate distance between the sensor array and the wheels.
Comment by aftab on 2014-10-03 19:02:22 -0700
well i would have liked to know about the deltailed steps you followed for the successful completion of the robot.
Comment by JD on 2014-11-04 23:25:34 -0700
Stan, please help..
I have a national robotics competition next week.
I’ve already made the line follwer robot and its working fine but I just want to ensure that its perfect…
So, can u tell me what should be appropriate distance between the sensor array an the wheels..
The robot has to follow an U-shaped whte path..
Comment by Stan on 2014-11-05 09:46:24 -0700
JD, I do not know what the optimal distance would be for your robot – trial and error is your best bet. In my case, having the sensors in the front and wheels at the back worked best. I am thinking that this set-up allows for a bit more time to react on turns. Also, having the bulk of the robot weight directly above the wheels helps with stability. Good luck on the competition and post back on how well you did!
Comment by JD on 2014-11-05 20:09:45 -0700
Thank u, Stan.
Comment by nilesh on 2015-01-09 22:13:48 -0700
the pwm part is not working properly
below the value 100 motors are not moving… plz help
Comment by Stan on 2015-01-12 17:21:46 -0700
Hi Nilesh, the usable PWM range when using the analogWrite() Arduino command for DC motors will never be the full 0-255. You need to figure out the minimum threshold, where your motor essentially provides insufficient torque to move your robot. Seems like you have hit that limit at around 100.
Comment by carlos on 2015-10-20 22:15:34 -0700
Hello
Please tell me how I can know the position of the center of a set of eight sensors. Everything works fine, but the robot is swinging left-right, stopped.
Thank you
Carlos
Comment by yangmike on 2016-02-04 23:05:48 -0700
hi , could you tell me why change the pwm frequency?
can i not change it?
Comment by yangmike on 2016-02-04 23:08:20 -0700
why dont u use odd numbers of ir ?
Comment by Stan on 2016-02-06 17:19:28 -0700
I have read that this would give better PWM control of the DC motors. I can’t say that I have noticed significant improvement, so this step is certainly optional.
Comment by Stan on 2016-02-06 17:20:44 -0700
Certainly possible, the key is the spacing between the IR receivers, compared to the width of the line.
Comment by Parth_R_Vaghela on 2016-02-21 11:32:25 -0700
hello. Can I Get Schematic for this project.
Comment by Parth_R_Vaghela on 2016-02-21 11:33:32 -0700
I am having BO MOTOR 150 rpm. Cani use it ?
Comment by Renu kshirsagar on 2016-02-28 06:19:14 -0700
hi, can we replace TCRT5000 with analog ir sensor like this one http://www.engineersgarage.com/sites/default/files/imagecache/Original/wysiwyg_imageupload/1/avr-analog-comparator-circuit.gif
Comment by Burhan on 2016-03-05 00:30:55 -0700
Nice Project, can you share its schematic diagram..
Comment by nijeesh joshy on 2016-03-10 07:34:37 -0700
i am getting thia error please help
Arduino: 1.6.4 (Windows 7), Board: “Arduino Due (Programming Port)”
kk.ino: In function ‘void setup()’:
kk:83: error: ‘TCCR0A’ was not declared in this scope
kk:83: error: ‘COM0A1’ was not declared in this scope
kk:83: error: ‘_BV’ was not declared in this scope
kk:83: error: ‘COM0B1’ was not declared in this scope
kk:83: error: ‘WGM01’ was not declared in this scope
kk:83: error: ‘WGM00’ was not declared in this scope
kk:84: error: ‘TCCR0B’ was not declared in this scope
kk:84: error: ‘CS00’ was not declared in this scope
‘TCCR0A’ was not declared in this scope
This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.
Comment by Faris on 2016-04-05 13:49:43 -0700
I am using 3 TCRT 5000 for my line follower. However it has A0 and D0 which I use and can u provide a code for if possible.
Thank you
Comment by sithira on 2016-04-14 02:55:45 -0700
hi, please tell me how to check the line sensor. nice project…
Comment by Jaid on 2016-06-18 18:49:00 -0700
Hey guys, does anyone know how I could make this work with an Infrared Line Track Follower Sensor? I’ve tried everything that I can think of but I’m not very good at this. Please help me.
Comment by Bawantha on 2016-10-08 06:50:46 -0700
Hi stan …,
I’ve made a line follower robot from your code..
Its working fine… but some times it misbehave..
It doesn’t follow the line correctly.. can you plz give a solution for my question plz reply zoooon…
Your project is awsome….
Thankyou
Comment by imakerobots on 2016-10-11 23:26:35 -0700
Hi Bawantha, you should modify some of the settings to match your hardware. Performance will depend on many factors, like the motor speed and torque, positioning of the sensors, distribution of the weight on the robot etc…Good luck!
Comment by sajjad on 2016-10-26 06:59:13 -0700
what will be the code if i use 6 TCRT5000 digital sensor array having .6 inch gap between each sensor and the black line is 2 inch in width? please help its urgent.
Comment by Kent Britania on 2017-01-05 09:31:20 -0700
what will be the code if Im gona use adafruit motorshield V1? How do I change the Code? Email me pls. I really needed this.
Comment by Sheroz Sharipov on 2017-03-24 04:40:43 -0700
Please Help. QTR8A I have this Sensor and how I can Use this code ? What should I chanche here ?
Comment by ASIF KHAN on 2017-04-11 07:44:33 -0700
how these two line work
can u explain me
irSensors = B000000;
irSensors = irSensors + (irSensorDigital[i]«b);
Comment by Stan on 2017-04-13 23:18:29 -0700
Hi Asif, the code in that section essentially loops through all the infrared sensor readings and sets the corresponding bit for the sensor in irSensors to either 1 or 0, based on the sensor reading. This reference article should help: http://playground.arduino.cc/Code/BitMath
Comment by abubakar on 2017-04-16 12:43:32 -0700
i m using 6 ir sensor modules..not tcrt500.but the robo is not working…
can u please sugusgt me solution …plzzz
Comment by Iftekhar Alam JOY on 2017-07-12 14:02:56 -0700
How can I reduce motor speed? it is running too fast?
Comment by Prakash on 2017-08-11 23:41:47 -0700
How to give connections to Arduino , L298, motors etc…??
Comment by henry on 2017-11-08 09:41:22 -0700
haii, can i get the way to calculate the numbers you show in your program
Comment by zeeshan on 2017-11-13 13:39:22 -0700
hey stan, can you pls send me its schematic diagram
Comment by Marc Domini Nas on 2019-03-18 05:34:16 -0700
Can I get the schematic diagram of your project?