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

ESP32-Controlled Robotic Trolley with Obstacle Avoidance and Bluetooth Connectivity

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled Robotic Trolley with Obstacle Avoidance and Bluetooth Connectivity

Circuit Documentation

Summary of the Circuit

The circuit in question is designed to control a trolley with four DC motors using an ESP32 microcontroller. The ESP32 is responsible for driving the motors through two L298N DC motor drivers, interfacing with an HC-SR04 ultrasonic sensor for distance measurement, and controlling a piezo buzzer for audible feedback. The system can be controlled via Bluetooth commands, allowing for wireless operation. The ESP32 is powered by a 12V battery, which also supplies power to the motor drivers.

Component List

ESP32 (30 pin)

DC Motor (x4)

L298N DC Motor Driver (x2)

HC-SR04 Ultrasonic Sensor

Piezo Buzzer

12V Battery

Wiring Details

ESP32 (30 pin)

DC Motors

L298N DC Motor Driver

HC-SR04 Ultrasonic Sensor

Piezo Buzzer

12V Battery

Documented Code

// Include necessary libraries
#include <WiFi.h>
#include <BluetoothSerial.h>
#include <NewPing.h>

// Define motor driver pins
#define IN1 32  // Motor 1
#define IN2 33
#define IN3 25  // Motor 2
#define IN4 26
#define ENA 27  // Enable pin for Motor 1 and Motor 2
#define ENB 14  // Enable pin for Motor 3 and Motor 4

// Define Ultrasonic Sensor pins
#define TRIG_PIN 18
#define ECHO_PIN 19
#define MAX_DISTANCE 200  // Maximum distance we want to ping (in centimeters)

// Define Buzzer pin
#define BUZZER_PIN 23

// Initialize Bluetooth
BluetoothSerial SerialBT;

// Initialize Ultrasonic Sensor
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

// Function to control motor movement
void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  // ENA and ENB are used to enable/disable the motors and are assumed to be HIGH for movement
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}

void moveBackward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}

void stopTrolley() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
}

// Setup function
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  SerialBT.begin("ESP32_Trolley");  // Bluetooth device name

  // Set motor driver pins as outputs
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);  // ENA
  pinMode(ENB, OUTPUT);  // ENB

  // Set Buzzer pin as output
  pinMode(BUZZER_PIN, OUTPUT);

  // Set Ultrasonic Sensor pins as input/output
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

// Loop function
void loop() {
  // Measure distance using the Ultrasonic Sensor
  unsigned int distance = sonar.ping_cm();

  // If an obstacle is detected within 10 cm, stop the trolley and sound the buzzer
  if (distance > 0 && distance <= 10) {
    stopTrolley();
    digitalWrite(BUZZER_PIN, HIGH);
  } else {
    digitalWrite(BUZZER_PIN, LOW);
  }

  // Check if there are any Bluetooth commands received
  if (SerialBT.available()) {
    char command = SerialBT.read();

    // Control trolley movement based on received command
    if (command == 'F') {
      moveForward();
    } else if (command == 'B') {
      moveBackward();
    } else if (command == 'L') {
      turnLeft();
    } else if (command == 'R') {
      turnRight();
    } else if (command == 'S') {
      stopTrolley();
    }
  }
}

This code is responsible for the operation of the trolley. It initializes the necessary hardware components, defines the pin connections, and contains functions to control the movement of the trolley. The main loop listens for Bluetooth commands to control the trolley and uses the ultrasonic sensor to detect obstacles, stopping the trolley and activating the buzzer if an obstacle is too close.