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

ESP32-CAM Controlled Wi-Fi Robot with Dual Gearmotors and L298N Driver

View and Edit Project In Cirkit DesignerImage of ESP32-CAM Controlled Wi-Fi Robot with Dual Gearmotors and L298N Driver

Circuit Documentation

Summary

This circuit is designed to control a pair of hobby gearmotors using an L298N DC motor driver, which is interfaced with an ESP32-CAM microcontroller. The ESP32-CAM also handles wireless communication and camera functionalities. The power source for the circuit is a pair of 18650 batteries. The ESP32-CAM receives commands over WiFi to control the motors' direction and speed, as well as to manage the camera's operation.

Component List

Hobby Gearmotor with 48:1 Gearbox

ESP32 - CAM

2x 18650 Battery Holder

L298N DC Motor Driver

Wiring Details

Hobby Gearmotor 1

Hobby Gearmotor 2

ESP32 - CAM

2x 18650 Battery Holder

L298N DC Motor Driver

Documented Code

ESP32-CAM Code (sketch.ino)

#include <AsyncTCP.h>
#include "esp_camera.h"
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <iostream>
#include <sstream>

// Motor pin structure and initialization
struct MOTOR_PINS {
  int pinEn;  
  int pinIN1;
  int pinIN2;    
};

std::vector<MOTOR_PINS> motorPins = {
  {12, 13, 15},  // RIGHT_MOTOR Pins (EnA, IN1, IN2)
  {12, 14, 2},   // LEFT_MOTOR  Pins (EnB, IN3, IN4)
};

#define LIGHT_PIN 4

// Movement directions
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define STOP 0

// Motor identifiers
#define RIGHT_MOTOR 0
#define LEFT_MOTOR 1

// Motor directions
#define FORWARD 1
#define BACKWARD -1

// PWM settings
const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int PWMSpeedChannel = 2;
const int PWMLightChannel = 3;

// Camera related constants
// (GPIO pin numbers and other camera settings)

// Wi-Fi credentials
const char* ssid     = "Cam_Car";
const char* password = "12345678";

// Web server and WebSocket setup
AsyncWebServer server(80);
AsyncWebSocket wsCamera("/Camera");
AsyncWebSocket wsCarInput("/CarInput");
uint32_t cameraClientId = 0;

// HTML content for the web interface
const char* htmlHomePage PROGMEM = R"HTMLHOMEPAGE(
<!DOCTYPE html>
<html>
  <!-- HTML content with JavaScript functions to handle WebSocket communication -->
</html>
)HTMLHOMEPAGE";

// Function to rotate the motor in a given direction
void rotateMotor(int motorNumber, int motorDirection) {
  // Implementation of motor rotation logic
}

// Function to move the car based on input value
void moveCar(int inputValue) {
  // Implementation of car movement logic
}

// Web server request handlers
void handleRoot(AsyncWebServerRequest *request) {
  // Serve the HTML home page
}

void handleNotFound(AsyncWebServerRequest *request) {
  // Handle not found error for web requests
}

// WebSocket event handlers for car input and camera
void onCarInputWebSocketEvent(AsyncWebSocket *server, 
                              AsyncWebSocketClient *client, 
                              AwsEventType type,
                              void *arg, 
                              uint8_t *data, 
                              size_t len) {
  // Handle WebSocket events for car input
}

void onCameraWebSocketEvent(AsyncWebSocket *server, 
                            AsyncWebSocketClient *client, 
                            AwsEventType type,
                            void *arg, 
                            uint8_t *data, 
                            size_t len) {
  // Handle WebSocket events for camera
}

// Camera setup function
void setupCamera() {
  // Initialize the camera with the given settings
}

// Function to send camera picture over WebSocket
void sendCameraPicture() {
  // Implementation of sending camera picture to the client
}

// Function to set up pin modes and PWM channels
void setUpPinModes() {
  // Set up PWM and pin modes for motors and light
}

// Arduino setup function
void setup(void) {
  // Initial setup for pin modes, serial communication, Wi-Fi, web server, and camera
}

// Arduino main loop function
void loop() {
  // Main loop to handle WebSocket clients and send camera pictures
}

2x 18650 Battery Holder Code (sketch.ino)

void setup() {
  // No setup code required for the battery holder
}

void loop() {
  // No loop code required for the battery holder
}

This documentation provides an overview of the circuit, including the components used, their wiring, and the embedded code for the ESP32-CAM microcontroller. The code is responsible for handling motor control, camera operation, and web server functionality for remote control and viewing.