This circuit is designed to control two motors using an L298N DC motor driver, interfaced with an Arduino UNO microcontroller. The circuit also includes an MKE-S01 Ultrasonic Distance Sensor for measuring distances and an MKE-S10 CNY70 Line Follower Sensor for detecting lines. The motors' operations are influenced by the sensors' readings. One motor's activation is based on the line follower sensor, while the other motor's speed is adjusted according to the distance measured by the ultrasonic sensor. The circuit is powered by a 12V battery.
5V
and GND
pins are used to power the MKE-S01 Ultrasonic Distance Sensor and MKE-S10 CNY70 Line Follower Sensor.A0
pin is connected to the SIG pin of the MKE-S10 CNY70 Line Follower Sensor.D10
pin is connected to the ECHO pin of the MKE-S01 Ultrasonic Distance Sensor.D9
pin is connected to the TRIG pin of the MKE-S01 Ultrasonic Distance Sensor.D2
to D6
pins are used to control the L298N DC Motor Driver.SIG
pin connected to Arduino UNO's A0
pin.5V
and GND
pins connected to the corresponding power supply pins on the Arduino UNO.TRIG
pin connected to Arduino UNO's D9
pin.ECHO
pin connected to Arduino UNO's D10
pin.5V
and GND
pins connected to the corresponding power supply pins on the Arduino UNO.IN1
to IN4
pins connected to Arduino UNO's D2
to D5
pins for motor control signals.ENA
and ENB
pins connected to 5V
for enabling the motor outputs.OUT1
to OUT4
pins connected to the motors.12V
and GND
pins connected to the 12V battery for motor power supply.vcc
and GND
pins connected to the OUT1
/OUT2
and OUT3
/OUT4
pins of the L298N DC Motor Driver, respectively.+
pin connected to the 12V
pin of the L298N DC Motor Driver.-
pin connected to the GND
pin of the L298N DC Motor Driver./*
* Arduino Sketch for controlling two motors using an L298 motor driver,
* an ultrasonic sensor, and a line follower sensor. Motor 1 is activated
* or stopped based on the line follower sensor, and motor 2's speed is
* adjusted based on the distance measured by the ultrasonic sensor.
*/
// Pin definitions
const int lineFollowerPin = A0; // Line follower sensor pin
const int trigPin = 9; // Ultrasonic sensor TRIG pin
const int echoPin = 10; // Ultrasonic sensor ECHO pin
const int motor1Pin1 = 2; // Motor 1 control pin 1
const int motor1Pin2 = 3; // Motor 1 control pin 2
const int motor2Pin1 = 4; // Motor 2 control pin 1
const int motor2Pin2 = 5; // Motor 2 control pin 2
const int motor2SpeedPin = 6; // Motor 2 speed control pin
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize pins
pinMode(lineFollowerPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2SpeedPin, OUTPUT);
}
void loop() {
// Read line follower sensor
int lineState = analogRead(lineFollowerPin);
// Control motor 1 based on line follower sensor
if (lineState < 500) { // Assuming black is low value
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
} else { // Assuming white is high value
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
}
// Measure distance using ultrasonic sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Control motor 2 speed based on distance
int motorSpeed = map(distance, 0, 100, 0, 255);
motorSpeed = constrain(motorSpeed, 0, 255);
analogWrite(motor2SpeedPin, motorSpeed);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
// Small delay to stabilize readings
delay(100);
}
This code is responsible for reading sensor inputs and controlling the motors accordingly. The line follower sensor's signal is read through an analog pin and is used to control the first motor. The ultrasonic sensor's distance measurement is used to adjust the speed of the second motor. The code includes setup routines for pin configurations and a main loop that handles sensor readings and motor control logic.