ESP32 Balancing Robot
So, for some time I've been working on a good ESP32 based, stepper motors, WiFi connection balancing robot that could also have everything on the same PCB. After a few attempts, I finally have a good enough version, actually it's just a combination of a few amazing projects I found on the internet. This balancing robot drives very well, the code is based on electrical-pro's project, all components are on the PCB, everything is explained in this tutorial and you can also find the part list below. So, if you want to make a similar robot, follow this tutorial. Currently there is only an Android version of the controller app, it supports robot control and telemetry. For iPhone users, just open a browser and use the robot's control interface. So, let's see how this works and how to build it.
Supplies
We need a lot of parts but don't worry, nothing difficult to get. We need the ESP32 chip, the sensor, the motors, and a few other components such as resistors, capacitors and LEDs. Check each component below. If the link is not updated, just go on eBay and search for the component using the names below.
For the robot we need:
- 2 x Motors : https://s.click.aliexpress.com/e/_AUzpWd
- 2 x Wheels (5mm inner bore) : https://s.click.aliexpress.com/e/_9idvRF
- DVR8825 stepper motor driver : https://s.click.aliexpress.com/e/_9xnMc5
- DC-DC Converter : https://s.click.aliexpress.com/e/_97wh9L
- ESP32 : https://s.click.aliexpress.com/e/_AnUQEp
- MPU6050 Gyroscope & Accelerometer Module : https://s.click.aliexpress.com/e/_A6SRW5
- INA219 Voltage & Current sensor : https://s.click.aliexpress.com/e/_9wVQ7L
- Pin Header : https://s.click.aliexpress.com/e/_A5RFzL
- Electrolytic Capacitor : https://s.click.aliexpress.com/e/_9gdUYl
- Ceramic Capacitors : https://s.click.aliexpress.com/e/_AfrfzL
- Resistors : https://s.click.aliexpress.com/e/_A3BVAH
- 1N5822 schottky diodes : https://s.click.aliexpress.com/e/_AZ47Tn
- 2P 5mm Screw Wire Terminal Block : https://s.click.aliexpress.com/e/_ArnMyl
- 3D printed parts
Robot Schematic
The schematic is just a simplified version of electrical-pro's. First we have the power block where we regulate our 5V for the ESP32 module. The ESP32 chip provides 3.3V to INA219, MPU6050 and DRV8825. To control the motors we use 2 DRV8825s, which gets its power from the lipo battery. The lipo battery is just a typical 3S1P RC battery, these battery have a operating voltage between 12.6 to 11.1 volts, which is within the limit of our DC-DC converter in the power block. Get the PCB and the components and following the markings on the schematic, solder the PCB.
Soldering the PCB
This is a PCB I designed to hold all the components, except stepper motors. Print the PCB with services such as PCBWay or JLCPCB using the Gerber file.
Gerber file download link : https://supr.link/lIQMe
Before soldering the rest of the components, make sure we have a good 5V voltage, otherwise we might burn the ESP32 module because the default output voltage of my DC-DC converter is around 10V. So, connect the MP1584EN DC-DC converter to a power supply, slowly turn the voltage adjustment screw clockwise to lower the output voltage, stop when the output voltage reaches 5V.
Ok, at this point we can solder the rest of the components onto our PCB.
Programming ESP32
First, download the code here : https://supr.link/DZjwK. It's basically the same as the original code, only with a few modification to make it work with my schematic. To build the code successfully, you need to copy all the libraries in the "lib" folder to "C:\Users\My Username\Documents\Arduino\libraries".
Now is a good chance to configure the robot's AP. In the first few lines of "defines.h", you'll find variables ROBOT_NAME and AP_PASSWORD, if you have multiple robots, each robot should have a unique ROBOT_NAME.
After that, follow this tutorial to build and upload the code. For my ESP32, I had to select "ESP32 Dev Module" under Tools ->Board. If everything went as expected, you should see a “Done uploading.” message.
Also, don't forget to upload the sketch data, which are required for the control and setup pages to work correctly. In the Arduino IDE, go to Tools -> ESP32 Sketch Data Upload. When the process is complete, you should see "SPiFFS Image Uploaded" in the IDE.
Print and Build the Frame
Parts are modified from this beautiful frame designed by DaveGun. Original files are here.
All parts are assembled together using M3 8mm and 12mm bolts, which screw directly into the 3D printed parts, no nuts needed. Button head screws are recommended.
For the robot we need:
- M3 8mm x 12 : https://s.click.aliexpress.com/e/_AXVe77
- M3 12mm x 4 : https://s.click.aliexpress.com/e/_AXVe77
Install Controller App
Install Balancing Robot Controller from Google Play Store.
I designed this simple App to control the balancing robot. On the first startup, the robot will configure to AP(Access Point) mode with SSID "Balancing Robot" and password "12345678". Connect your WiFi to this access point. Once connection is established, open the App and should see telemetry data from the robot, including speed, voltage, power...etc, if not just press the Connect button to connect manually.
Note that connecting to a WiFi without internet might not work on some devices.
If you're using an iPhone, the built in web interface is the only way to control and setup the robot. After connecting to the robot's AP, open a browser and access the following pages:
- Robot Control : http://192.168.4.1/control.htm
- Robot Setup : http://192.168.4.1/index.htm
For some reason, the control interface doesn't work correctly on Firefox.
Final Steps
Finally it's time to add the wheels and the battery. First, make sure you calibrate the gyro and accelerometer using Balancing Robot Controller App or web interface. Then check if the stepper motors rotate in the correct direction. There is a small arrow on the PCB pointing to the front of the robot, when you tilt the robot forward, both motors should spin forward. Once everything is setup correctly, put on the wheels and connect the battery, the robot should start balancing itself after a few beeps.
And Race!
The best way to control this robot is using the App. If there are multiple robots in the same space, make sure you setup different SSIDs for each robot. Have fun!
Reference
- https://github.com/electrical-pro/ESP32_Robot
- https://github.com/bluino/esp32_wifi_balancing_robot
- https://www.jjrobots.com/much-more-than-a-self-balancing-robot/
- https://www.thingiverse.com/thing:2863280