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

Arduino Nano Line Follower Robot with Obstacle Avoidance and PID Control

View and Edit Project In Cirkit DesignerImage of Arduino Nano Line Follower Robot with Obstacle Avoidance and PID Control

Circuit Documentation

Summary

This document provides a detailed overview of a line follower robot with obstacle avoidance capabilities. The robot uses an 8-array IR sensor to follow a line and an ultrasonic sensor to detect and avoid obstacles. The control logic is implemented using an Arduino Nano microcontroller. The robot also features two pushbuttons for calibration and starting the robot, and a blue LED to indicate calibration status. The motors are controlled via an H-bridge motor driver.

Component List

  1. Arduino Nano

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
  2. H-Bridge Motor Driver (Ponte H)

    • Description: Motor driver for controlling two DC motors.
    • Pins: OUT4-Motor2, OUT3-Motor2, OUT2-Motor1, OUT1-Motor1, 5V, GND, 12V, IN3, IN1, IN2, IN4
  3. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor for obstacle detection.
    • Pins: VCC, TRIG, ECHO, GND
  4. DC Mini Metal Gear Motor

    • Description: Small DC motor with metal gears.
    • Pins: IN1, IN2
  5. 8-Array IR Sensor

    • Description: Infrared sensor array for line following.
    • Pins: GND, IR, D1, D2, D3, D4, D5, D6, D7, D8, VCC
  6. LED: Two Pin (Blue)

    • Description: Blue LED for indicating calibration status.
    • Pins: cathode, anode
  7. Pushbutton

    • Description: Pushbutton for user input.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  8. Battery 12V

    • Description: 12V battery for powering the circuit.
    • Pins: +, -
  9. Rocker Switch

    • Description: On/Off switch for the circuit.
    • Pins: 1, 2

Wiring Details

Arduino Nano

H-Bridge Motor Driver (Ponte H)

HC-SR04 Ultrasonic Sensor

DC Mini Metal Gear Motor

Another DC Mini Metal Gear Motor

8-Array IR Sensor

LED: Two Pin (Blue)

Pushbutton

Another Pushbutton

Battery 12V

Rocker Switch

Code Documentation

/*
 * Line Follower Robot with Obstacle Avoidance
 * This Arduino sketch uses a PID algorithm to follow a line using an 8-array IR sensor.
 * It also uses an ultrasonic sensor to detect and avoid obstacles.
 * Two buttons are used: one for calibration and one to start the robot.
 * During calibration, a blue LED is lit.
 */

#include <PID_v1.h>

// Pin definitions
#define IR_PIN A0
#define TRIG_PIN A7
#define ECHO_PIN A6
#define LED_PIN 4
#define CALIBRATE_BUTTON_PIN 2
#define RUN_BUTTON_PIN A3
#define MOTOR1_IN1 9
#define MOTOR1_IN2 6
#define MOTOR2_IN3 5
#define MOTOR2_IN4 3

// PID parameters
double setpoint = 0, input = 0, output = 0;
PID myPID(&input, &output, &setpoint, 2, 5, 1, DIRECT);

// Function prototypes
void calibrateSensors();
void followLine();
void avoidObstacle();

void setup() {
  pinMode(IR_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(CALIBRATE_BUTTON_PIN, INPUT);
  pinMode(RUN_BUTTON_PIN, INPUT);
  pinMode(MOTOR1_IN