Hello,

In this tutorial-based article, we will learn more about the ESP32cam module and we will use it to make a remote-controlled car using Wi-Fi.

 

What is the ESP32-CAM module?

 

 

  • The ESP32-CAM is a normal microcontroller based on the ESP32 structure and it also contains a slot for a MicroSD card and an integrated camera. It’s very economical and easy to operate and is perfect for IoT devices requiring a camera with advanced functions like image tracking and recognition.

 

The architecture of the ESP32-CAM module

  • The ESP32-CAM module has lesser input and output pins than the ESP-32 module on which it is based. Most of the GPIO pins are used for the microSD card port and the camera.
    The thing which is missing from the ESP32-CAM module is the USB port to upload the code. In this project, we are going to use the CP2102 USB to UART module to upload the code to ESP32-CAM.

  • This module has printed circuits on both sides. On the top, the board has the connector for the camera module. It also has a slot for a microSD card on the top. 

  • On the bottom of the board, there is a reset switch that is used to reset the program uploaded from the software

  • As we know, ESP32-CAM shares the same structure as the ESP32-S. So the specifications of both the boards are almost the same.

 

  • 802.11b/g/n Wi-Fi.

  • Bluetooth 4.2 with BLE.

  • The clock speed is up to 160 MHz.

  • Computing power up to 600 DMIPS.

  • 520 KB SRAM plus 4 MB PSRAM.

  • Supports Wi-Fi Image upload.

  • Multiple sleep modes.

  • Firmware over the air (FOTA) upgrades are possible.

  • 9 GPIO ports. 

  • Built-in flash LED. 

Camera specifications

  • The image transfer rate of this module is 15-60 fps.

  • Output formats for this module include YUV422, YUV420, RGB565, RGB555, and 8-bit compressed data

  • Array size UXGA 1622 x 1200

  • Camera sensor of 2 Megapixel

Uploading the code to ESP32-cam

 

 

  • The ESP32-cam module has no USB port. So you cannot just connect it to your computer and upload programs onto it. 

  • To upload the code, you need to connect the esp32 to an external FTDI adapter. 

  • Make sure your Arduino software is up to date. Open the Arduino IDE.

  • Choose the File menu on the top menu bar.

  • Look for the preferences menu and click on it. 

  • Look for the textbox labeled “Additional Boards Manager URLs”.

 

  • Click on OK. This will save the settings which you have changed.

Hardware required

Software required

Car chassis and motors

CP2102 USB to TTL module

  • CP2102 module is a USB to UART converter module. It requires minimal external components. CP2102 can be used to connect devices to computers via USB.

L298N motor driver

  • L298N motor driver is perfect for driving DC motors and Stepper motors. It can control 4 DC motors or 2 DC motors with direction and speed control. This motor driver is perfect for robotics projects and perfect for controlling motors from microcontrollers. Perfect for driving DC stepper motors for micro line-follower robots, robot arms, etc.

Li-ion battery

  • 18650 is a single cell, compact and powerful cell with a 2200 mAh capacity. It is very convenient to fulfill the 3.7 Volt requirement with this cell. The battery terminals can use in any compatible battery adapter/holder or they can be permanently soldered to your application's power source wires. 

Circuit Diagram

Pins on L298N motor driver

Pins on the ESP32 Camera module

IN1

GPIO 2

IN2

GPIO 14

IN3

GPIO 15

IN4

GPIO 13

5V

5V

GND

GND

  • Connect the positive terminal of the battery to 12V and the negative terminal to GND.

  • Connect the two motors on the right side together and same with the left side. Now both the motors on the right side will move together. 

  • Now connect the two terminals of motors on the right side with pins Out 1, 2 on the motor driver. Similarly, connect the two motors of the left side on pins Out 3, 4.

 

The working concept of the Remote controlled car

 

 

  • ESP32-camera module works on Wi-Fi. So it can be given commands wirelessly via IP address.

  • As the command is given from an internet browser with the same IP address, the ESP32 then gives the command to the motor driver.

  • The motor driver then turns the motors which translate into motion.

  • Simultaneously, the ESP32-camera module can stream the video from the camera back to the browser via Wi-Fi.

 

Arduino Code

 

  • The code for remote controlled car using ESP32-camera can be downloaded here. 


 
#include "esp_wifi.h"
#include "esp_camera.h"
#include
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
 
const char* ssid1 = "ESP32-CAM Robot";
const char* password1 = "1234567890";
 
extern volatile unsigned int  motor_speed;
extern void robot_stop();
extern void robot_setup();
extern uint8_t robo;
extern volatile unsigned long previous_time;        
extern volatile unsigned long move_interval;
 
#define CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
 
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22
 
void startCameraServer();
 
void setup()
{
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
 
 
 
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  if(psramFound()){
    config.frame_size = FRAMESIZE_QVGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_QVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
 
 
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
 
  sensor_t * s = esp_camera_sensor_get();
  s->set_framesize(s, FRAMESIZE_QVGA);
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
 
  WiFi.softAP(ssid1, password1);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  
  startCameraServer();
 
  ledcSetup(7, 5000, 8);
  ledcAttachPin(4, 7);  
  robot_setup();
  
  for (int i=0;i<5;i++)
  {
    ledcWrite(7,10);  
    delay(50);
    ledcWrite(7,0);
    delay(50);    
  }
      
  previous_time = millis();
}
 
void loop() {
  if(robo)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previous_time >= move_interval) {
      previous_time = currentMillis;
      robot_stop();
      char rsp[32];
      sprintf(rsp,"SPPED: %d",motor_speed);
      Serial.println("Stop");
      robo=0;
    }
  }
  delay(1);
  yield();
}

Uploading the code for a remote-controlled car

  • First, perform the steps given above to add ESP32 boards to your Arduino IDE.

  • Now go to tools. Set the values according to the given picture below.

  • Upload the code using the circuit diagram given for ESP32-cam and CP2102. Press the reset button on ESP32 if there is an error in uploading the code.

  • After uploading the code, remove the shorted pins IOO and GND.

  • Now open the Serial Monitor on Arduino IDE and press the reset button on ESP32.

  • A window like this will appear. Wait for it to say Wi-Fi connected. 

 

 

 

  • Once the ESP32 is connected to Wi-Fi, copy the IP address and open it in the browser of your device.

  • Now you can control the car remotely. You can also watch the live feed from the ESP32 camera and turn on/off the flash too.

 

Now your remote-controlled car is ready for use. Enjoy!