Line-Following Robot

by yashvi27 in Circuits > Arduino

944 Views, 4 Favorites, 0 Comments

Line-Following Robot

Screenshot 2024-09-28 102955.png
Screenshot 2024-09-28 103622.png

The circuit I created is for a line follower robot that uses Hbridges and a three-way infrared sensor to detect a black line and then send a signal to an Arduino. Next, the Arduino uses the code and output from the sensors to drive the motor. This circuit works because the reflection of light on the white surface is maximum (off) and minimum (on) on the black surface because the black surface absorbs the maximum amount of light. I used this property of light to create a line follower robot that senses a black line and would shift accordingly.

Supplies

3 way IR Sensor

Bread Board

Arduino

4 wheels

4 motors

4 NPN Transistors

4 PNP Transistors

Wires

1K resistors

Build Two H Bridges on Breadboard

Screenshot 2024-09-28 100821.png

Using the following schematic of one H bridge, build two H bridges on your breadboard

Connect Motors

Connect Motors to the H Bridges by connecting the collector of the PNP transistor to one end of the motor and the collector of the NPN transistor to the other end of the motor. Repeat for both H bridges

Connect Arduino Pins

Connect Arduino GND and 5V to GND and 5V on the breadboard respectively. Connect Arduino Pins 6 and 7 to the base of NPN and PNP transistors for the left motor. Connect Pin 8 and 9 to the base of the NPN and PNP transistor for the right motor. Make sure to connect using a current-limiting resistor!

Connect IR Sensor

Screenshot 2024-09-28 102955.png

Connect VCC to 5V on the bread board and GND to ground on the bread board. Connect the rest of the pins to 11,12,10.


NOTE: the image is a rough idea of what your final project should look like.

Downloads

CODE!

Write code by defining Sensor pins and Motor.


SAMPLE CODE IS HERE:

#define MS 12   // Middle Sensor of 3 IR sensor 

#define LS 11   // Left sensor of 3 IR sensor

#define RS 10   // Right sensor of 3 IR sensor

#define LM1 7   // left motor

#define LM2 6   // left motor

#define RM1 9   // right motor

#define RM2 8   // right motor

void setup()

{

  Serial.begin(9600);

  pinMode(MS, INPUT);

  pinMode(LS, INPUT);

  pinMode(RS, INPUT);

// sensor receives input 

  pinMode(LM1, OUTPUT);

  pinMode(LM2, OUTPUT);

  pinMode(RM1, OUTPUT);

  pinMode(RM2, OUTPUT);

// motors output data 

}

// distance and shadows make the sensor detect white as on and black as off.

void loop()

{

  if(!digitalRead(MS))  // Middle Sensor On Line (not on white)

  {

    if(digitalRead(LS) && digitalRead(RS)) //LS and RS not on line (on black)

    {

    Serial.println("move forward"); //used to verify code using serial monitor

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, HIGH);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, HIGH);

    }

    else if(!digitalRead(LS) && digitalRead(RS)) //Sharp Left

    {

    Serial.println("Sharp Left");

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, HIGH);

    digitalWrite(RM1, HIGH); 

    digitalWrite(RM2, LOW);

    }

    else if(digitalRead(LS) && !digitalRead(RS)) //Sharp Right

    {

    Serial.println("Sharp Right");

    digitalWrite(LM1, HIGH);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, HIGH);

    }

    else if(!digitalRead(LS) && !digitalRead(RS)) 

    {

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, LOW);

    Serial.println("Stop");

    }

  }

  else

  {

  if(!digitalRead(LS) && digitalRead(RS))  // Turn left

  {

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, HIGH);

    digitalWrite(RM1, HIGH); 

    digitalWrite(RM2, LOW);

    Serial.println("Left");

  }

  else if(digitalRead(LS) && !digitalRead(RS))  // turn right

  {

    digitalWrite(LM1, HIGH);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW);

    digitalWrite(RM2, HIGH);

    Serial.println("Right");

  } 

    else if(digitalRead(LS) && digitalRead(RS))  // turn left to get back to path

  {

    digitalWrite(LM1, LOW);

    digitalWrite(LM2, LOW);

    digitalWrite(RM1, LOW); 

    digitalWrite(RM2, LOW);

  }

  }

  delay(5);

}