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

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

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

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control a robot using an Arduino UNO microcontroller. The circuit includes various sensors, motor drivers, and motors to enable the robot to detect obstacles and follow lines. The Arduino UNO is programmed to read sensor inputs and control the motors accordingly.

Component List

  1. L298N DC Motor Driver

    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
    • Description: Motor driver used to control the speed and direction of the motors.
  2. IR Sensor (Right)

    • Pins: out, gnd, vcc
    • Description: Infrared sensor used to detect the presence of a line.
  3. IR Sensor (Left)

    • Pins: out, gnd, vcc
    • Description: Infrared sensor used to detect the presence of a line.
  4. Motor with Reducer (Right)

    • Pins: 3 - 6 VCC, GND
    • Description: Motor with a gear reducer to control the speed and torque.
  5. Motor with Reducer (Left)

    • Pins: 3 - 6 VCC, GND
    • Description: Motor with a gear reducer to control the speed and torque.
  6. Arduino UNO

    • Pins: SDA, AREF, GND, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, A5, A4, A3, A2, A1, A0, Vin, 5V, 3.3V, RESET, IOREF, SCL
    • Description: Microcontroller used to control the entire circuit.
  7. Micro Servo 9G

    • Pins: GND, +5V, PWM
    • Description: Small servo motor used for precise control of angular position.
  8. HC-SR04 Ultrasonic Sensor

    • Pins: VCC, TRIG, ECHO, GND
    • Description: Ultrasonic sensor used to measure distance to obstacles.
  9. 4 x AAA Battery Mount

    • Pins: -, +
    • Description: Battery holder for four AAA batteries to power the circuit.
  10. Bluetooth Module HM-10

    • Pins: BRK, RX, TX, GND, VCC, State
    • Description: Bluetooth module used for wireless communication.

Wiring Details

L298N DC Motor Driver

IR Sensor (Right)

IR Sensor (Left)

Motor with Reducer (Right)

Motor with Reducer (Left)

Arduino UNO

Micro Servo 9G

HC-SR04 Ultrasonic Sensor

4 x AAA Battery Mount

Bluetooth Module HM-10

Documented Code

#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 180

//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

void setup()
{
  //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  //For that we need to increase the frequency of analogWrite.
  //Below line is important to change the frequency of PWM signal on pin D5 and D6
  //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  //This sets frequecny as 7812.5 hz.
  TCCR0B = TCCR0B & B11111000 | B00000010 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT);
  pinMode(IR_SENSOR_LEFT, INPUT);
  rotateMotor(0,0);   
}

void loop()
{
  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);

  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW)
  {
    rotateMotor(MOTOR_SPEED, MOTOR_SPEED);
  }
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW )
  {
      rotateMotor(-MOTOR_SPEED, MOTOR_SPEED); 
  }
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH )
  {
      rotateMotor(MOTOR_SPEED, -MOTOR_SPEED); 
  } 
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed