My line following robot from Bajdi on Vimeo.
A couple of months ago I started building a line following robot, I named it Volga 🙂 The plan was to draw a small track and plot it out on a large piece of paper and make a robot that could follow the track. In this case the plan was successful as tonight my little robot has been doing some laps around my test track without needing my intervention. I’m pleased with the result 🙂
Because I designed a special PCB for this simple robot it took me so long to finish it. Volga is controlled by an Arduino Nano plugged on my “Nano undershield”. Volga has 2 continuous rotation servos and the line following sensor is made up of 5 TCRT5000 sensors. I originally started with 3 TCRT5000 sensors. This worked but sometimes the robot would run of the track at 90° corners. I originally started programming the line following code on my own using lots of if functions. You can see a small video of my results with 3 sensors here.
Chris from Rocket brand studios then gave me a link to an Arduino sketch with some line following code in it. His code uses 5 sensors so I decided to add 2 more TCRT5000 line sensors. I had a good look at the sketch and adapted it to my robot, the result you can see in the above video. The robot is a bit spastic as it changes direction but it keeps following the black line pretty well.
This robot is easy to replicate and not so hard to code, line following is actually pretty straight forward to program. The whole robot was made and programmed in only a couple of hours, spread out over 3 months 🙂 It’s also a cheap project. The chassis is a plastic box I found, I bought the 2 continuous rotation servos and wheels on Ebay for a very reasonable price. I’m especially proud on my self designed Nano Undershield and the line following sensor board which I also made myself. The TCRT5000 sensors can be found on Ebay for only a couple of dollars or you can by them from Yourduino if you prefer a webshop.
Here is the sketch I used in the top video:
// Volga: the cheap and simple line following robot // http://www.bajdi.com // With the help from Chris @ http://rocketbrandstudios.com/ // 2 continuous rotation servos // 5 TCRT5000 sensors connected to analog inputs = line following sensor #include <Servo.h> Servo leftservo; // create servo object to control a servo Servo rightservo; // create servo object to control a servo int rawtcrt[5]; //these are the raw ADC values from the line follow sensor /* rawtcrt[0] = left sensor rawtcrt[1] = left middle sensor rawtcrt[2] = middle sensor rawtcrt[3] = right middle sensor rawtcrt[4] = right sensor */ int bwtcrt[5]; //these are basically booleans we will use 0 for white and 1 for black int i; int colorThreshold = 400; // < 400 = white / > 400 = black void setup() { leftservo.attach(2); // attaches the servo on pin 2 to the servo object leftservo.write(90); // stop servo rightservo.attach(3); // attaches the servo on pin 3 to the servo object rightservo.write(90); // stop servo } void loop() { checkLineSensor(); //1 = seeing black 0 = seeing white forward(); if (bwtcrt[1]==1) { forwardleft(); } if (bwtcrt[3]==1) { forwardright(); } if (bwtcrt[4]==1) { right(); } if (bwtcrt[0]==1) { left(); } } void checkLineSensor() { for (i=0;i<5;i++) { rawtcrt[i]=analogRead(i); if (rawtcrt[i]<colorThreshold) { bwtcrt[i]=0; } else{ bwtcrt[i]=1; } } } void forward(){ leftservo.write(70); // go straight forward rightservo.write(110); } void forwardleft(){ leftservo.write(85); // go forward left rightservo.write(102); } void forwardright(){ leftservo.write(78); // go forward right rightservo.write(95); } void left(){ leftservo.write(110); // go left rightservo.write(110); } void right(){ leftservo.write(70); // go right rightservo.write(70); }
Hi there,
I am trying to make a line following robot that steers with one servo motor attached to a wheel in the front of the robot. How would I go about making this one servo turn according to what the sensors see? I will start out using 3 of them.
I was thinking of something that basically says if the outside sensors read white, servo should be at 90degrees. If the left and middle read white and the right reads black, servo should be at 65degrees, etc. The only problem is I don’t know how to code this!
Also, I don’t see in your code where you specify what pins the sensors are connected to. Isn’t that necessary??
Any advice is greatly appreciated 🙂
You don’t need to specify analog pins. The checkLineSensor() function has a for loop that reads the 5 tcrt5000 sensors.
hi, I am making a line following robot but my TCRT5000 does not detect a line, the analog value does not change when the line sees the black line. it stays at around 540.. please help me!
Have you seen this: http://www.bajdi.com/analogread-from-a-tcrt5000-sensor/ Check every sensor separate, you might need to change the resistor values to get better readings.
hello!
can use this code for arduino uno?
i’m making a project for my robotics class.
Here are the things i used.
1. Aruino Uno
2. 2 TCRT5000 IR Sensors
3. 2 Modified Servos
Yes it should work perfectly on an Arduino Uno.