Arduino-Controlled Robotic Car with Ultrasonic Sensors and Servo-Operated Dustbin
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit is designed to control a robotic vehicle with two DC motors, two ultrasonic sensors for distance measurement, and two servos for actuation mechanisms. An Arduino UNO microcontroller serves as the central processing unit, interfacing with the ultrasonic sensors and servos directly, and controlling the DC motors through an L298N DC motor driver. The system is powered by a pair of 18650 Li-ion batteries.
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.
L298N DC Motor Driver
- A module that can control up to two DC motors with individual speed and direction control
- It is capable of driving inductive loads such as relays, solenoids, and DC and stepping motors.
DC Motors and Wheels (x2)
- Electric motors that convert electrical energy into mechanical energy, attached to wheels for the robotic vehicle's movement.
18650 Li-ion Battery x 2
- Rechargeable batteries used to power the circuit.
Ultrasonic Sensors (x2)
- Sensors that measure distance by emitting ultrasonic waves and detecting the time taken for the echo to return.
Servos (x2)
- Small rotary actuators with built-in control circuitry, used for precise control of angular position.
Wiring Details
Arduino UNO
- 5V to Ultrasonic Sensors VCC, Servos VCC
- GND to Ultrasonic Sensors GND, Servos GND, L298N GND
- Vin to L298N 12V
- Digital Pins:
- D13 to Ultrasonic Sensor 1 Echo
- D12 to Ultrasonic Sensor 1 Trigger
- D11 to Ultrasonic Sensor 2 Trigger
- D10 to Ultrasonic Sensor 2 Echo
- D8 to Servo 2 PWM
- D7 to L298N ENA
- D6 to L298N IN1
- D5 to L298N IN2
- D4 to L298N IN3
- D3 to L298N IN4
- D2 to L298N ENB
- D1 to Servo 1 PWM
- Power Pins:
- "+" to Battery "+"
- "-" to Battery "-"
L298N DC Motor Driver
- OUT1 to Motor 1 VCC
- OUT2 to Motor 1 GND
- OUT3 to Motor 2 VCC
- OUT4 to Motor 2 GND
DC Motors and Wheels
- Each motor has two connections, VCC and GND, which connect to the L298N driver outputs.
Ultrasonic Sensors
- Each sensor has four connections: VCC, Trigger, Echo, and GND.
Servos
- Each servo has three connections: GND, VCC, and PWM.
Documented Code
#include <Servo.h>
// Pin definitions
const int trigPin1 = 12;
const int echoPin1 = 13;
const int trigPin2 = 11;
const int echoPin2 = 10;
const int motorPin1 = 6;
const int motorPin2 = 5;
const int motorPin3 = 4;
const int motorPin4 = 3;
const int enA = 7;
const int enB = 2;
const int servoPin1 = 8;
const int servoPin2 = 9; // Changed from 1 to 9
// Servo objects
Servo servo1;
Servo servo2;
// Variables
long duration1, distance1;
long duration2, distance2;
unsigned long lastDustbinAction = 0;
bool dustbinOpen = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// Attach servos
servo1.attach(servoPin1);
servo2.attach(servoPin2);
// Set initial servo positions
servo1.write(90);
servo2.write(0);
}
void loop() {
// Measure distance from ultrasonic sensor 1
distance1 = measureDistance(trigPin1, echoPin1);
// Measure distance from ultrasonic sensor 2
distance2 = measureDistance(trigPin2, echoPin2);
// Object detection and movement
if (distance1 < 20) {
stopCar();
if (!dustbinOpen) {
openDustbin();
lastDustbinAction = millis();
dustbinOpen = true;
}
} else if (distance2 < 20) {
moveCarForward();
} else {
stopCar();
}
// Close dustbin after 2 seconds if it was opened
if (dustbinOpen && millis() - lastDustbinAction > 2000) {
closeDustbin();
dustbinOpen = false;
}
}
long measureDistance(int trigPin, int echoPin) {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance
long distance = duration * 0.034 / 2;
return distance;
}
void moveCarForward() {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enA, 255);
analogWrite(enB, 255);
}
void stopCar() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void openDustbin() {
servo2.write(90);
}
void closeDustbin() {
servo2.write(0);
}
This code is responsible for controlling the robotic vehicle's movement and the actuation of the dustbin mechanism. It reads distance measurements from the ultrasonic sensors to navigate and avoid obstacles, controls the DC motors through the motor driver, and operates the servos to open and close the dustbin.