ESP32 Wi-Fi RC car with the video camera

Kirill Yatsenko
6 min readNov 13, 2020
super cool photo

Intro

There is a lot of tutorials done on this topic using Arduino libraries
I’m not against them.

But I think you can learn a lot more about the dev board
if using native frameworks, in this case, esp-idf

I always liked to play with remotely controlled cars from early childhood I had a few of them. I’ve decided to build one myself.

After this tutorial, you will be able to:
1. Drive DC motors
2. Configure Wi-Fi Access Point
3. Creating HTTP server
4. Writing simple Javascript/HTML/CSS
5. Stream video from the camera

GitHub repository: https://github.com/KirillYatsenko/wicar

Prerequisites

I assume you already have some knowledge of ESP32 and done some small projects at least led blinking
If not please check this guide. It will walk you through installing esp-idf and flashing the hello world program to the chip
I also recommend buying this course it’s not advertising I really learned a lot from it.

Hardware:
1. ESP32 camera module.
2. UART to USB converter. I’ve covered flashing here
3. Base for the car
4. l298n driver module
5. 2x18650 batteries
6. Battery holder

Demo time

In this section, we will assemble the hardware and will flash our board. But first, you should check the demo

ESP32 | RC WiFi CAR | CAMERA

Assembling base

I recommend to check out this video on how to assemble the car base

How to Assemble a 4WD Robot Smart Car Chassis Kits

When you are done with the base you need to wire motors to the driver module and connect the ESP32

Wiring

Wire your components as in the diagram below.

Flashing

There is no MicroUSB port onboard so you will need a special converter from UART to the USB. I’ve covered flashing here.

In the logs, you should see ESP32 IP. In my case it’s 192.168.4.1 it may be different for you.

After you have flashed the dev board please connect it back to the driver.

Turning ON

When turning on, the car will blink the LED for 1 second it means that the wifi access point is working and HTTP serves is up and running.

And yes I’ve said serverS because there are actually two HTTP servers
running at the same time. One is for driving and the other one is for the camera.

That’s how powerful ESP32 is it can run two HTTP servers at the same time but let’s discuss it’s later

Let’s go deeper, how does it work?

In this section, we will check how it works for both the hardware and software parts. I will go over the project’s files trying to explain key components. The code is very straightforward.

Drivers

Okay, so let's start with the main feature of the project which is controlling our drivers. We would be using the L298N driver module which is restricted to two motors meaning that we won’t be able to control all four motors separately at the same time. Driving two motors on the same side as one solves the issue.

On the module, there are 4 control ports: IN1 IN2 IN3 IN4 and two output ports A and B

To power the motor connected to output A, you should set IN1 to high and IN2 to low signals. If you change IN1 to low and IN2 to high rotor will spin in the other direction. That’s basically how turning and back movement are implemented.

When turning the car what’s happens is that one side spins straight and another backward similar to how tanks are turning

M1A1 turning demo

Powering the ESP32

2x18650 batteries are enough for driving 4 motors.

The ESP32 module can be powered from the driver module for this ESP32’s 5v pin should be connected to the driver’s 5v out. Also, don’t forget about the GND line.

I also tried to power the car with a lot of AA batteries but it wasn’t working stable

Communication

For controlling the wifi car, the controlling device should be connected to the ESP32’s Wi-Fi, and the ESP32’s IP address should be entered in the browser address bar

A controller can be any device that supports Wi-Fi and has a browser. In my case, it’s an Android smartphone

Communication is done through HTTP protocol. It can potentially be some other protocol but I stick to HTTP because I’m already familiar with the web development and it’s not required to develop and install client applications for controlling the car.

Checking the code

Let me briefly explain how the code works. This time I won’t include any screenshots it takes too much space and I will focus only on key parts of the code.

There are only 5 source files:
1. main.c one file to rule them all
2. connect.c responsible for configuring Wi-Fi Access Point
3. driver.c controlling the motors
4. server.c configures endpoints and runs both HTTP servers
5. camera.c HTTP handle for streaming video from the camera
5. led.c controlling the onboard led

Main.c. If Wi-Fi is initialized successfully the HTTP server will start running

Connect.c Password and SSID are taken from the configuration file

Driver.c The file contains 4 functions for each direction and also one function for stoping the car. Here you can specify different GPIO pins for driving motors. I’ve found it easier to change these pins and flash again than to reconnect wires

Server.c In the serverStart function endpoints are registered and two HTTP servers are initialized. One is responsible for controlling the car and the second for streaming video. The most interesting handler here is on_url_hit
that’s where the server loads the website to the client’s browser.
It takes HTML, javascript, and CSS files from the spiffs storage. All the requests are HTPP POST requests except the base URL.

Camera.c This is where the integration with the camera is implemented.
The code gets raw photo data from the camera converts it’s to JPEG and streams chunk by chunk in the while(true) loops so the thread and the connection never dies. That’s by the way why two HTTP servers are used because you have only one RTOS thread per server and in the case of the video HTTP servers will be stuck inside the while(true) block and won’t be able to process other requests such as requests for controlling the movement of the car.

Led.c Not the core functionality but still the very cool feature. The LED allows you to drive through the dark. This code just toggles the onboard LED which by the way is quite bright.

bright led

Website(HTML/CSS/JS)

The website is built on bare javascript without fancy frontend frameworks like Angular, VueJS, etc.

On the page there are:
1. video frame for streaming video from the camera
2. 4 arrows for controlling the movement of the car
3. stop button fallback option for manually stopping the car
4. led toggle button for controlling the onboard led

The only interesting part here is to load video from the same origin but with a different port. In the window.onload callback function the video src attribute is overridden with the HTTP video server.

Summary

You can make lots of improvements to this project. I would start with adding PWM so the controlling would be smoother. It’s also easy to migrate to tank tracks for example this. You could also add servo motors for rotating the camera

I really love ESP32 for its power and simplicity it’s really easy to get started and make exciting projects like this one. You are also not obligated to use the esp-idf framework which is more advanced but you can use the friendlier Arduino libraries.

!!!THANK YOU!!!

--

--