Skip to content

vitorccs/esp32cam-rc-car

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ESP32-CAM RC Car

Remote Controlled Car using ESP32-CAM board (Ai Thinker).

Recording

Note: enable the sound by clicking on the speaker icon from the video toolbar

Car.Recording.mp4

Description

Esp32CamRcCar_v3_bb

This is a Platform IO IDE project coded in C++.

The ESP32-CAM connects to your Wi-Fi network and provides a Web page containing a virtual joystick.

Note: In order to control your car remotelly from your public internet IP address, you need to set up Port Forwarding in your home router for ports 80 (Web page), 81 (video streaming) and 82 (web socket/joystick commands).

Components

  • 01 - ESP32-CAM board
  • 01 - Car Chassis (2WD or 4WD)
  • 02 - DC Motors (3v - 6v)
  • 01 - L298N Dual H-Bridge board
  • 02 - White Leds
  • 03 - 18650 batteries (3.7v - 4.2v)
  • 01 - Battery support
  • 01 - Electrolytic Capacitor 1000 μF (16v - 50v)
  • 01 - Antenna for ESP32-CAM board - improves video streaming and prevents lags

Note: in order to enable the external antenna, it is required to change resistor position in ESP32-CAM board see tutorial.

About PlatformIO IDE

Platform IO is a plugin for Microsoft Visual Studio Code. It is a more robust IDE compared to the official Arduino IDE. It also allows us to easily create our own private libraries and use a more object oriented code.

About the code

The PINs can be customized in the main.cpp

#include <Arduino.h>
#include <WifiHandler.h>
#include <StreamServer.h>
#include <SocketServer.h>
#include <DCMotor.h>
#include <WebJoystickHandler.h>
#include <JoyCoords.h>
#include <Car.h>
#include <sensor.h>

// Replace with your network credentials
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PWD "YOUR_PWD"
#define WIFI_AP_MODE false // Access Point mode (no internet connection)
#define JOYSTICK_DEBUG true
#define PIN_FRONT_LED 2
#define PIN_CAMERA_LED 4
#define PIN_M1_IN1 14
#define PIN_M1_IN2 15
#define PIN_M2_IN1 12
#define PIN_M2_IN2 13
#define MIN_MOTOR_SPEED 80 // (0 to 255)
#define FRAME_SIZE FRAMESIZE_VGA
#define JPEG_QUALITY 25 // (0 to 63) lower means higher quality

// Car components
DCMotor motor1(PIN_M1_IN1, PIN_M1_IN2);
DCMotor motor2(PIN_M2_IN1, PIN_M2_IN2);
DigitalLed frontLed(PIN_FRONT_LED);
DigitalLed camLed(PIN_CAMERA_LED);
Car car(motor1, motor2, frontLed, camLed);

// Handlers
WifiHandler wifiHandler;
WebJoystickHandler webJoystickHandler(car);

// Stream and Socket servers
StreamServer streamServer = StreamServer();
SocketServer socketServer = SocketServer();

void setup()
{
  Serial.begin(115200);
  Serial.setDebugOutput(false);

  car.stop();
  car.setMinAbsSpeed(MIN_MOTOR_SPEED);

  streamServer.init(FRAME_SIZE, JPEG_QUALITY);

  // Wi-Fi connection
  if (WIFI_AP_MODE)
  {
    wifiHandler.apMode(WIFI_SSID, WIFI_PWD);
  }
  else
  {
    wifiHandler.connect(WIFI_SSID, WIFI_PWD);
  }

  // Start streaming web server
  streamServer.startStream();

  // Set Web Joystick (Web sockets)
  webJoystickHandler.setDebug(JOYSTICK_DEBUG);

  // Start Web Sockets
  CoordsHandlerFunction coordsHandler = [&](JoyCoords coords)
  {
    webJoystickHandler.handle(coords);
  };

  ButtonToggleHandlerFunction buttonAHandler = [&](bool toggle)
  {
    webJoystickHandler.toggleCamLed(toggle);
  };

  ButtonToggleHandlerFunction buttonBHandler = [&](bool toggle)
  {
    webJoystickHandler.toggleFrontLights(toggle);
  };

  socketServer.init(coordsHandler,
                    buttonAHandler,
                    buttonBHandler);
}

void loop()
{
  socketServer.loop();
}

Fine-tuning customizations can be done in the individual files like DCMotor.h for changing speed parameters

#ifndef DCMOTOR_H
#define DCMOTOR_H
#include <Arduino.h>

class DCMotor
{
public:
    DCMotor(uint8_t pinIn1, uint8_t pinIn2);
    void backward(uint8_t speed = 100);
    void forward(uint8_t speed = 100);
    void setMinAbsSpeed(uint8_t absSpeed);
    void stop();

private:
    uint8_t pinIn1;
    uint8_t pinIn2;
    uint8_t absSpeed = 0;
    uint8_t maxAbsSpeed = 255;
    uint8_t minAbsSpeed = 50;
    uint8_t ignoreAbsSpeed = 30;

    void setSpeed(uint8_t speed);
};
#endif

About Car Chassis

This project can work with a 2WD or 4WD car chassis like these ones:

Reference 1 (Brazil):

https://www.robocore.net/robotica-robocore/plataforma-robotica-rocket-tank

Reference 2 (International):

https://www.aliexpress.us/item/3256801542172576.html

Reference 3 (International):

https://www.aliexpress.us/item/3256805855273192.html

About the Power Supply

I recommend to use high quality 18650 batteries (3.7v - 4.2v, 2200mAh, at least 2C of discharge rate).

Most people prefer to use different power sources for ESP32-CAM (3.3v or 5v) and Bridge driver (6 - 35v).

I prefer to have a single power source and thus a single power switch. However, it is recommended to use capacitor to filter the electrical noise created by the bridge.

Fritzing file

The electronic schematic was created in the Fritzing software and can be downloaded at Esp32CamRcCar_v3.zip

About

Remote Controlled Car using ESP32-CAM board

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published