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

Arduino Nano-Powered PID Line Following Robot with Reflectance Sensor Array and Dual Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino Nano-Powered PID Line Following Robot with Reflectance Sensor Array and Dual Motor Driver

Circuit Documentation

Summary

This circuit is designed for an advanced line-following robot that uses a PID control algorithm for precise movement. The core components include a QTRX-HD-07RC Reflectance Sensor Array for line sensing, a Motor Driver 1A Dual TB6612FNG for controlling two DC Mini Metal Gear Motors, and an Arduino Nano microcontroller for processing the sensor data and controlling the motors. The circuit is powered by a 5V battery for the logic and a 12V battery for the motor power.

Component List

QTRX-HD-07RC Reflectance Sensor Array

Motor Driver 1A Dual TB6612FNG

Arduino Nano

5V Battery

DC Mini Metal Gear Motor (x2)

12V Battery

Wiring Details

QTRX-HD-07RC Reflectance Sensor Array

Motor Driver 1A Dual TB6612FNG

Arduino Nano

DC Mini Metal Gear Motors

5V Battery

12V Battery

Documented Code

/*
 * Arduino Sketch for advanced line following using PID control
 *
 * Components:
 * - QTRX-HD-07RC Reflectance Sensor Array
 * - Motor Driver 1A Dual TB6612FNG
 * - Two DC Mini Metal Gear Motors
 * - Arduino Nano
 *
 * Connections:
 * - Reflectance Sensor Array to Arduino Analog Pins A0-A5
 * - Motor Driver to Arduino Digital Pins D3-D9
 * - Power connections as per the provided net list
 */

// Pin definitions
const int sensorPins[] = {A0, A1, A2, A3, A4, A5};
const int motorPWMA = 3;
const int motorAIN2 = 4;
const int motorAIN1 = 5;
const int motorSTBY = 6;
const int motorBIN1 = 7;
const int motorBIN2 = 8;
const int motorPWMB = 9;

// PID control variables
float Kp = 0.5;
float Ki = 0.0;
float Kd = 0.1;
float lastError = 0;
float integral = 0;

void setup() {
  // Initialize sensor pins
  for (int i = 0; i < 6; i++) {
    pinMode(sensorPins[i], INPUT);
  }
  // Initialize motor driver pins
  pinMode(motorPWMA, OUTPUT);
  pinMode(motorAIN2, OUTPUT);
  pinMode(motorAIN1, OUTPUT);
  pinMode(motorSTBY, OUTPUT);
  pinMode(motorBIN1, OUTPUT);
  pinMode(motorBIN2, OUTPUT);
  pinMode(motorPWMB, OUTPUT);
  // Enable motor driver
  digitalWrite(motorSTBY, HIGH);
}

void loop() {
  // Read sensor values
  int sensorValues[6];
  for (int i = 0; i < 6; i++) {
    sensorValues[i] = analogRead(sensorPins[i]);
  }
  // Calculate error
  int error = (sensorValues[0] * -2 + sensorValues[1] * -1 + sensorValues[2] * 0 +
               sensorValues[3] * 0 + sensorValues[4] * 1 + sensorValues[5] * 2);
  // PID calculations
  integral += error;
  float derivative = error - lastError;
  float output = Kp * error + Ki * integral + Kd * derivative;
  lastError = error;
  // Calculate motor speeds
  int baseSpeed = 150;
  int leftMotorSpeed = baseSpeed + output;
  int rightMotorSpeed = baseSpeed - output;
  leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);
  rightMotorSpeed = constrain(rightMotorSpeed, 0, 255);
  // Set motor speeds
  analogWrite(motorPWMA, leftMotorSpeed);
  analogWrite(motorPWMB, rightMotorSpeed);
  // Set motor directions
  digitalWrite(motorAIN1, leftMotorSpeed > 127 ? HIGH : LOW);
  digitalWrite(motorAIN2, leftMotorSpeed > 127 ? LOW : HIGH);
  digitalWrite(motorBIN1, rightMotorSpeed > 127 ? HIGH : LOW);
  digitalWrite(motorBIN2, rightMotorSpeed > 127 ? LOW : HIGH);
  delay(10);
}

This code is responsible for reading the sensor values from the QTRX-HD-07RC Reflectance Sensor Array, calculating the error for the PID control, and adjusting the speed and direction of the DC Mini Metal Gear Motors accordingly. The PID control parameters (Kp, Ki, Kd) can be tuned to optimize the robot's line-following performance.