Ajaya Dahal
Published

Simple Line Follower

Arduino using IR sensor for basic line follower.

BeginnerProtip3 hours1,643
Simple Line Follower

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
L239d motor driver
×1
SparkFun RedBot Sensor - Line Follower
SparkFun RedBot Sensor - Line Follower
×2
DC motor (generic)
×2
9V battery (generic)
9V battery (generic)
×1
Chassis
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Demo

Schematics

Code

Basic Line follower

C/C++
/*Author: Ajaya Dahal
 * created on: 08/20/2018
*/


/*-------definning Inputs------*/
#define leftSensor 2      // left sensor
#define rightSensor 3      // right sensor

/*-------definning Outputs------*/
#define leftMotor 4       // left motor
#define leftMotor2 5       // left motor
#define rightMotor1 6       // right motor
#define rightMotor2 7       // right motor

void setup()
{
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);
  pinMode(leftMotor, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
}

void loop()
{
  if(digitalRead(leftSensor) && digitalRead(rightSensor))     // Move Forward
  {
    digitalWrite(leftMotor, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
  }
  
  if(!(digitalRead(leftSensor)) && digitalRead(rightSensor))     // Turn right
  {
    digitalWrite(leftMotor, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, HIGH);
    digitalWrite(rightMotor2, LOW);
  }
  
  if(digitalRead(leftSensor) && !(digitalRead(rightSensor)))     // turn left
  {
    digitalWrite(leftMotor, HIGH);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
  }
  
  if(!(digitalRead(leftSensor)) && !(digitalRead(rightSensor)))     // stop
  {
    digitalWrite(leftMotor, LOW);
    digitalWrite(leftMotor2, LOW);
    digitalWrite(rightMotor1, LOW);
    digitalWrite(rightMotor2, LOW);
  }
}

Credits

Ajaya Dahal
11 projects • 14 followers
Electrical and Computer Engineering Certified Part 107 FAA Drone Pilot

Comments