Tuesday, 7 March 2017

color probing line follower

COLOR PROBING LINE FOLLOWER


Introduction

Hey! I just made a line following robot. This robot not only follows the black line but can also detect other colors. There is a RGB LED which glows in the color on which the robot is placed. I used the arduino board for this project for simplicity; you can use any other board you are familiar.


 Pre requisite

You should be little familiar with some micro controller. You should have some knowledge of infrared sensors and some fundamentals of electronics.

Components

Arduino board, Color sensor, Infrared sensor, Breadboard, Jumpers, Chassis, Wheels, Motor(150 rpm), Castor wheel, Power supply, L293D module, RGB LED  Multimeter, some Nuts and Screws.

Step1
The first step is to make the connections of arduino with infrared sensor. Before we move to the connections lets first talk about infrared sensors. These sensors work on very simple principal. There is an infrared LED which emits the lights and if the surface is white the light will be reflected back and can be detected by photo diode. This sensor has three pins, 2 for power supply and one for output. This output pin can be connected to any general purpose pin of arduino. The sensor also has to potentiometer to calibrate the proximity range.








Step2
The connection of motor driver and arduino. L293D is used to amplify the signals coming from arduino. The 3 upper most pins of the module are power supply pins. The control signal pins are connected to any general purpose pins of arduino. PWM pins can only be connected to analog pins of the board.











Step 3
The connection of arduino and color sensor.  I used TCS3200 module. You can download the data sheet from this link http://www.mouser.com/catalog/specsheets/TCS3200-E11.pdf   this sensor is very simple to use. The light sensor works by throwing a white light at an object and then recording the reflected color, it can read the intensity. Through red, green and blue color filters the photodiode converts the amount of light to current and this can be read by arduino. The connections of arduino board and RGB sensor can be easily done. The s2 and s3 pins are select lines. By selecting the values of s2 and s3 the value of particular color can be read. S0 and gnd are ground pins.  S1 and vcc are 5v. Pin OE is active low.
The following table is about the selecting lines to read particular color.
S2
S3
Color
LOW
LOW
Red
LOW
HIGH
Green
HIGH
HIGH
Blue

The out pin of the module is used to read the value of three colors.



Three more pins of arduino will be required to connect with RGB LED with particular resistance. Make sure these pins should be connected to the PWM pins of arduino. As we know there is no color which is perfect red , green or blue. So I programmed the sensor to read all the three colors and write that particular value of every color on RGB led by making necessary calculations.







Step 4
Programming
The code is very simple. I have written all the necessary comments to make it more easy to understand.




 int s2=7, s3=8;//select lines
 int outpin=4;
 int r,g,b;
 int rpin=11;
 int bpin=9;
 int gpin=10;

 unsigned int pulsewidth;
 void setup()
{
  Serial.begin(9600);//to activate serial monitor
  pinMode(s2,OUTPUT);//select line
  pinMode(s3,OUTPUT);//select line
  pinMode(outpin,INPUT);//output from rgb sensor 
  pinMode(rpin,OUTPUT);//output to red pin
  pinMode(bpin,OUTPUT);//output to blue pin
  pinMode(gpin,OUTPUT);//output to green pin
  pinMode(2,INPUT);//right sensor
  pinMode(3,INPUT);//left sensor
  pinMode(5,OUTPUT);//right forward
  pinMode(6,OUTPUT);//right backward
  pinMode(13,OUTPUT);//left forward
  pinMode(7,OUTPUT);//left backward
}
void loop()
{
 int a,b;
   a=(digitalRead,3 );//right
   b=(digitalRead,4);//left
   if(a==1 && b==1)//forward
   {
     digitalWrite(5,HIGH);
     digitalWrite(13,HIGH);
     digitalWrite(6,LOW);
     digitalWrite(7,LOW);
   } 
   if(a==0 && b==1)//right turn
   {
     digitalWrite(6,HIGH);
     digitalWrite(13,HIGH);
     digitalWrite(5,LOW);
     digitalWrite(7,LOW);
   }
   if(a==1 && b==0)//left turn
   {
     digitalWrite(7,HIGH);
     digitalWrite(5,HIGH);
     digitalWrite(13,LOW);
     digitalWrite(6,LOW);
   }
   if(a==0 && b==0)//forward
   {
     digitalWrite(13,HIGH);
     digitalWrite(5,HIGH);
     digitalWrite(6,LOW);
     digitalWrite(7,LOW);
   }

   //reading red
  digitalWrite(s2,LOW);
  digitalWrite(s3,LOW);
  pulsewidth = pulseIn(outpin, LOW);
  
  r = pulsewidth/25. -1;
  r = (4080-r);

    //reading green
  digitalWrite(s2,HIGH);
  digitalWrite(s3,HIGH);
  pulsewidth = pulseIn(outpin,LOW);
  
  g = pulsewidth/25. -1;
  g = (4080-g);

    //reading blue
  digitalWrite(s2,LOW);
  digitalWrite(s3,HIGH);
  pulsewidth = pulseIn(outpin,LOW);
  
  b = pulsewidth/25. -1;
  b = (4080-b);
  

if(r>g && r>b && g>b)
{
  r=r;
  g=g/3;
  b=b/7;
 
}

else if(r>g && r>b && b>g)
{
  r=r;
  b=b/3;
  g=g/7;
 
}
else if(g>b && g>r && r>b)
{
  g=g;
  r=r/3;
  b=b/7;
 
}
else if(g>b && g>r && b>r)
{
  g=g;
  b=b/3;
  g=g/7;
 
}
else if(b>g && b>r && r>g)
{
  b=b;
  r=r/3;
  g=g/7;
 
}
else if(b>g && b>r && g>r)
{
  b=b;
  g=g/3;
  r=r/7;
 
}
r=r/16;
g=g/16;
b=b/16;
g=g*0.3;
b=b*0.3;

  Serial.print(r);
  Serial.print("  ,");
  Serial.print(g);
  Serial.print("  ,");
  Serial.println(b);

  analogWrite(rpin,r);
  analogWrite(gpin,g);
  analogWrite(bpin,b);
}

THANK YOU