Arduino-Controlled Ultrasonic Obstacle Avoidance Robot with L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control two DC motors using an L298N motor driver module, with an Arduino UNO as the central microcontroller. The circuit also includes an HC-SR04 ultrasonic sensor for distance measurement. A 9V battery is used to power the motor driver, which in turn powers the motors and the Arduino UNO.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
HC-SR04 Ultrasonic Sensor
- Ultrasonic distance sensor
- Provides 2cm to 400cm non-contact measurement functionality with a ranging accuracy that can reach up to 3mm.
L298N DC Motor Driver
- Dual H-Bridge motor driver
- Can drive two DC motors or one bipolar stepper motor
- Supports a supply voltage of up to 35V and can deliver a continuous current of 2A to each motor channel.
Motor Amarillo Motorreductor Hobby (x2)
- Yellow geared DC hobby motor
- Typically used for small projects and robotic applications.
9V Battery
- Standard 9V battery used to provide power to the circuit.
Wiring Details
Arduino UNO
- 5V connected to HC-SR04 VCC
- GND connected to HC-SR04 GND and L298N GND
- Vin connected to L298N 5V
- D11 connected to L298N ENB
- D10 connected to L298N ENA
- D9 connected to HC-SR04 ECHO
- D8 connected to HC-SR04 TRIG
- D7 connected to L298N IN4
- D6 connected to L298N IN3
- D5 connected to L298N IN2
- D4 connected to L298N IN1
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino UNO 5V
- TRIG connected to Arduino UNO D8
- ECHO connected to Arduino UNO D9
- GND connected to Arduino UNO GND
L298N DC Motor Driver
- OUT1 connected to Motor Amarillo Motorreductor Hobby GND
- OUT2 connected to Motor Amarillo Motorreductor Hobby VCC
- 12V connected to 9V Battery +
- GND connected to 9V Battery - and Arduino UNO GND
- 5V connected to Arduino UNO Vin
- ENA connected to Arduino UNO D10
- IN1 connected to Arduino UNO D4
- IN2 connected to Arduino UNO D5
- IN3 connected to Arduino UNO D6
- IN4 connected to Arduino UNO D7
- ENB connected to Arduino UNO D11
- OUT3 connected to Motor Amarillo Motorreductor Hobby GND
- OUT4 connected to Motor Amarillo Motorreductor Hobby VCC
Motor Amarillo Motorreductor Hobby
- GND connected to L298N OUT1/OUT3
- VCC connected to L298N OUT2/OUT4
9V Battery
- + connected to L298N 12V
- - connected to L298N GND
Documented Code
#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); // Set Trig as output
pinMode(Echo, INPUT); // Set Echo as input
// Set L298N control pins as outputs
pinMode(in21, OUTPUT);
pinMode(in22, OUTPUT);
pinMode(in23, OUTPUT);
pinMode(in24, OUTPUT);
}
unsigned int impulseTime=0;
unsigned int distance_sm=0;
void loop()
{
// Generate ultrasonic pulse
digitalWrite(Trig, HIGH);
delayMicroseconds(10); // 10 microseconds
digitalWrite(Trig, LOW);
// Measure the length of the echo pulse
impulseTime = pulseIn(Echo, HIGH);
distance_sm = impulseTime / 58; // Convert to centimeters
// If the distance is greater than 25 centimeters
if (distance_sm > 25)
{
// Move motors forward
digitalWrite(in21, LOW);
digitalWrite(in22, HIGH);
analogWrite(enA, 60);
analogWrite(enB, 60);
digitalWrite(in23, HIGH);
digitalWrite(in24, LOW);
}
else
{
// Move motors backward
digitalWrite(in21, HIGH);
digitalWrite(in22, LOW);
analogWrite(enA, 100);
analogWrite(enB, 100);
digitalWrite(in23, HIGH);
digitalWrite(in24, LOW);
delay(1100); // Adjust time for turning if necessary
}
delay(50); // Short delay before next loop
}
This code is designed to control the motors connected to the L298N motor driver based on the distance measured by the HC-SR04 ultrasonic sensor. If the measured distance is greater than 25 centimeters, the motors will move forward. If the distance is less than or equal to 25 centimeters, the motors will move backward for a short period to avoid obstacles.