This circuit is designed to control a set of DC motors using an L298N motor driver, interfaced with an Arduino UNO microcontroller. The circuit includes sensors for detecting flames and lines, as well as an ultrasonic sensor for distance measurement. The Arduino UNO reads sensor data and controls the motor driver to navigate the environment based on sensor inputs. The circuit is powered by a 12V battery, which also supplies power to the motor driver.
// Pin definitions
const int ENA = 10;
const int ENB = 11;
const int IN1 = 4;
const int IN2 = 5;
const int IN3 = 6;
const int IN4 = 7;
const int TRIG = A3;
const int ECHO = A4;
const int FLAME_D0 = 8;
const int FLAME_A0 = A0;
const int LINE1 = A1;
const int LINE2 = A2;
void setup() {
// Initialize motor driver pins as outputs
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Initialize sensor pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(FLAME_D0, INPUT);
pinMode(LINE1, INPUT);
pinMode(LINE2, INPUT);
Serial.begin(9600);
}
void loop() {
// Read sensors
int flameDetected = digitalRead(FLAME_D0);
int line1 = analogRead(LINE1);
int line2 = analogRead(LINE2);
int distance = getDistance();
// Check for white line and flame
if ((line1 > 500 || line2 > 500) && (flameDetected == LOW)) {
stopMotors();
return;
}
// Check for walls
if (distance < 20) {
avoidWall();
return;
}
// Check for flame
if (flameDetected == LOW) {
moveForward();
} else {
// If no flame is detected, keep searching
searchForFlame();
}
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 128); // 50% duty cycle
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 128); // 50% duty cycle
}
void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
void avoidWall() {
// Simple wall avoidance: stop and turn
stopMotors();
delay(500);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 128); // 50% duty cycle
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 128); // 50% duty cycle
delay(1000);
stopMotors();
delay(500);
}
void searchForFlame() {
// Simple search pattern: rotate in place
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 128); // 50% duty cycle
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 128); // 50% duty cycle
}
int getDistance() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
This code is responsible for controlling the motors based on sensor inputs. It includes functions for moving forward, stopping the motors, avoiding walls, and searching for flames. The getDistance
function calculates the distance to an object using the ultrasonic sensor.