Arduino-Controlled Obstacle-Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit in question appears to be designed for a mobile robot platform equipped with an Arduino UNO microcontroller, an ultrasonic sensor for distance measurement, DC gearmotors for locomotion, a servomotor for directional control or other actuation, and an L298N motor driver for controlling the gearmotors. Power is supplied by a 2x 18650 battery pack, and a rocker switch is used to control power to the circuit. An Arduino Sensor Shield v5.0 is used to facilitate easy connections between the Arduino UNO and the various peripherals.
Component List
Microcontroller
- Arduino UNO: A microcontroller board based on the ATmega328P, featuring digital and analog I/O pins.
Sensors
- HC-SR04 Ultrasonic Sensor: A sensor for measuring distance via ultrasonic waves.
Actuators
- Gearmotor DC Wheels (left and right): Motors with gear reduction used for driving the wheels of the robot.
- Servomotor SG90: A small servo used for precise angular movement, possibly for steering or actuation.
Motor Driver
- L298N DC Motor Driver: A module capable of driving up to two DC motors with directional and speed control.
Power Supply
- 2x 18650 Battery Pack: A power source consisting of two 18650 lithium-ion batteries.
- Rocker Switch (SPST): A single-pole single-throw switch used to control the power to the motor driver.
Interface Board
- Arduino Sensor Shield v5.0: An expansion board that allows for easy connection of sensors and actuators to the Arduino UNO.
Wiring Details
Arduino UNO
- Connected to the Arduino Sensor Shield v5.0 for interfacing with other components.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to the 5V output from the Arduino Sensor Shield v5.0 (13-V).
- TRIG: Connected to a digital pin on the Arduino Sensor Shield v5.0 (13-S) for triggering the sensor.
- ECHO: Connected to a digital pin on the Arduino Sensor Shield v5.0 (12-S) for receiving the echo signal.
- GND: Connected to the ground on the Arduino Sensor Shield v5.0 (12-G).
Gearmotor DC Wheels (left and right)
- PIN1 and PIN2: Connected to the L298N DC motor driver outputs (OUT1, OUT2, OUT3, OUT4) for motor control.
Servomotor SG90
- SIG: Connected to a digital pin on the Arduino Sensor Shield v5.0 (11-S) for signal control.
- VCC: Connected to the 5V output from the Arduino Sensor Shield v5.0 (11-V).
- GND: Connected to the ground on the Arduino Sensor Shield v5.0 (11-G).
L298N DC Motor Driver
- 12V: Connected to one terminal of the Rocker Switch (SPST).
- GND: Connected to the ground of the 2x 18650 battery pack and the Arduino Sensor Shield v5.0.
- 5V: Connected to the 5V output from the Arduino Sensor Shield v5.0.
- ENA, ENB: Connected to digital pins on the Arduino Sensor Shield v5.0 (6-S, 3-S) for enabling the motor outputs.
- IN1, IN2, IN3, IN4: Connected to digital pins on the Arduino Sensor Shield v5.0 (7-S, 5-S, 4-S, 2-S) for motor direction control.
Rocker Switch (SPST)
- 1: Connected to the positive terminal of the 2x 18650 battery pack (vcc).
- 2: Connected to the 12V input of the L298N DC motor driver.
2x 18650 Battery Pack
- vcc: Connected to one terminal of the Rocker Switch (SPST).
- gnd: Connected to the ground of the Arduino Sensor Shield v5.0 and the L298N DC motor driver.
Documented Code
#include <Servo.h>
volatile int IN1;
volatile int IN2;
volatile int IN3;
volatile int IN4;
volatile int rightDistance;
volatile int leftDistance;
Servo servo_4;
void left_side_forward(int speed) {
// Left
analogWrite(IN1,(speed * 0.92));
analogWrite(IN2,0);
}
void right_side_forward(int speed) {
// Right
analogWrite(IN3,speed);
analogWrite(IN4,0);
}
void left_side_backward(int speed) {
// Left
analogWrite(IN1,0);
analogWrite(IN2,(speed * 0.92));
}
void right_side_backward(int speed) {
// Right
analogWrite(IN3,0);
analogWrite(IN4,speed);
}
float checkdistance_A1_A2() {
digitalWrite(A1, LOW);
delayMicroseconds(2);
digitalWrite(A1, HIGH);
delayMicroseconds(10);
digitalWrite(A1, LOW);
float distance = pulseIn(A2, HIGH) / 58.00;
delay(10);
return distance;
}
void forward(int speed) {
left_side_forward(speed);
right_side_forward(speed);
}
void backward(int speed) {
left_side_backward(speed);
right_side_backward(speed);
}
void left(int speed, int time) {
left_side_backward(speed);
right_side_forward(speed);
delay(time);
stop();
}
void right(int speed, int time) {
left_side_forward(speed);
right_side_backward(speed);
delay(time);
stop();
}
void stop() {
left_side_forward(0);
right_side_forward(0);
}
void setup(){
IN1 = 11;
IN2 = 6;
IN3 = 5;
IN4 = 3;
rightDistance = 3;
leftDistance = 3;
servo_4.attach(4);
pinMode(A1, OUTPUT);
pinMode(A2, INPUT);
}
void loop(){
servo_4.write(90);
delay(500);
while (checkdistance_A1_A2() > 15) {
forward(100);
}
stop();
servo_4.write(0);
delay(500);
rightDistance = checkdistance_A1_A2();
servo_4.write(180);
delay(500);
leftDistance = checkdistance_A1_A2();
if (rightDistance >= leftDistance) {
right(200, 380);
} else {
left(200, 380);
}
}
This code is designed to control a robot with differential steering. It uses an ultrasonic sensor to detect obstacles and makes decisions to move forward or turn based on the distance readings. The servomotor is used to change the direction of the ultrasonic sensor for scanning the environment. The L298N motor driver is controlled through the Arduino pins to drive the gearmotors for movement.