Arduino-Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and L298N Motor Driver
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a mobile robot with ultrasonic sensing capabilities. It uses an Arduino UNO as the main microcontroller, interfaced with an Arduino Sensor Shield v5.0 for easy connection to various peripherals. The robot's movement is driven by two pairs of gearmotors connected to an L298N DC motor driver, which allows for independent control of the left and right sides of the robot. An HC-SR04 Ultrasonic Sensor is used for distance measurement, and a Servomotor SG90 is included for directional control or additional functionality. Power is supplied by a 2x 18650 battery pack, and a Rocker Switch (SPST) is used to control the power flow to the circuit.
Component List
- Arduino UNO: A microcontroller board based on the ATmega328P, with a variety of digital and analog I/O pins.
- HC-SR04 Ultrasonic Sensor: A sensor that measures distance by emitting ultrasonic waves and timing their return after bouncing off objects.
- Gearmotor DC Wheels (left and right): Motors that provide locomotion for the robot.
- Servomotor SG90: A small and lightweight servo motor for precise angular control.
- L298N DC Motor Driver: A dual H-bridge motor driver that can drive two DC motors or one stepper motor.
- Rocker Switch (SPST): A single-pole single-throw switch used to control the power supply.
- Arduino Sensor Shield v5.0: An expansion board that allows for easy connection of sensors and actuators to the Arduino UNO.
- 2x 18650 Battery Pack: A power source for the circuit, consisting of two 18650 lithium-ion batteries.
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino Sensor Shield v5.0 (13-V)
- TRIG connected to Arduino Sensor Shield v5.0 (13-S)
- ECHO connected to Arduino Sensor Shield v5.0 (12-S)
- GND connected to Arduino Sensor Shield v5.0 (12-G)
Gearmotor DC Wheels (Left and Right)
- PIN1 of both left and right gearmotors connected to L298N DC motor driver (OUT2 and OUT4 respectively)
- PIN2 of both left and right gearmotors connected to L298N DC motor driver (OUT1 and OUT3 respectively)
Servomotor SG90
- SIG connected to Arduino Sensor Shield v5.0 (11-S)
- VCC connected to Arduino Sensor Shield v5.0 (11-V)
- GND connected to Arduino Sensor Shield v5.0 (11-G)
L298N DC Motor Driver
- 12V connected to Rocker Switch (SPST) (2)
- GND connected to 2x 18650 Battery Pack (gnd)
- 5V connected to Arduino Sensor Shield v5.0 (VCC)
- +5V-J2 connected to Arduino Sensor Shield v5.0 (1-V)
- ENA connected to Arduino Sensor Shield v5.0 (6-S)
- IN1 connected to Arduino Sensor Shield v5.0 (7-S)
- IN2 connected to Arduino Sensor Shield v5.0 (5-S)
- IN3 connected to Arduino Sensor Shield v5.0 (4-S)
- IN4 connected to Arduino Sensor Shield v5.0 (2-S)
- ENB connected to Arduino Sensor Shield v5.0 (3-S)
Rocker Switch (SPST)
- 1 connected to 2x 18650 Battery Pack (vcc)
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 controls the robot's movement based on the distance measurements from the ultrasonic sensor. It includes functions for moving forward, backward, turning left, and turning right. The checkdistance_A1_A2 function is used to measure the distance from obstacles, and the robot makes decisions based on these measurements to navigate its environment.