This circuit is designed to control a pair of motors using an L298N DC motor driver, interfaced with an Arduino Nano microcontroller. The Arduino Nano reads inputs from two TCS3200 color sensors and drives the motors based on the sensor readings to perform line following or obstacle avoidance tasks. The motor driver is powered by a battery pack, and the Arduino Nano controls the motor driver through digital output pins.
// Motor pins
const int leftMotorForward = 3;
const int leftMotorBackward = 4;
const int rightMotorForward = 5;
const int rightMotorBackward = 6;
// Sensor pins
const int leftSensor = A0;
const int rightSensor = A1;
// Threshold for detecting the line
const int threshold = 500; // Adjust based on your sensors
void setup() {
pinMode(leftMotorForward, OUTPUT);
pinMode(leftMotorBackward, OUTPUT);
pinMode(rightMotorForward, OUTPUT);
pinMode(rightMotorBackward, OUTPUT);
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);
}
void loop() {
int leftValue = analogRead(leftSensor);
int rightValue = analogRead(rightSensor);
if (leftValue < threshold && rightValue < threshold) {
// Move forward if both sensors see the line
moveForward();
} else if (leftValue < threshold) {
// Turn right if only the left sensor sees the line
turnRight();
} else if (rightValue < threshold) {
// Turn left if only the right sensor sees the line
turnLeft();
} else {
// Stop if neither sensor sees the line
stop();
}
}
void moveForward() {
digitalWrite(leftMotorForward, HIGH);
digitalWrite(leftMotorBackward, LOW);
digitalWrite(rightMotorForward, HIGH);
digitalWrite(rightMotorBackward, LOW);
}
void turnRight() {
digitalWrite(leftMotorForward, HIGH);
digitalWrite(leftMotorBackward, LOW);
digitalWrite(rightMotorForward, LOW);
digitalWrite(rightMotorBackward, HIGH);
}
void turnLeft() {
digitalWrite(leftMotorForward, LOW);
digitalWrite(leftMotorBackward, HIGH);
digitalWrite(rightMotorForward, HIGH);
digitalWrite(rightMotorBackward, LOW);
}
void stop() {
digitalWrite(leftMotorForward, LOW);
digitalWrite(leftMotorBackward, LOW);
digitalWrite(rightMotorForward, LOW);
digitalWrite(rightMotorBackward, LOW);
}
This code is designed to be uploaded to the Arduino Nano microcontroller. It sets up the motor control pins and sensor input pins, then continuously reads the sensor values in the loop()
function. Based on the sensor readings, it controls the motors to move forward, turn left, turn right, or stop. The threshold
value is used to determine whether the sensors detect the line or not, and should be calibrated based on the actual sensor readings obtained from the environment.