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

Arduino UNO IR Sensor-Controlled Robot with L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO IR Sensor-Controlled Robot with L298N Motor Driver

Circuit Documentation

Summary

This document describes a robot control circuit using an Arduino UNO, an L298N motor driver, two DC motors, two IR sensors, and a 12V battery. The robot moves forward when no obstacle is detected by the IR sensors. If an obstacle is detected by the left IR sensor, the robot turns right. If an obstacle is detected by the right IR sensor, the robot turns left.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver for controlling DC motors.
    • 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
  3. DC Motor (Motor 1)

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  4. DC Motor (Motor 2)

    • Description: Standard DC motor.
    • Pins: pin 1, pin 2
  5. Battery 12V

    • Description: 12V power supply.
    • Pins: +, -
  6. IR Sensor (Left)

    • Description: Infrared sensor for obstacle detection.
    • Pins: out, gnd, vcc
  7. IR Sensor (Right)

    • Description: Infrared sensor for obstacle detection.
    • Pins: out, gnd, vcc

Wiring Details

Arduino UNO

L298N DC Motor Driver

DC Motor (Motor 1)

DC Motor (Motor 2)

Battery 12V

IR Sensor (Left)

IR Sensor (Right)

Code Documentation

/*
 * IR ROBOT
 * This Arduino sketch controls a robot using two IR sensors and an L298N motor driver.
 * The robot moves forward when no obstacle is detected by the IR sensors.
 * If an obstacle is detected by the left IR sensor, the robot turns right.
 * If an obstacle is detected by the right IR sensor, the robot turns left.
 */

// Pin definitions
const int irSensorLeft = 4;
const int irSensorRight = 7;
const int motorEnableA = 3;
const int motorEnableB = 5;
const int motorIn1 = 11;
const int motorIn2 = 10;
const int motorIn3 = 9;
const int motorIn4 = 6;

void setup() {
  // Initialize IR sensor pins as inputs
  pinMode(irSensorLeft, INPUT);
  pinMode(irSensorRight, INPUT);
  // Initialize motor control pins as outputs
  pinMode(motorEnableA, OUTPUT);
  pinMode(motorEnableB, OUTPUT);
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);
  // Start with motors disabled
  digitalWrite(motorEnableA, LOW);
  digitalWrite(motorEnableB, LOW);
}

void loop() {
  // Read IR sensor values
  int leftSensorValue = digitalRead(irSensorLeft);
  int rightSensorValue = digitalRead(irSensorRight);
  // Determine motor actions based on sensor values
  if (leftSensorValue == HIGH && rightSensorValue == HIGH) {
    // No obstacle, move forward
    moveForward();
  } else if (leftSensorValue == LOW) {
    // Obstacle on left, turn right
    turnRight();
  } else if (rightSensorValue == LOW) {
    // Obstacle on right, turn left
    turnLeft();
  }
}

void moveForward() {
  digitalWrite(motorEnableA, HIGH);
  digitalWrite(motorEnableB, HIGH);
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
}

void turnRight() {
  digitalWrite(motorEnableA, HIGH);
  digitalWrite(motorEnableB, HIGH);
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, HIGH);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
}

void turnLeft() {
  digitalWrite(motorEnableA, HIGH);
  digitalWrite(motorEnableB, HIGH);
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, HIGH);
}

This code initializes the IR sensors and motor control pins, reads the sensor values, and controls the motors to move the robot forward, turn right, or turn left based on the sensor inputs.