Introduction: [Arduino] Linefollower With Photoresistors


Intro:
Hello!
I built a linefollower robot that could be used in many different ways, and I summed up my project in some steps.
To begin with, all you need is the following parts:

Parts List
-Robot frame
-Two wheels
-Two DC motors
-Mini breadboard
-Two leds
-Two photoresistors
-Two 56k resistors
-Two 330 resistors
-Some wire
-Diodes (if not already included into the motor controller)
-Motor controller(i used L298)
-Big breadboard
-Three 1.5v battery
-Arduino UNO

Step 1: Robot Frame

Parts List:
-Robot frame
-Two wheels
-Two DC motors

How to:
I used an old robot frame with two wheels commanded by two DC motors, but you can use an equivalent solution as well.

Step 2: Sensor Ir + Photoresistor

Parts List:
-Mini breadboard
-Two leds
-Two photoresistors
-Two 56k resistors
-Two 330 resistors
-Some wire

How to:
Calculate the distance between the two pairs of leds and photoresistors basing on the width of your tape.
Position the photoresistors and the leds as seen in the pictures.
Connect leds to two resistors of 330 ohm and put some non trasparent tape around it to prevent any interference with the photoresistors
Place the mini breadboard in the lower part of the frame.

Step 3: Motor Controller

Parts List:
-Some wire
-Diodes (if not already included into the motor controller)
-Motor controller
-Big breadboard
-Three 1.5v batteries

Sensors Assembling:
Plug in the input pins of bridge A on pins 11 and 12 of Arduino.
Plug in the input pins of bridge B on pins 6 and 5 of Arduino.
Connect with some diodes the output pins of each bridge to motors, if needed.
Connect the logic supply voltage (Vss) to 5v of Arduino and connect the ground of motor controller to the gnd of arduino.
Connect the supply voltage (Vs) to 3 AA batteries.

Step 4: Programming Arduino

Parts List:
-Arduino UNO
-Some wire

Use this code:
int PinSens_A = A5;
int PinSens_B = A4;
int ValSens_A = 0;
int ValSens_B = 0;
int Ris;

int PinMotorA_1 = 10;
int PinMotorA_2 = 11;
int PinMotorB_1 = 5;
int PinMotorB_2 = 6;

int SpeedMotor_A;
int SpeedMotor_B;

int difference = 0;

int RoL(int A, int B)
{
  if(A > B)
    return 1;
  if(B > A)
    return 2;
  if (A = B)
    return 0;
}

void Follow(int val, int speedMax)
{
  if(val == 2)
  {
    SpeedMotor_A = speedMax;
    SpeedMotor_B = 0;
  }
  if(val == 1)
  {
    SpeedMotor_A = 0;
    SpeedMotor_B = speedMax;
  }
}

int Move(int dir)
{
  if(dir == 0)
  {
    analogWrite(PinMotorA_1, SpeedMotor_A);
    analogWrite(PinMotorB_1, SpeedMotor_B);
  }
  if(dir == 1)
  {
    analogWrite(PinMotorA_2, SpeedMotor_A);
    analogWrite(PinMotorB_2, SpeedMotor_B);
  }
}

int calcDiff()
{
  ValSens_A = analogRead(PinSens_A);
  ValSens_B = analogRead(PinSens_B);
  if(ValSens_A > ValSens_B)
  {
    int diff = ValSens_A - ValSens_B;
    return diff;
  }
  if(ValSens_A < ValSens_B)
  {
    int diff = ValSens_B - ValSens_A;
    return diff;
  } 
}

void Debug()
{
  Serial.print(Ris);
  Serial.print("      ");
  Serial.print(ValSens_A);
  Serial.print("      ");
  Serial.print(ValSens_B);
  Serial.println("      ");
}

void setup()
{
  Serial.begin(9600);
  pinMode(PinMotorA_1, OUTPUT);
  pinMode(PinMotorA_2, OUTPUT);
  pinMode(PinMotorB_1, OUTPUT);
  pinMode(PinMotorB_2, OUTPUT);

  pinMode(PinSens_A, INPUT);
  pinMode(PinSens_B, INPUT);


}
void loop()
{
  if(difference == 0)
  {
    difference = 1;
  }
  ValSens_A = analogRead(PinSens_A);
  ValSens_B = analogRead(PinSens_B)+150; //use a variable number to balance the sensors
  Ris = RoL(ValSens_A, ValSens_B);
  Follow(Ris, 100);
  Move(0);
  Debug();
};

Step 5: Testing

Finally, test your configuration for best performance.


Robot Challenge

Participated in the
Robot Challenge