Arduino UNO and ESP32-CAM Controlled DC Motor System with Emergency Stop
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit involves an Arduino UNO microcontroller, an ESP32-CAM module, two L298N DC motor drivers, four DC motors, a 12V battery, a 3.3V battery, and two rocker switches. The Arduino UNO controls the motor drivers and the ESP32-CAM, while the rocker switches are used for direction control and emergency stop functionality.
Component List
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
DC Motor
- Description: Standard DC motor.
- Pins: pin 1, pin 2
L298N DC Motor Driver
- Description: Dual H-Bridge motor driver.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
Battery 12V
- Description: 12V power source.
- Pins: +, -
ESP32-CAM
- Description: Wi-Fi and Bluetooth module with camera.
- Pins: 5V, GND, IO12, IO13, IO15, IO14, IO2, IO4, VOT, VOR, VCC, IO0, IO16, 3V3
Rocker Switch
- Description: On/Off switch.
- Pins: output, input
Battery 3.3V
- Description: 3.3V power source.
- Pins: +, -
Wiring Details
Arduino UNO
- 5V connected to ESP32-CAM 5V
- GND connected to ESP32-CAM GND, Battery 12V -, Battery 3.3V -
- Vin connected to Battery 12V +
- D13 connected to L298N DC Motor Driver IN1
- D12 connected to L298N DC Motor Driver IN2
- D11 connected to L298N DC Motor Driver IN3
- D10 connected to L298N DC Motor Driver IN4
- D8 connected to Rocker Switch output
- D7 connected to L298N DC Motor Driver IN1
- D6 connected to L298N DC Motor Driver IN2
- D5 connected to L298N DC Motor Driver IN3
- D4 connected to L298N DC Motor Driver IN4
- D2 connected to Rocker Switch output
- D1 connected to ESP32-CAM VOT
- D0 connected to ESP32-CAM VOR
DC Motor
- pin 1 connected to L298N DC Motor Driver OUT4
- pin 2 connected to L298N DC Motor Driver OUT3
L298N DC Motor Driver
- OUT1 connected to DC Motor pin 2
- OUT2 connected to DC Motor pin 1
- 12V connected to Battery 12V +
- GND connected to Battery 12V -
- IN1 connected to Arduino UNO D13
- IN2 connected to Arduino UNO D12
- IN3 connected to Arduino UNO D11
- IN4 connected to Arduino UNO D10
Battery 12V
- + connected to Arduino UNO Vin, L298N DC Motor Driver 12V
- - connected to Arduino UNO GND, L298N DC Motor Driver GND
ESP32-CAM
- 5V connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
- VOT connected to Arduino UNO D1
- VOR connected to Arduino UNO D0
Rocker Switch
- output connected to Arduino UNO D8, Arduino UNO D2
- input connected to Battery 3.3V +
Battery 3.3V
- + connected to Rocker Switch input
- - connected to Arduino UNO GND
Code Documentation
// Motor driver 1 pins
const int IN1_1 = 13;
const int IN2_1 = 12;
const int IN3_1 = 11;
const int IN4_1 = 10;
// Motor driver 2 pins
const int IN1_2 = 7;
const int IN2_2 = 6;
const int IN3_2 = 5;
const int IN4_2 = 4;
// ESP32-CAM pins
const int VOR = 0;
const int VOT = 1;
// Rocker switch pins
const int DIRECTION_SWITCH = 2;
const int EMERGENCY_STOP_SWITCH = 8;
void setup() {
// Initialize motor driver 1 pins
pinMode(IN1_1, OUTPUT);
pinMode(IN2_1, OUTPUT);
pinMode(IN3_1, OUTPUT);
pinMode(IN4_1, OUTPUT);
// Initialize motor driver 2 pins
pinMode(IN1_2, OUTPUT);
pinMode(IN2_2, OUTPUT);
pinMode(IN3_2, OUTPUT);
pinMode(IN4_2, OUTPUT);
// Initialize ESP32-CAM pins
pinMode(VOR, INPUT);
pinMode(VOT, OUTPUT);
// Initialize rocker switch pins
pinMode(DIRECTION_SWITCH, INPUT);
pinMode(EMERGENCY_STOP_SWITCH, INPUT);
// Initial motor states
stopMotors();
}
void loop() {
// Read switch states
int directionSwitchState = digitalRead(DIRECTION_SWITCH);
int emergencyStopSwitchState = digitalRead(EMERGENCY_STOP_SWITCH);
// Control motors based on switch states
if (emergencyStopSwitchState == HIGH) {
// Emergency stop: Stop all motors
stopMotors();
} else {
if (directionSwitchState == HIGH) {
// Move motors forward
moveForward();
} else {
// Move motors backward
moveBackward();
}
}
delay(100); // Small delay for switch debounce
}
void stopMotors() {
// Stop motor driver 1
digitalWrite(IN1_1, LOW);
digitalWrite(IN2_1, LOW);
digitalWrite(IN3_1, LOW);
digitalWrite(IN4_1, LOW);
// Stop motor driver 2
digitalWrite(IN1_2, LOW);
digitalWrite(IN2_2, LOW);
digitalWrite(IN3_2, LOW);
digitalWrite(IN4_2, LOW);
}
void moveForward() {
// Motor driver 1: Forward
digitalWrite(IN1_1, HIGH);
digitalWrite(IN2_1, LOW);
digitalWrite(IN3_1, HIGH);
digitalWrite(IN4_1, LOW);
// Motor driver 2: Forward
digitalWrite(IN1_2, HIGH);
digitalWrite(IN2_2, LOW);
digitalWrite(IN3_2, HIGH);
digitalWrite(IN4_2, LOW);
}
void moveBackward() {
// Motor driver 1: Backward
digitalWrite(IN1_1, LOW);
digitalWrite(IN2_1, HIGH);
digitalWrite(IN3_1, LOW);
digitalWrite(IN4_1, HIGH);
// Motor driver 2: Backward
digitalWrite(IN1_2, LOW);
digitalWrite(IN2_2, HIGH);
digitalWrite(IN3_2, LOW);
digitalWrite(IN4_2, HIGH);
}
This code initializes the pins for the motor drivers, ESP32-CAM, and rocker switches. It reads the states of the rocker switches to control the direction of the motors and to handle emergency stops. The stopMotors, moveForward, and moveBackward functions control the motor states accordingly.