Hello friends, I hope you all are doing great. Today, I am going to share the Differential Drive Robot with Joystick and ESP32 - Proteus Simulation, the seventy-third project of our IoT Projects series. If you have searched for an ESP32 joystick robot, how to drive FS90R continuous servos, arcade drive mixing, or robot odometry and dead reckoning, here is a complete Proteus simulation of one. Most small robots, from robot vacuums to school rovers, move on two driven wheels: drive both the same and they go straight, drive them differently and they turn. This ESP32 reads a thumb joystick, turns it into two wheel speeds, drives two FS90R continuous-rotation servos as the wheels, and works out from those speeds where the robot has gone.
Each of those steps hides a piece of real robotics. A joystick has two axes but a robot has two wheels, so the stick must be mixed into wheel speeds. A continuous servo turns a pulse width into a speed, but not in a straight line, so the ESP32 must turn the servo's speed curve around. And the position comes from the kinematics of differential drive, integrated step by step, which is called dead reckoning. In this tutorial, we will go through the circuit, the joystick and its dead zone, arcade mixing, the FS90R's speed curve, the kinematics and the odometry. Then we drive the robot in the simulation: straight ahead, a U-turn, a spin on the spot, and finally with a load on one wheel, which shows why real robots need wheel encoders. At the end, you can download the project with the sketch and every library file.
NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the differential drive robot with joystick and ESP32 in Proteus simulation:
What Does This Robot Do?
- Reads a thumb joystick: a KY-023 module with two potentiometers; forward / back on one ADC pin, left / right on another, its push switch to zero the position.
- Mixes the stick into two wheels: arcade drive, the left wheel = forward + turn, the right wheel = forward - turn.
- Drives two FS90R continuous servos: the pulse width sets the speed; the ESP32 turns the servo's speed curve around to get the speed it wants.
- Knows that the wheels face opposite ways: the left servo turns counter-clockwise to drive forward, the right one clockwise.
- Stops cleanly: with the stick centred, the servos get no pulses at all, so they cannot creep.
- Works out where it is: differential drive kinematics every 50 ms, the position x and y and the heading, by dead reckoning.
- Shows the path: a map on a 0.96" OLED and the same map, bigger, on a dashboard, on a laptop or a phone.
It is a great project for learning ADC reading, servo pulses, a little trigonometry and the basics of mobile robots in one go. In the simulation, the joystick's pop-up has a D-pad and a SPRING switch, so the stick stays where it is put, and each FS90R's pop-up shows the speed it really turns at, the pulse it receives and a set of load buttons for its horn.
Components Used in the Proteus Simulation
All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.
| Part | Proteus device | Library post | Connection |
|---|---|---|---|
| ESP32 DevKit V1 | ESP32 DEVKIT V1 | ESP32 DevKit V1 Library for Proteus | U1, ESP32_Robot_Base.hex, TEP-WiFi 192.168.1.101 |
| KY-023 joystick module | JOYSTICKADVTEP | Joystick Module Library for Proteus | U2: VRX D34, VRY D35, SW D32, VCC +3.3V |
| FS90R continuous servo (left wheel) | FS90RSERVOTEP | FS90R Continuous Servo Library for Proteus | SERVO1: SIG D25, V+ +5V |
| FS90R continuous servo (right wheel) | FS90RSERVOTEP | FS90R Continuous Servo Library for Proteus | SERVO2: SIG D26, V+ +5V |
| SSD1306 OLED 0.96" 128x64 | OLED12864WHITEADVTEP | OLED SSD1306 Library for Proteus | OLED1, I2C at 0x3C |
| Laptop WiFi Monitor | LAPTOP WIFI MONITOR | Laptop WiFi Monitor | PC1 |
| Mobile WiFi Monitor | MOBILE WIFI MONITOR | Mobile WiFi Monitor | MOB1 |
Proteus Simulation of the Robot: the Circuit
| Part | Pins | ESP32 / supply |
|---|---|---|
| U2 joystick | VCC / GND / VRX / VRY / SW | +3.3V / GND / D34 / D35 / D32 |
| SERVO1 left wheel | V+ / GND / SIG | +5V / GND / D25 |
| SERVO2 right wheel | V+ / GND / SIG | +5V / GND / D26 |
| OLED1 | SDA / SCL / VCC | D21 / D22 / +3.3V |
The joystick is two 10k potentiometers, each a divider from VCC to ground with its wiper on VRX or VRY. Powered from 3.3 V, the wipers stay inside the ESP32's ADC range, on D34 and D35, two ADC1 pins that keep working while Wi-Fi is on. The push switch shorts SW to ground; the board has no pull-up, so the sketch turns on the ESP32's own. The two FS90R servos need 4.8 to 6 V on V+, so they sit on a +5V supply, and their SIG inputs take 3.3 V servo pulses from D25 and D26. The OLED shares the I2C bus on D21 and D22. In the simulation the joystick's SPRING property is OFF, so the stick stays where it is pushed instead of springing back.
How the Differential Drive Robot Works
The Joystick and Its Dead Zone
A released stick does not come back to the exact middle: this one rests at 520 and 506 counts of 1023, 1.675 V and 1.632 V on 3.3 V. So the sketch measures the rest voltages at start-up and works from there. Each axis becomes a number from -1 to +1: the distance from the rest, divided by the distance from the rest to the end on that side. Then comes a dead zone of 8 % of the travel around the centre, where the robot ignores the stick, so the small wobble of a released stick never moves the wheels. Outside it, the axis is stretched again, so it still reaches exactly 1 at the end.
Arcade Mixing
Arcade drive is the classic mix for one stick and two wheels: forward and back make both wheels turn the same way, left and right add to one wheel and take from the other. The sketch uses left = forward + 0.25 x turn and right = forward - 0.25 x turn, and if either passes 1, both are scaled down together, so the ratio between them, and therefore the curve, is kept. Worked example: the stick up and right, forward = 1 and turn = 1, gives left = 1.25 and right = 0.75. Scaled by 1.25, that is left = 1 and right = 0.6: the left wheel at full speed, the right one at 60 %, a curve to the right. The stick right alone gives left = 0.25 and right = -0.25: the wheels in opposite directions, a spin on the spot. The factor 0.25 keeps the turns gentle enough to steer; with 1 the robot would spin at almost 400 degrees a second.
Turning the FS90R's Speed Curve Around
An FS90R looks like a normal hobby servo, but its pulse sets a speed instead of an angle. A 1500 microsecond pulse stops it; longer pulses turn it counter-clockwise, shorter ones clockwise, faster the further from 1500. The relation is not a straight line. After a dead band of 5 microseconds, the drive to its motor rises quickly, then flattens out: half drive at 100 microseconds from the stop, 85 % at 200 and 96 % at 300, which follows drive = tanh((microseconds - 5) / 150) closely. But the wheel's speed is not the drive: the motor first has to beat its own friction. Our first test run measured it: 114.4 RPM at 300 microseconds, but only 14.3 RPM at 41 microseconds, where the drive alone predicted twice that. Both points fit RPM = 137.8 x (drive - 0.13): friction eats the first 13 % of the drive, and the top speed comes out at 119.6 RPM, just what the servo's datasheet gives at 5 V.
To get a wheel speed it wants, the sketch runs the whole curve backwards: drive = RPM / 137.8 + 0.13, then microseconds = 5 + 150 x atanh(drive). Worked example: the right wheel at 60 % of 115 RPM, 69 RPM, needs a drive of 69 / 137.8 + 0.13 = 0.631, and 5 + 150 x atanh(0.631) = 116 microseconds, a pulse of 1500 - 116 = 1384 microseconds. The two servos face opposite ways on a robot, so for forward the left one gets pulses above 1500 and the right one pulses below it. This kind of calibration, measure, fit, turn around, is exactly what you would do with two real servos on your bench.
Differential Drive Kinematics
Each wheel's speed along the ground is its turning speed times its circumference: v = RPM x 2 x pi x r / 60. With 60 mm wheels at 115 RPM that is 115 x 2 x 3.1416 x 0.030 / 60 = 0.361 m/s. The robot moves at the average of its two wheels, v = (vL + vR) / 2, and turns at their difference divided by the distance between them, w = (vR - vL) / track, in radians per second, positive to the left. Worked example for the curve above: vL = 0.361 and vR = 0.6 x 0.361 = 0.217 m/s, so v = 0.289 m/s and w = (0.217 - 0.361) / 0.100 = -1.445 rad/s, 83 degrees a second to the right, on a circle of radius v / w = 0.2 m. A U-turn, 180 degrees, takes 180 / 83 = 2.2 s. For the spin, vL = 0.090 and vR = -0.090 m/s give v = 0 and w = -1.806 rad/s, 103 degrees a second on the spot.
Dead Reckoning
About every 50 ms the sketch moves the robot by those speeds for the time dt that really passed: heading = heading + w x dt, then x = x + v x cos(heading) x dt and y = y + v x sin(heading) x dt. The real dt matters: while the ESP32 sends a web page the loop is held up, and a sketch that always assumed 50 ms simply lost that time (one of our test runs dropped 40 % of a straight run this way). The robot starts at x = 0 and y = 0 facing north, a heading of 90 degrees. This is dead reckoning, the way ships found their position before satellites: from the speed, the direction and the time, without ever looking outside. Its weakness is built in. Every error, a wheel a little slower than the sketch thinks, a little slip on the floor, is added up and never corrected, so the estimate drifts further from the truth the longer the robot drives. That is why real robots measure their wheels with encoders, and add a gyroscope or landmarks on top.
The Arduino Code Explained
The sketch is ESP32_Robot_Base.ino, written in the Arduino IDE for the ESP32 DevKit V1. It uses ESP32Servo for the two servos, Adafruit SSD1306 for the OLED, and WiFi and WebServer for the dashboard. Each joystick axis, with the dead zone taken out:
float a = mv >= c ? (mv - c) / float(3300 - c) : (mv - c) / float(c);
a = (fabs(a) - DEAD) / (1 - DEAD) * (a > 0 ? 1 : -1);
The arcade mix, scaled down together when a wheel passes full speed:
float l = jy + TURN * jx, r = jy - TURN * jx, m = max(fabs(l), fabs(r));
if (m > 1) { l /= m; r /= m; }
The FS90R's speed curve turned around: the drive the speed needs, friction first, then atanh written out because the board's maths library has none:
float d = min(f * MAX_RPM / K_RPM + D0, 0.99f); // the drive this speed needs (friction first)
return BAND + SPAN * 0.5 * log((1 + d) / (1 - d)); // (atanh(d): the board's maths library has no atanh)
The pulses, with the right servo facing the other way:
usL = 1500 + int(usFor(l) * (l > 0 ? 1 : -1)); // the left servo: CCW (longer) = forward
usR = 1500 - int(usFor(r) * (r > 0 ? 1 : -1)); // the right servo faces the other way: CW (shorter) = forward
With the stick centred, the servos are detached, so they get no pulses and cannot creep:
if (!go && driving) { left.detach(); right.detach(); } // (no pulses: an FS90R stops dead - no creep)
The kinematics and the dead reckoning, for the time that really passed, at the wheel speeds of that time, before the new command:
odometry(a / 1000.0); // (the time that really passed, at the speeds of that time: a web
w = (vR - vL) / TRACK; // rad/s, positive = turning left
x += v * cos(heading * PI / 180) * dt;
y += v * sin(heading * PI / 180) * dt;
The OLED gets a new frame every 250 ms: the position, the heading, the two wheels and the distance on the left, and on the right a 64 x 64 map with the start in the middle, north up, the last 60 points of the path (one every half second) and the robot as a dot with a line for its heading. The map scales itself so the whole path fits. Every 2 seconds a report line goes to the serial monitor. The pages are / (the dashboard), /map.bmp (the OLED's map, twice as big, read straight from the OLED's memory and sent eight rows at a time, so it needs no extra memory) and /data (JSON).
How to Run the Proteus Simulation
- Download the project zip below and install the library files (the steps are under the download button).
- Open ESP32-Robot-Base.pdsprj from the Proteus Simulation folder. Keep the HEX file and the DLL files next to it.
- Press Play. The robot starts at (0, 0) facing north.
- On the joystick's pop-up (U2) press UP, then UR, UP, RIGHT and CTR. On the left wheel's pop-up (SERVO1) press 0.50 kg.cm, then UP again.
- Open the Laptop or the Mobile WiFi Monitor and press Go for the dashboard.
Simulation Results: the Robot in Proteus
We drove the whole route in one simulation: straight ahead, a U-turn, back again, a spin on the spot, a stop, and finally straight ahead with a load on the left wheel. At start-up the sketch measured the stick's rest, 1677 and 1628 mV. Then we pressed UP on the joystick's pop-up:
6.4 s stick 1.00/0.00 wheels 115/115 RPM (1806/1194 us) v 0.361 m/s, w 0.0 deg/s at -0.00, 0.85 m, heading 90, 0.85 m driven
Both wheels got their full-speed pulses, 1806 microseconds for the left servo and 1194 for the right one, mirror images around 1500. The left wheel's pop-up confirms the calibration, 114.8 RPM where the sketch asked for 115. The robot covered 0.72 m every 2 seconds, exactly 0.361 m/s, and the OLED and the dashboard draw the straight line north. (The joystick's pop-up names the position CENTER because its word follows the 5 V demo sketch's rule; on 3.3 V, fully up is 3.3 V, which the voltage shows.)
At 17.7 seconds we pressed UR, up and right, and the mix gave the right wheel 60 %:
18.4 s stick 1.00/0.98 wheels 115/70 RPM (1806/1382 us) v 0.290 m/s, w -81.2 deg/s at 0.10, 5.10 m, heading 34, 5.14 m driven
The right wheel turned at 69.3 RPM where the sketch asked for 70, so the turned-around curve holds in the middle of its range too. The robot turned right at 81 degrees a second, the worked example's 83 with the stick at 98 % instead of 100 %. After 2.45 seconds we pressed UP again: the heading was 257 degrees, a U-turn of 193 degrees on a circle of about 0.2 m, and the robot drove back towards the start, a hairpin on the map:
At 31.7 seconds we pressed RIGHT, the stick to the side only. Now the wheels turn in opposite directions at 25 % each:
32.4 s stick 0.00/1.00 wheels 29/-29 RPM (1557/1557 us) v 0.000 m/s, w -103.5 deg/s at -0.52, 0.78 m, heading 181, 9.83 m driven
The position stays put and only the heading turns, 103.5 degrees a second. Both servos get the same pulse, 1557 microseconds, because they face opposite ways: the same pulse turns them the same way on the bench, which drives the two wheels in opposite directions on the robot. This is where the friction term earns its place. In our first test run, without it, the sketch sent 1541 microseconds for the same 29 RPM, and the servo only crept at 14.3 RPM. Now it turns at 27.7. Then we pressed CTR: the servos were detached, and the pop-up shows no pulses at all and the wheel stopped dead.
Finally, at 40.2 seconds, we pressed 0.50 kg.cm on the left wheel's pop-up, as if that wheel were rubbing on something, and pressed UP. The pop-ups tell the true story:
The left wheel slowed to 70.8 RPM, the right one kept 114.8, and the ESP32 had no way to know: it kept sending full-speed pulses and kept drawing a straight line, 6.8 m north-east by the end. A real robot with these wheel speeds would not go straight at all. The left wheel moves 0.222 m/s and the right 0.361 m/s, so the robot turns left at (0.361 - 0.222) / 0.100 = 1.39 rad/s, 80 degrees a second. In the 19 seconds of that run it would have spun round four times on a circle of 0.21 m, while the dead reckoning claims a straight line. This is the limit of dead reckoning from commands, and the reason real robots count their wheels' turns with encoders.
The run covered 62 seconds of simulation in 378 seconds on our PC, 16.7 m of driving in all. The servos' pop-ups were an independent judge throughout: every wheel speed the ESP32 assumed could be checked against the speed the servo really turned at, which is exactly what you cannot do with a real robot unless you add the encoders.
Download the Project Files
Click the button below to download Differential-Drive-Robot-Joystick-ESP32-Proteus-Simulation-v1.0.zip (about 3.8 MB, without the source code of the models):
Differential Drive Robot with Joystick and ESP32 - Proteus Simulation V1.0- Proteus Simulation: ESP32-Robot-Base.pdsprj, ESP32_Robot_Base.hex and copies of the models.
- Arduino Code: ESP32_Robot_Base.ino, the sketch.
- Proteus Library Files: the LIB files of the ESP32 DevKit V1, the joystick, the FS90R servo, the OLED and the two WiFi Monitors.
- Proteus Model Files: the DLL models.
- README.txt: the installation steps, the properties, the test results and the links to every library post.
No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.
How to Install the Project Files
- Close Proteus and extract the whole zip file, for example to your Desktop.
- Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
- Copy the files from Proteus Model Files into the MODELS folder.
- Start Proteus and open the project from the Proteus Simulation folder.
Changing the Code
Open ESP32_Robot_Base.ino in the Arduino IDE with the TEP boards for Proteus installed, compile it, export the compiled binary and set U1's PROGRAM property to the new HEX file. The dead zone, the turn factor, the top speed, the wheel size, the track and the speed curve are at the top. Set the joystick's SPRING property to ON for a stick that springs back to the centre, as on the real module.
Ideas to Extend the Project
- Add wheel encoders (a slotted disc and an optical sensor per wheel) and measure the real wheel speeds instead of assuming them.
- Close the loop: a PI controller per wheel that holds the speed the stick asks for, whatever the load.
- Add a gyroscope such as an MPU6050 and correct the heading, the part of dead reckoning that drifts fastest.
- Drive the robot from the dashboard instead of the joystick, with buttons or a touch pad on the phone.
- Record the path and play it back: the robot drives the same route again on its own.
- Use the push switch to drop waypoints and have the robot return to the start.
From Simulation to Real Hardware
- Power the servos separately: two FS90Rs draw up to 0.6 A each when they stall; give them their own 5 V supply with the grounds joined, not the ESP32's 3.3 V or its USB pin.
- Trim the neutral: a real FS90R has a trim pot; set it so a 1500 microsecond pulse really stops the wheel, or measure each servo's neutral and put it in the sketch.
- Measure your own curve: every servo is a little different; drive each at a few pulse widths, count the turns, and fit the curve to your servos.
- Calibrate the odometry: drive one metre straight and turn 360 degrees, compare with the estimate, and correct the wheel radius and the track.
- Expect slip: wheels slip on smooth floors and when they turn on the spot, an error no encoder can see.
Troubleshooting
- The wheels never turn: the servos' V+ needs the +5V supply terminal; SIG on D25 / D26.
- A wheel creeps with the stick centred: the sketch detaches the servos at the centre; if you change it to send 1500 microseconds instead, a servo with a slightly different neutral will creep.
- The robot drives backwards: the servos face opposite ways; swap the sign of one wheel in the sketch, not the wiring.
- The robot moves with the stick released: with SPRING OFF the stick stays where it was left; press CTR on the D-pad.
- The OLED stays blank: SDA / SCL on D21 / D22, address 0x3C, VCC on 3.3 V.
Limitations of the Simulation
- The robot does not move on the sheet: the servos turn, and the ESP32 works out the path from what it told them, exactly as a robot without encoders does.
- The FS90R model has its own speed curve, its friction and its load; a real servo and real wheels on a real floor differ.
- There is no wheel slip, no floor and no inertia of the robot's body in the simulation.
- The network to the laptop and the phone is simulated inside Proteus.
- ESP32 is a product of Espressif Systems; this project is not made or endorsed by them or by Labcenter.
So, that was all about the Differential Drive Robot with Joystick and ESP32 - Proteus Simulation. I hope it helps you understand how a joystick is read and mixed into two wheels, how a continuous servo's speed curve is turned around, how differential drive kinematics and dead reckoning give a robot its position, and why that position drifts without encoders, all tested in a simulation before you build a real robot. More IoT projects with the ESP32 and the Raspberry Pi 5 are coming in this series. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!