This circuit is designed to control a set of DC motors using an Arduino UNO, an L298N motor driver, and various sensors including an HC-SR04 Ultrasonic Sensor, IR sensors, and an HC-05 Bluetooth Module. The circuit is powered by a 2x 18650 battery pack. The Arduino UNO is programmed to control the motor driver based on sensor inputs.
Arduino UNO
HC-SR04 Ultrasonic Sensor
2x 18650 Battery Pack
IR Sensor
L298N DC Motor Driver
HC-05 Bluetooth Module
DC Motor
5V connected to:
GND connected to:
Vin connected to:
D11 connected to:
D10 connected to:
D9 connected to:
D8 connected to:
D7 connected to:
D6 connected to:
D5 connected to:
D4 connected to:
D3 connected to:
D2 connected to:
D1 connected to:
D0 connected to:
VCC connected to:
GND connected to:
TRIG connected to:
ECHO connected to:
vcc connected to:
gnd connected to:
vcc connected to:
gnd connected to:
out connected to:
vcc connected to:
gnd connected to:
out connected to:
5V connected to:
GND connected to:
12V connected to:
ENB connected to:
ENA connected to:
IN4 connected to:
IN3 connected to:
IN2 connected to:
IN1 connected to:
OUT1 connected to:
OUT2 connected to:
OUT3 connected to:
OUT4 connected to:
VCC connected to:
GND connected to:
TXD connected to:
RXD connected to:
#define Trig 8
#define Echo 9
const int in21 = 4; // L298N-2 pin 4
const int in22 = 5; // L298N-2 pin 5
const int in23 = 6; // L298N-2 pin 6
const int in24 = 7; // L298N-2 pin 7
const int enA = 10; // L298N-2 pin 10
const int enB = 11; // L298N-2 pin 11
void setup()
{
pinMode(Trig, OUTPUT); // выход
pinMode(Echo, INPUT); // вход
pinMode(in21, OUTPUT); // выход на L298n
pinMode(in22, OUTPUT); // выход на L298n
pinMode(in23, OUTPUT); // выход на L298n
pinMode(in24, OUTPUT); // выход на L298n
}
unsigned int impulseTime=0;
unsigned int distance_sm=0;
void loop()
{
digitalWrite(Trig, HIGH);
delayMicroseconds(10); // 10 микросекунд
digitalWrite(Trig, LOW);
impulseTime=pulseIn(Echo, HIGH); // замеряем длину импульса
distance_sm=impulseTime/58; // переводим в сантиметры
if (distance_sm>25) // если расстояние более 25 сантиметров
{
digitalWrite(in21, LOW);
digitalWrite(in22, HIGH);
analogWrite(enA, 60);
analogWrite(enB, 60);
digitalWrite(in23, HIGH);
digitalWrite(in24, LOW);
}
else
{
digitalWrite(in21, HIGH);
digitalWrite(in22, LOW);
analogWrite(enA, 100);
analogWrite(enB, 100);
digitalWrite(in23, HIGH);
digitalWrite(in24, LOW);
delay(1100); // если застряет или не выезжает из угла - измените время поворота. сейчас там стоит 1,1 с.
}
delay(50);
}
This code is designed to control the L298N motor driver based on the distance measured by the HC-SR04 Ultrasonic Sensor. The motors are controlled to move forward if the distance is greater than 25 cm and to turn if the distance is less than 25 cm. The IR sensors and Bluetooth module are also connected but not utilized in this code snippet.