Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based Line Following Robot with IR Sensors and Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Line Following Robot with IR Sensors and Motor Driver

Circuit Documentation

Summary

This circuit is designed to control a pair of DC Mini Metal Gear Motors using an Arduino UNO microcontroller and a TB6612FNG Motor Driver. The circuit also includes multiple IR sensors that provide input to the Arduino for processing and decision-making. The Arduino is powered by a 7.4V power source, which also supplies power to the motor driver. The IR sensors are powered by the 5V output from the Arduino. The motors are controlled by PWM signals from the Arduino, which are fed to the motor driver to control the speed and direction of the motors.

Component List

Arduino UNO

IR Sensors

TB6612FNG Motor Driver

7.4V Power Source

DC Mini Metal Gear Motors

Wiring Details

Arduino UNO

IR Sensors

TB6612FNG Motor Driver

DC Mini Metal Gear Motors

Documented Code

const int m11 = 4;
const int m12 = 2;
const int m21= 7;
const int m22 = 8;
const int m1pwm = 6; // PWM pin for left motor
const int m2pwm = 3; // PWM pin for right motor

float kP = 250; 
int Speed = 160; 
int kD = 0.0;
int last_val;

void setup() {
    Serial.begin(9600);
    pinMode(m11, OUTPUT);
    pinMode(m12, OUTPUT);
    pinMode(m21, OUTPUT);
    pinMode(m22, OUTPUT);
    pinMode(m1pwm, OUTPUT);
    pinMode(m2pwm, OUTPUT);
}

void loop() {
    int a0 = (analogRead(0) < 500 ? 0 : 1);
    int a1 = (analogRead(1) < 500 ? 0 : 1);
    int a2 = (analogRead(2) < 500 ? 0 : 1);
    int a3 = (analogRead(3) < 500 ? 0 : 1);
    int a4 = (analogRead(4) < 500 ? 0 : 1);
    int a5 = (analogRead(5) < 500 ? 0 : 1);

    int s_mode = digitalRead(12);

    int error = (a0 * 4.5) + (a1 * 3.5) + (a2 * 2.5) - (a3 * 2.5) - (a4 * 3.5) - (a5 * 4.5);

    int prop = map(error,-5.0,5.0,-kP,kP);

    int correction = prop + (error-last_val)*kD ;
    last_val = error;
    
    int leftMotorSpeed = constrain(Speed - correction, 0, 255);
    int rightMotorSpeed = constrain(Speed + correction, 0, 255);

    analogWrite(m1pwm, leftMotorSpeed);
    analogWrite(m2pwm, rightMotorSpeed);

    if (s_mode==0){
       Speed = 155; 
    }else{
      Speed = 200; 
      }
      
    digitalWrite(m11, LOW);
    digitalWrite(m12, HIGH);
    digitalWrite(m21, LOW);
    digitalWrite(m22, HIGH);
}

This code is responsible for reading the IR sensor values and adjusting the speed of the motors accordingly. The analogRead function is used to read the sensor values, which are then processed to calculate an error value. This error is used to adjust the motor speeds to keep the system on track. The digitalRead function checks the state of pin D12 to switch between two speed modes. The analogWrite function is used to control the speed of the motors through PWM signals.