Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Mega 2560 Controlled Robotic Vehicle with Bluetooth and ESP32-CAM

Image of Arduino Mega 2560 Controlled Robotic Vehicle with Bluetooth and ESP32-CAM

Circuit Documentation

Summary

The circuit in question is designed to control a vehicle with various sensors and communication modules. It includes an Arduino Mega 2560 as the central microcontroller, interfaced with an HC-05 Bluetooth Module for wireless communication, an ESP32-CAM for image capture and processing, and an HC-SR04 Ultrasonic Sensor for distance measurement. The circuit also features a L293D Driver Shield to control multiple DC motors, a lipo battery for power supply, and a Rocker Switch as a power toggle. The motors in the circuit are two DC Mini Metal Gear Motors and a Hobby Gearmotor with a 48:1 gearbox.

Component List

  • Arduino Mega 2560: A microcontroller board based on the ATmega2560, with numerous digital and analog I/O pins.
  • L293D Driver Shield: A motor driver shield that can control up to 4 DC motors or 2 stepper motors.
  • Rocker Switch: A simple on-off switch to control the power flow in the circuit.
  • DC Mini Metal Gear Motor: A small DC motor with a gearbox for increased torque.
  • Hobby Gearmotor with 48:1 gearbox: A DC motor with a 48:1 reduction gearbox for high torque applications.
  • lipo battery 2200mAH 30c: A rechargeable lithium polymer battery with a capacity of 2200mAh.
  • HC-05 Bluetooth Module: A Bluetooth module for wireless data communication.
  • ESP32 - CAM: A small camera module with Wi-Fi capabilities, based on the ESP32 chip.
  • HC-SR04 Ultrasonic Sensor: An ultrasonic distance sensor that can measure distances by emitting ultrasonic waves.

Wiring Details

Arduino Mega 2560

  • 5V connected to HC-05 Bluetooth Module VCC
  • GND connected to HC-05 Bluetooth Module GND, ESP32 - CAM GND, L293D Driver Shield GND, and lipo battery GND
  • A0 connected to HC-SR04 Ultrasonic Sensor TRIG
  • A1 connected to HC-SR04 Ultrasonic Sensor ECHO
  • D19/RX1 connected to ESP32 - CAM VOT
  • D18/TX1 connected to ESP32 - CAM VOR
  • D17 PWM/RX2 connected to HC-05 Bluetooth Module TXD
  • D16 PWM/TX2 connected to HC-05 Bluetooth Module RXD

L293D Driver Shield

  • m1 connected to DC Mini Metal Gear Motor IN1 and IN2
  • m2 connected to Hobby Gearmotor with 48:1 gearbox pin 1 and pin 2
  • m3 connected to another DC Mini Metal Gear Motor IN1 and IN2
  • GND shared with Arduino Mega 2560 GND
  • + and - connected to Rocker Switch 2

Rocker Switch

  • 1 connected to lipo battery VCC
  • 2 connected to L293D Driver Shield + and -, and ESP32 - CAM 5V

DC Mini Metal Gear Motor

  • Two instances, each with IN1 and IN2 connected to L293D Driver Shield m1 and m3 respectively

Hobby Gearmotor with 48:1 gearbox

  • pin 1 and pin 2 connected to L293D Driver Shield m2

lipo battery 2200mAH 30c

  • VCC connected to Rocker Switch 1
  • GND shared with Arduino Mega 2560 GND

HC-05 Bluetooth Module

  • VCC connected to Arduino Mega 2560 5V
  • GND shared with Arduino Mega 2560 GND
  • TXD connected to Arduino Mega 2560 D17 PWM/RX2
  • RXD connected to Arduino Mega 2560 D16 PWM/TX2

ESP32 - CAM

  • 5V connected to Rocker Switch 2
  • GND shared with Arduino Mega 2560 GND
  • VOT connected to Arduino Mega 2560 D19/RX1
  • VOR connected to Arduino Mega 2560 D18/TX1

HC-SR04 Ultrasonic Sensor

  • VCC not explicitly connected in the provided net list
  • TRIG connected to Arduino Mega 2560 A0
  • ECHO connected to Arduino Mega 2560 A1
  • GND not explicitly connected in the provided net list

Documented Code

The code is written for the Arduino Mega 2560 and is responsible for controlling the vehicle's movement based on sensor inputs and wireless commands. It includes motor control, distance measurement with the ultrasonic sensor, and communication with the ESP32-CAM and Bluetooth module.

#include <AFMotor.h>
#include <Wire.h>

// Sonar sensor pins
int echo_pin = A1;
int trigger_pin = A0;

// Data handling
String espData = "";   // Variable to store data from ESP32-CAM
String btData = "";    // Variable to store data from Bluetooth
boolean espNewData = false;  // Flag to indicate new data from ESP32-CAM
boolean btNewData = false;   // Flag to indicate new data from Bluetooth

// Motor initialization
AF_DCMotor LF(1);
AF_DCMotor LB(2);
AF_DCMotor RF(3);
//AF_DCMotor RB(4);

void setup() {
  pinMode(echo_pin, INPUT);
  pinMode(trigger_pin, OUTPUT);
  
  Serial.begin(115200);    // Start USB serial communication for debugging
  Serial1.begin(115200);   // Start serial communication with the ESP32-CAM
  Serial2.begin(9600);     // Start serial communication with the Bluetooth module

  Serial.println("Arduino Ready to Receive Data");
  Wire.begin();
  
  // Set motor speeds
  LF.setSpeed(150);
  LB.setSpeed(80);
  RF.setSpeed(150);
  //RB.setSpeed(90);
}

void loop() {
  long duration, distance;
  boolean actionTriggered = false;

  // Trigger the sonar sensor
  digitalWrite(trigger_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin, LOW);

  // Read the echo signal and calculate distance
  duration = pulseIn(echo_pin, HIGH);
  distance = (duration / 2) / 29.1;  // Convert time to distance (cm)

  // If distance is less than or equal to 60 cm, turn the vehicle
  if (distance <= 40 && distance > 0) {  // Ensure valid reading
    Serial.println("Obstacle detected by Sonar! Turning the vehicle.");
    RobotRight();  // Turn the vehicle
    delay(1000);   // Turn duration (adjust as needed)
    RobotStop();   // Stop after turning
    actionTriggered = true;
  }

  // Check if data is available on Serial1 (from ESP32-CAM)
  while (Serial1.available() > 0) {
    char receivedChar = Serial1.read();  // Read each character from Serial1
    espData += receivedChar;             // Append character to the string

    // Check if the data received ends with a newline character
    if (receivedChar == '\n') {
      espNewData = true;
      break;
    }
  }

  // Check if data is available on Serial2 (from Bluetooth)
  while (Serial2.available() > 0) {
    char receivedChar = Serial2.read();  // Read each character from Serial2
    btData += receivedChar;              // Append character to the string

    // Check if the data received ends with a newline character
    if (receivedChar == '\n') {
      btNewData = true;
      break;
    }
  }

  // Process the Bluetooth data if available and not already triggered by sonar
  if (btNewData && !actionTriggered) {
    btData.trim();
    Serial.println("Bluetooth Command: " + btData);

    // Handle Bluetooth commands
    if (btData == "X") {
      Serial.println("Action Triggered by Bluetooth: Stop the vehicle!");
      RobotStop();
      actionTriggered = true;
    } else if (btData == "R") {
      Serial.println("Action Triggered by Bluetooth: Turn the vehicle!");
      RobotRight();
      delay(1000);
      actionTriggered = true;
    } else if (btData == "F") {
      Serial.println("Action Triggered by Bluetooth: Move Forward!");
      RobotForward();
    }

    // Clear the Bluetooth data
    btData = "";
    btNewData = false;
  }

  // If no Bluetooth command, process the ESP32-CAM data
  if (espNewData && !actionTriggered) {
    espData.trim();

    // Print the received data for debugging
    Serial.println("Received from ESP32-CAM: " + espData);

    // Parse the incoming data (e.g., "Stop