Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Robotic Car with Ultrasonic Sensors and Servo-Operated Dustbin

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Robotic Car with Ultrasonic Sensors and Servo-Operated Dustbin

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

L298N DC Motor Driver

DC Motors and Wheels (x2)

18650 Li-ion Battery x 2

Ultrasonic Sensors (x2)

Servos (x2)

Wiring Details

Arduino UNO

L298N DC Motor Driver

DC Motors and Wheels

Ultrasonic Sensors

Servos

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.