I have to build a line following robot that will be able to detect a selected colored line on the floor and start following it.
How do color sensors do this after detecting the specific colored line?
It seems like you are asking how to do the line following, so here's one way to think about it:
Once you have found the line, one way to follow it is to follow one of the edges of the line. For example: following the right edge could be done like this:
If you have two color sensors (left and right) and the left sensor is on the line while the right is not, continue forward.
If the right sensor or both sensors detect the line, turn right slightly until the robot is back on the right edge.
If the left sensor doesn't detect the line and the right doesn't detect the line, the robot has gone off the right side of the line. Turn left.
This is a common problem in First Lego League, and, while legos are simple, the approach would hold for many other environments. Here is a video showing a way to follow the middle of the line using two color sensors. And here is an article showing the principles of how to do it.
In short, the algorithm is like this (assuming you are following the right side of the line)
As you drive, continually measure the color, assume you should be measuring half way between "the line" and "not the line".
It might require a little bit of soldering to create such a color sensor, but you can probably handle it.
The easiest way to create a color sensor for such an application is by using a reflective optical sensor such as the following: Optical Sensor
It is basically just a light detector and a LED. The light emitted from the LED gets reflected by the surface and hits the detector.
The amount of reflected light is dependent on many things, but mainly on the color of the surface. If the surface is black all the light will be absorbed and the detector won't see any light. If the surface is white the detector will see a lot of light and so on.
To create an linetracking sensor for the robot i suggest that you use several (e.g. 6) of these sensors and place them pointing downwards on the front bumper of the robot. You can now detect where the line is by looking at the individual sensors and detecting which one (or more) sees less or more light.
This will give you a relatively cheap sensor that should be sufficient for a line tracking robot.
The downside is however that it requires a bit of soldering. On google i have found the following diagram that you might use.