IntermediateInteractive Control

Wi-Fi Robot Car

Drive two DC motors from your phone over Wi-Fi.

Time
3 h
Estimated cost
~£52
Hardware
ESP32-DevKitC V4 (38-pin)

Example code — not yet validated

Try it in the browser

What you will build

A two-wheel robot car that creates its own Wi-Fi network called XBOT-CAR. Join it from your phone, open http://192.168.4.1, and drive with on-screen buttons and a speed slider. If your phone disconnects, the car stops by itself within half a second.

How it works

The ESP32 runs as a Wi-Fi access point and a small web server, so no router is needed. While you hold a button, the page sends a command such as /cmd?c=F&s=180 every 150 ms. The ESP32 sets the TB6612FNG motor driver: two direction pins and one PWM speed pin per motor. The driver takes power from the battery pack and handles the motor current; the ESP32 only sends logic signals. A watchdog stops both motors if no command arrives for 500 ms.

Command set (shared with Project 09 and the browser preview):

Code Meaning
S Stop
F / B Forward / Backward
L / R Spin left / right
s=0–255 Speed (PWM duty)

Parts

Part Qty Purpose Required
ESP32-DevKitC V4 + mini breadboard 1 Controller + Wi-Fi Yes
2WD robot chassis kit (2 × TT gear motors, wheels, caster) 1 Body and drive Yes
TB6612FNG motor driver breakout 1 Drives the motors Yes
4 × AA battery holder with switch + 4 AA batteries 1 Motor power (≈ 6 V) Yes
Small USB power bank 1 ESP32 power Yes
Jumper wires, M3 screws/standoffs or double-sided tape — Mounting Yes

We use the TB6612FNG rather than the older L298N: it wastes far less battery as heat and works directly with 3.3 V logic.

Build steps

  1. Assemble the chassis. Solder or crimp wires to both motors.
  2. Mount the battery holder, power bank, breadboard and driver. Keep wires away from the wheels.
  3. Wire the driver:
    • PWMA → GPIO 25, AIN1 → GPIO 26, AIN2 → GPIO 27 (left motor)
    • PWMB → GPIO 19, BIN1 → GPIO 18, BIN2 → GPIO 23 (right motor)
    • STBY → GPIO 33, VCC → 3V3, GND → GND
    • VM → battery +, battery – → driver GND (shared with ESP32 GND)
    • Left motor → A01/A02, right motor → B01/B02
  4. Power the ESP32 from the power bank via USB.
  5. Upload the code (over USB, battery switch off).
  6. Wheels-up test: rest the car on a box, switch on the batteries, join XBOT-CAR (password xbotnow123), open 192.168.4.1 and press each button.
  7. Change the Wi-Fi password before using the car anywhere else: every car built from this tutorial starts with the same password, so anyone nearby could join and drive yours. Set your own AP_PASS (8 or more characters) in the code and upload it again.
  8. If a wheel spins the wrong way, swap that motor's two wires (or swap AIN1/AIN2 in code).
  9. Checkpoint: put it on the floor and drive. Switch off your phone's Wi-Fi mid-drive: the car stops.

Try it in the browser

Interactive Preview. A top-down virtual car on the project page uses the same command set and speed slider, so you can try the controls and the 500 ms watchdog before building.

Code

Download wifi-robot-car.ino

// Wi-Fi Robot Car — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x · Driver: TB6612FNG
#include <WiFi.h>
#include <WebServer.h>

const char* AP_SSID = "XBOT-CAR";
const char* AP_PASS = "xbotnow123";   // 8+ characters, change it
const int PWMA = 25, AIN1 = 26, AIN2 = 27;
const int PWMB = 19, BIN1 = 18, BIN2 = 23, STBY = 33;
const unsigned long WATCHDOG_MS = 500;

WebServer server(80);
unsigned long lastCmd = 0;

void motor(int in1, int in2, int pwm, int speed) {   // speed -255..255
  digitalWrite(in1, speed > 0);
  digitalWrite(in2, speed < 0);
  ledcWrite(pwm, abs(speed));
}
void drive(int left, int right) { motor(AIN1, AIN2, PWMA, left); motor(BIN1, BIN2, PWMB, right); }

void handleCmd() {
  char c = server.arg("c").length() ? server.arg("c")[0] : 'S';
  int s = constrain(server.arg("s").toInt(), 0, 255);
  switch (c) {
    case 'F': drive( s,  s); break;
    case 'B': drive(-s, -s); break;
    case 'L': drive(-s,  s); break;
    case 'R': drive( s, -s); break;
    default:  drive( 0,  0);
  }
  lastCmd = millis();
  server.send(200, "text/plain", "ok");
}

const char PAGE[] PROGMEM = R"HTML(
<!doctype html><meta name=viewport content="width=device-width,user-scalable=no">
<style>body{font-family:sans-serif;text-align:center;user-select:none}
button{width:90px;height:90px;font-size:2em;margin:6px;border-radius:16px}</style>
<h3>XBOT Car</h3>
<div><button data-c=F>&#9650;</button></div>
<div><button data-c=L>&#9664;</button><button data-c=S>&#9632;</button><button data-c=R>&#9654;</button></div>
<div><button data-c=B>&#9660;</button></div>
<p>Speed <input id=sp type=range min=80 max=255 value=180></p>
<script>
let cur='S';
document.querySelectorAll('button').forEach(b=>{
  b.onpointerdown=()=>cur=b.dataset.c; b.onpointerup=b.onpointerleave=()=>cur='S';});
setInterval(()=>fetch(`/cmd?c=${cur}&s=${sp.value}`).catch(()=>{}),150);
</script>)HTML";

void setup() {
  Serial.begin(115200);
  int outs[] = {AIN1, AIN2, BIN1, BIN2, STBY};
  for (int p : outs) pinMode(p, OUTPUT);
  ledcAttach(PWMA, 20000, 8);   // 20 kHz: quiet motors
  ledcAttach(PWMB, 20000, 8);
  drive(0, 0);
  digitalWrite(STBY, HIGH);

  WiFi.softAP(AP_SSID, AP_PASS);
  Serial.printf("Join %s, open http://%s\n", AP_SSID, WiFi.softAPIP().toString().c_str());
  server.on("/", [] { server.send_P(200, "text/html", PAGE); });
  server.on("/cmd", handleCmd);
  server.begin();
}

void loop() {
  server.handleClient();
  if (millis() - lastCmd > WATCHDOG_MS) drive(0, 0);   // connection-loss stop
}

Troubleshooting

Symptom Likely cause Check
Motors hum but do not turn Speed too low or weak batteries Raise the minimum speed; fresh batteries
Car turns when it should go straight Motors differ slightly Scale one side, e.g. drive(s, s*0.92)
Nothing moves, page works STBY low or VM unpowered Check GPIO 33 and battery switch
Phone drops XBOT-CAR Phone prefers mobile data Turn off mobile data while driving

Safety and limitations

Never power motors from a GPIO or the ESP32's 3V3 pin. Use AA batteries for this build (no bare LiPo packs). Keep the battery switch reachable and switch off before changing wiring. Drive indoors on the floor, away from stairs, pets and small children.

Checklist

Next project

Motion-Controlled Robot Car: replace the phone with a tilt controller.

Next project: Motion-Controlled Robot Car