

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.
5V connected to HC-05 Bluetooth Module VCCGND connected to HC-05 Bluetooth Module GND, ESP32 - CAM GND, L293D Driver Shield GND, and lipo battery GNDA0 connected to HC-SR04 Ultrasonic Sensor TRIGA1 connected to HC-SR04 Ultrasonic Sensor ECHOD19/RX1 connected to ESP32 - CAM VOTD18/TX1 connected to ESP32 - CAM VORD17 PWM/RX2 connected to HC-05 Bluetooth Module TXDD16 PWM/TX2 connected to HC-05 Bluetooth Module RXDm1 connected to DC Mini Metal Gear Motor IN1 and IN2m2 connected to Hobby Gearmotor with 48:1 gearbox pin 1 and pin 2m3 connected to another DC Mini Metal Gear Motor IN1 and IN2GND shared with Arduino Mega 2560 GND+ and - connected to Rocker Switch 21 connected to lipo battery VCC2 connected to L293D Driver Shield + and -, and ESP32 - CAM 5VIN1 and IN2 connected to L293D Driver Shield m1 and m3 respectivelypin 1 and pin 2 connected to L293D Driver Shield m2VCC connected to Rocker Switch 1GND shared with Arduino Mega 2560 GNDVCC connected to Arduino Mega 2560 5VGND shared with Arduino Mega 2560 GNDTXD connected to Arduino Mega 2560 D17 PWM/RX2RXD connected to Arduino Mega 2560 D16 PWM/TX25V connected to Rocker Switch 2GND shared with Arduino Mega 2560 GNDVOT connected to Arduino Mega 2560 D19/RX1VOR connected to Arduino Mega 2560 D18/TX1VCC not explicitly connected in the provided net listTRIG connected to Arduino Mega 2560 A0ECHO connected to Arduino Mega 2560 A1GND not explicitly connected in the provided net listThe 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