Line follower robot (Type: programmed with PIC 18F4550)

In previous tutorial, i explained you from scratch about a line follower robot. The logic behind it, the principal, Mechanic formation, motor and sensor caliberating and i also showed you a working model of line follower robot, which was not very flexible with turbulant nature of environment. It has some weakness and drawbacks like it was required to caliberated before eunning, it needed to be caliberated every time in every different ambient light. And most importantly, it was unable to make good decisions accept following an continous smooth black line over white or lighter background. 

To fill all these gaps in the robot which we made last time, I will now introduce one of my friend "PIC 18F4550 MCU" from Microchip. Yes! he will solve all of these problems, which i have mentioned above. So lets start with sensor part. Before we start, if you want to get familiar with this development board of PIC 18F series, please click here. This board is designed and developed by me and has various interesting features.

In sensor connection also, in past we were restricted to use only 2  IR sensor module, but now in this case, we can use any amount of sensor module depending upon what quality and speed, if need and how much ADC pins are available freely on this chip. This is true, that more IR sensors are there on  line follower robot to detect the line, more accurate and error free robot will be. You can also use higher RPM motors to run robot fast. 

In this tutorial, we will only use 3 IR sensor modules to keep it basic. Main aim to use more than 2 sensors is to eliminate the intense need of caliberating IR powered sensor everytime. Previosuly, with 2 sensors, we were just getting the value of sensor and comparing it with a caliberated value, it was greater, we ran the motor and if it was lesser than that value, then we were stopping the motor on that side. Now, with three sensor, we will compare the value comming from each sensor, or each sensor with a caliberated value. In this tutorial, we will follow the later solution, because for the former solution (which eliminated the need of caliberation completely) is only successfull, if the mechanical chassis is made in such a way that IR sensor will not be effected by SUN and much intense IR light in some Bulbs. Because high IR disturbance can make a system very unstable that even PIC will not be able to handle it.

Hence, there will be still a need of caliberating, but its done only once after construction of robot with new sensors and that too only for ambient light error correction. But this line follower robot will be really very smooth. Reason is in the analysis of the states of robot.

After the change in number of IR sensors, there is also changes in the  states of robots. like to run:

Robot forward before, both sensor were required to be on both side of the black line, but now logic is, middle sensor value lesser and sensor on the sides.

Robot left/right turn before,  one sensor had to be on black line. But now, its two sensor on black line.

How this change will help?

when you saw the video of working old linefollower, you must have noticed the zigzag effect. This is the effect when a line follower robot's path correcting loop corrects the path, when robot is going out of line in a zigzag manner or damping style. This happens due to the fact that line follower with 2 sensors does not stop the turning of robot at right time and it leads to overcorrection and hence opposite side corrects and again it leads to overcorrection.

With 3 sensors, there is no such problem because of the middle sensor which stops the correcting code at right time, before robot crosses to opposite side. ALso, you will notice that in comparator based line follower, robot is always in hurry means it is always moving and correcting, which is also good and also bad. But we do not have control over it because there is no option to change that. But in PIC based or any other Mcu based, we can program it such a way that robot will first correct itself during going wrong and then move forward.

Basicle, now we can run motors as we want. we can also reverse them in some state. But i wont go into that detail now. You can see all these effects which i mentioned you in the video. This robot would have run so nice but i knowingly have run it on a line whoes thickness is changed after programming this robot, so that you can see the correcting effect.

Code explaination


I have made a robotic car which functions for 10 types of autonomous behaviour (all close to each other logically) for example: light detection, line follower, lighter line follower, obstacle avoidance and so on. From them, today i will tell you about darker line follower robot. you can find code for this multi-behavioural robot on my git repository for pic codes with name All_in_one_robot.c

Now let's discuss the line follower code. In this image, you can see that i have called useSensor function made by me for opening ADC channels. Here, top three lines in lo=4 is responsible for opening adc at adc_ch0, 1 and 2.

Please do not get confuse due to extra calling statements, these are for some other sensors. As i told you already that its code for several behavier with same logic. 

Next, we have the functional routins which get us the value of ADC of different sensors.  Here, firstly we are setting adc channel to particular channel connected to that sensor physically, then we call convert(), wat for completion and then save the value for further use in the main function.

Main logic is in side while loop, where we have different comparision of these sensor. Here i have not used saved value instead i have used int returning function with same defination, so here you can see that i have coded different actions with different value pattern of sensor as described above. I can't explain complete code here, but i think that once you will have a look over the code on my git repository, you will get an idea of logic implementation. at each function, i have also a portion of self caliberation code for sensors and it also indicates the caliberated states by green LEDs.

I hope that this article helped you in making your own PIC powered line follower robot. If still, there are  somethings unclear, feel free to contact me at my contact page or comment below.