Projects / Wi-Fi robot car with a phone joystick

Wi-Fi robot car with a phone joystick

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP32ROBOT

    Assemble the chassis

    Motors, wheels and a caster.

  2. ESP32LM2596

    Set the buck module to 5.0 V

    With a multimeter, no load.

  3. ESP32

    Wire the TB6612 and ESP32

    As in the table.

  4. ESP32

    Install the WebSockets library

    By Markus Sattler.

  5. ESP32

    Upload with the wheels in the air

    Check each wheel direction.

  6. ESP32ROBOT

    Join the RobotCar Wi-Fi

    Open the page and drag the joystick.

  7. ESP32

    Swap motor wires if a wheel runs the wrong way

    A01 and A02.

  8. ESP32

    Drive on the floor

    Check the failsafe by switching off the phone Wi-Fi.

In short

How do you build a wi-Fi robot car with a phone joystick?

An ESP32 makes its own Wi-Fi network and serves a web page with a touch joystick, then drives a two-wheel car over WebSockets with low delay. You need ESP32 DevKit V1, TB6612FNG Motor Driver, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, LM2596 Buck Module and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
18650 holder (2 cells in series, 7.4 V) plusTB6612 VM and the LM2596 IN+Motor supply and buck input
18650 holder minusTB6612 GND, LM2596 IN-, ESP32 GNDCommon ground
LM2596 OUT set to 5.0 VESP32 VIN pinSet the output with a multimeter before connecting
ESP32 3V3TB6612 VCC and STBYLogic power and enable
ESP32 GPIO26, GPIO27, GPIO25TB6612 AIN1, AIN2, PWMALeft motor
ESP32 GPIO14, GPIO32, GPIO13TB6612 BIN1, BIN2, PWMBRight motor
TB6612 A01, A02Left motorSwap the two wires if it runs backwards
TB6612 B01, B02Right motorSwap the two wires if it runs backwards

Code

The ESP32 starts its own Wi-Fi hotspot called RobotCar, so it works outdoors with no router. Join it with your phone, open http://192.168.4.1 and drag the on-screen joystick. The page sends the stick position as two numbers, x for turn and y for forward, twenty times a second through a WebSocket. WebSockets keep one connection open, which gives much less delay than reloading a page for each command. The ESP32 mixes the two numbers into wheel speeds: left = y + x and right = y - x, so pushing the stick forward and right makes the left wheel faster and the car turns right. Two safety features keep it from running away: if the phone disconnects or no message arrives for half a second, the car stops. A small dead zone ignores tiny stick movements. Power: two 18650 cells in series give about 7.4 V for the motors through the TB6612, and an LM2596 buck module steps it down to 5 V for the ESP32 VIN pin. Turn the buck module's trimmer until a multimeter reads 5.0 V before connecting the ESP32. Use protected cells in a proper holder, never charge them unattended, and fit a switch. Change the access point password in the sketch before you drive. Setup: Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module". Install "WebSockets" by Markus Sattler from the Library Manager. Wheels off the ground for the first test.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>     // library "WebSockets" by Markus Sattler

const char *AP_SSID = "RobotCar";
const char *AP_PASS = "CHANGE_ME_8PLUS";   // placeholder: choose your own password, at least 8 characters

const uint8_t AIN1 = 26, AIN2 = 27, PWMA = 25;
const uint8_t BIN1 = 14, BIN2 = 32, PWMB = 13;
const uint32_t FAILSAFE_MS = 500;

WebServer server(80);
WebSocketsServer ws(81);
uint32_t lastMsg = 0;

const char PAGE[] PROGMEM = R"rawliteral(
<!DOCTYPE html><html><head>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<style>
body{margin:0;background:#111;color:#eee;font-family:sans-serif;text-align:center;touch-action:none}
#pad{width:240px;height:240px;border-radius:50%;background:#222;margin:40px auto;position:relative;touch-action:none}
#knob{width:80px;height:80px;border-radius:50%;background:#4af;position:absolute;left:80px;top:80px}
</style></head><body>
<h2>Robot car</h2>
<div id="pad"><div id="knob"></div></div>
<p id="st">connecting</p>
<script>
var ws=new WebSocket("ws://"+location.hostname+":81/");
var x=0,y=0,pad=document.getElementById("pad"),knob=document.getElementById("knob"),st=document.getElementById("st");
ws.onopen=function(){st.textContent="connected"};
ws.onclose=function(){st.textContent="closed"};
function move(e){
  var r=pad.getBoundingClientRect();
  var dx=e.clientX-(r.left+120),dy=e.clientY-(r.top+120);
  var d=Math.sqrt(dx*dx+dy*dy);
  if(d>120){dx=dx*120/d;dy=dy*120/d;}
  knob.style.left=(80+dx)+"px";knob.style.top=(80+dy)+"px";
  x=Math.round(dx/1.2);y=Math.round(-dy/1.2);
}
function stop(){knob.style.left="80px";knob.style.top="80px";x=0;y=0;}
pad.addEventListener("pointerdown",function(e){pad.setPointerCapture(e.pointerId);move(e);});
pad.addEventListener("pointermove",function(e){if(e.buttons)move(e);});
pad.addEventListener("pointerup",stop);
pad.addEventListener("pointercancel",stop);
setInterval(function(){if(ws.readyState==1)ws.send(x+","+y);},50);
</script></body></html>
)rawliteral";

void setMotor(uint8_t in1, uint8_t in2, uint8_t pwm, int speed) {   // speed -100 to 100
  if (abs(speed) < 10) speed = 0;                                   // dead zone near the centre
  digitalWrite(in1, speed > 0);
  digitalWrite(in2, speed < 0);
  ledcWrite(pwm, abs(speed) * 255 / 100);
}

void drive(int x, int y) {
  int left = constrain(y + x, -100, 100);      // mix: forward plus turn
  int right = constrain(y - x, -100, 100);
  setMotor(AIN1, AIN2, PWMA, left);
  setMotor(BIN1, BIN2, PWMB, right);
}

void onWs(uint8_t num, WStype_t type, uint8_t *payload, size_t len) {
  if (type == WStype_DISCONNECTED) {
    drive(0, 0);                                // phone gone: stop
  } else if (type == WStype_TEXT) {
    int x = 0, y = 0;
    if (sscanf((const char *)payload, "%d,%d", &x, &y) == 2) {
      drive(constrain(x, -100, 100), constrain(y, -100, 100));
      lastMsg = millis();
    }
  }
}

void setup() {
  uint8_t pins[] = {AIN1, AIN2, BIN1, BIN2};
  for (uint8_t p : pins) pinMode(p, OUTPUT);
  ledcAttach(PWMA, 20000, 8);                 // ESP32 core 3: PWM is set up per pin
  ledcAttach(PWMB, 20000, 8);
  drive(0, 0);
  WiFi.softAP(AP_SSID, AP_PASS);               // the car is its own Wi-Fi hotspot, no router needed
  server.on("/", []() { server.send_P(200, "text/html", PAGE); });
  server.begin();
  ws.begin();
  ws.onEvent(onWs);
}

void loop() {
  server.handleClient();
  ws.loop();
  if (millis() - lastMsg > FAILSAFE_MS) drive(0, 0);   // no message for half a second: stop
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Join the hotspot and open 192.168.4.1Joystick page loads, status says connected
Push the stick upBoth wheels turn forward
Push it rightCar turns right
Let goMotors stop
Turn phone Wi-Fi offCar stops within half a second

If it does not work

Page does not load

Check you are on the RobotCar network and use http, not https.

Page loads, car does not move

Check the WebSockets library and the TB6612 STBY pin on 3V3.

One wheel backwards

Swap its two motor wires.

ESP32 resets when motors start

Check the buck module, add a 470 uF capacitor on the 5 V line.

Car keeps creeping

Increase the dead zone in setMotor.

Buck output wrong

Re-set the trimmer to 5.0 V.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the wi-Fi robot car with a phone joystick?

ESP32 DevKit V1, TB6612FNG Motor Driver, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, LM2596 Buck Module and Jumper wires male to male (40 pcs, 20 cm).

How long does the wi-Fi robot car with a phone joystick take to build?

About 180 minutes for a intermediate maker, in 8 steps.

How do I know the wi-Fi robot car with a phone joystick works?

Join the hotspot and open 192.168.4.1: Joystick page loads, status says connected. Push the stick up: Both wheels turn forward. Push it right: Car turns right.

Page does not load: what should I check?

Check you are on the RobotCar network and use http, not https.

Page loads, car does not move: what should I check?

Check the WebSockets library and the TB6612 STBY pin on 3V3.

One wheel backwards: what should I check?

Swap its two motor wires.

ESP32 resets when motors start: what should I check?

Check the buck module, add a 470 uF capacitor on the 5 V line.