How to Build a Simple Wi-Fi IoT Robot With an ESP32 and Raspberry Pi That Nobody Else Knows About
I have been fascinated with building robots from a young age. Recently, I discovered a way to build a simple Wi-Fi enabled robot using just a few components that most hobbyists likely already have on hand - an ESP32 microcontroller and a Raspberry Pi. This combination allows you to control a small robot remotely using Wi-Fi, opening up many cool project possibilities.
Selecting the Right Hardware Components
The key components I used for this project are:
- ESP32 - This small but powerful microcontroller includes integrated Wi-Fi and Bluetooth connectivity. It can be programmed using Arduino IDE.
- Raspberry Pi - The Raspberry Pi single board computer provides the brains for more complex logic and processing. I used a Raspberry Pi 3 Model B.
- Motor Driver - To drive the robots motors, I used an L298N dual motor driver module. This handles delivering power to the motors.
- Motors - For the robot's wheels, two small DC motors are ideal. I used cheap surplus motors.
- Chassis - The base of the robot was a simple 2WD robot chassis I had on hand. Many options would work here.
- Battery - A portable USB power bank provides untethered power for the robot.
This combination of readily available components enabled building a Wi-Fi controlled robot on the cheap!
Hooking up the Electronics
With the main components selected, I first wired up the ESP32 and the motor driver:
- Connect ESP32 pins to the L298N to control the motors. For example, connect pin 16 to IN1, pin 17 to IN2, etc.
- Attach motors to OUT1 and OUT2 on the driver.
- Supply power to the motors and L298N module.
Next, I added connections between the ESP32 and Raspberry Pi:
- Connect appropriate GPIO pins between the ESP32 and Pi. This allows communication.
- The ESP32 should connect to the Pi via serial UART. For example, connect ESP32 pin U0RXD to Pi pin TXD.
Finally, provide power:
- Use a portable USB power bank to power the Pi and motors.
- The ESP32 can be powered directly from the Pi.
With the basic electronics hooked up, it was time to program each device.
Programming the ESP32 and Raspberry Pi
To program the ESP32, I used the Arduino IDE with the ESP32 add-on. This allows programming the ESP32 using familiar Arduino functions.
The code loaded on the ESP32 accomplished a few key tasks:
- Initialize Wi-Fi connectivity and connect to a network
- Create a web server to receive control commands via HTTP
- Take commands from the web server and control the motors
- Communicate with the Raspberry Pi over serial
For the Raspberry Pi, I wrote a simple Python script:
- Initialize serial communication with the ESP32
- Send motor control commands over serial based on input
- Provide functions for forward, reverse, left, right, stop, etc.
With these programs loaded on each device, I now had bi-directional communication between the ESP32 and Pi!
Controlling the Robot Remotely
To wirelessly control the robot, I simply needed to send HTTP requests to the web server running on the ESP32.
For basic manual control, I used a simple web interface on my laptop with buttons to:
- Go forward/back
- Turn left/right
- Stop
By clicking a button, it would send a request like http://192.168.1.101/forward
to the ESP32 to drive the motors.
I could also control the robot by curling URLs from the command line. This enabled scripting more complex movements.
Finally, for voice control I made a Python script that uses Speech Recognition to listen for commands and then curl the appropriate URL to the ESP32. Now I could simply say "Move forward" and the robot would spring to life!
Wrapping Up
Building this simple Wi-Fi robot ended up being a fun weekend project using components I already had on hand. With an ESP32, Raspberry Pi, and a few other electronics, I was able to create a robot that can be controlled from anywhere!
While basic, this project provides a foundation for building more advanced Wi-Fi robots. Some ideas for enhancements include:
- Adding sensors for collision detection
- Mounting a camera for streaming video
- Enabling autonomous navigation
- Control via Bluetooth instead of Wi-Fi
I learned a ton about interfacing microcontrollers and single board computers on this robot project. Let me know if you end up building your own ESP32 Wi-Fi robot!