IntermediateInteractive Control

Motion-Controlled Robot Car

Control your robot car with gestures or tilt movements.

Time
2–3 h
Estimated cost
~£18 add-on (≈ £70 from scratch)
Hardware
ESP32-DevKitC V4 (38-pin)

Example code — not yet validated

Try it in the browser

What you will build

A handheld tilt controller. Hold the drive button and tilt forward to go forward, back to reverse, left and right to steer; the further you tilt, the faster it goes. Let go of the button and the car stops. The phone controls from Project 08 keep working.

How it works

The controller is a second ESP32 with an MPU6050 motion sensor. It measures gravity on three axes and works out pitch (forward/back tilt) and roll (left/right tilt). Pitch becomes throttle, roll becomes steering, and these are mixed into a left and right wheel speed. The controller sends them 20 times a second over ESP-NOW, a direct ESP32-to-ESP32 radio link that needs no router or phone. The car uses the same drive() function and 500 ms watchdog as Project 08.

Parts

Part Qty Purpose Required
Completed Wi-Fi Robot Car (Project 08) 1 The car Yes
ESP32-DevKitC V4 1 Controller Yes
MPU6050 breakout (GY-521) 1 Tilt sensing Yes
Tactile button 1 Drive (dead-man) button Yes
Mini breadboard + USB power bank 1 Handheld build Yes

Build steps

  1. Find the car's MAC address: upload the car code below, open the Serial Monitor, copy the line Car MAC: xx:xx:....
  2. Controller wiring: MPU6050 VCC → 3V3, GND → GND, SDA → GPIO 21, SCL → GPIO 22. Button GPIO 27 → GND.
  3. Install the Adafruit MPU6050 library (with dependencies).
  4. Paste the car's MAC into CAR_MAC in the controller sketch and upload it.
  5. Hold the controller flat and press reset: it calibrates "level" for 1 second.
  6. Wheels-up test: hold the drive button and tilt. Check direction and steering.
  7. Checkpoint: release the button → car stops instantly. Switch the controller off → car stops within 0.5 s.

Try it in the browser

Interactive Preview. A virtual tilt controller (drag the dot, or use your phone's motion sensor where the browser allows) drives the same top-down virtual car as Project 08.

Code

Car: add ESP-NOW to the Project 08 sketch.

Download esp-now-receiver-example.h

// Add to Wi-Fi Robot Car sketch — XBOTNOW (core esp32 v3.x)
#include <esp_now.h>

struct DrivePacket { int16_t left; int16_t right; uint8_t seq; };

void onDrive(const esp_now_recv_info_t* info, const uint8_t* data, int len) {
  if (len != sizeof(DrivePacket)) return;
  DrivePacket p; memcpy(&p, data, sizeof(p));
  drive(constrain(p.left, -255, 255), constrain(p.right, -255, 255));
  lastCmd = millis();          // feeds the same 500 ms watchdog
}

// in setup(), replace WiFi.softAP(...) with:
//   WiFi.mode(WIFI_AP_STA);
//   WiFi.softAP(AP_SSID, AP_PASS, 1);          // fixed channel 1
//   Serial.printf("Car MAC: %s\n", WiFi.macAddress().c_str());
//   esp_now_init();
//   esp_now_register_recv_cb(onDrive);

Controller sketch.

Download motion-controller.ino

// Motion Controller — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x · Library: Adafruit MPU6050
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <Adafruit_MPU6050.h>

uint8_t CAR_MAC[] = {0x24, 0x6F, 0x28, 0x00, 0x00, 0x00};  // from car Serial Monitor
const int DRIVE_BTN = 27;
const float DEADZONE = 5;      // degrees
const float MAX_TILT = 35;     // degrees for full speed

struct DrivePacket { int16_t left; int16_t right; uint8_t seq; };
Adafruit_MPU6050 mpu;
float pitch0 = 0, roll0 = 0, pitchF = 0, rollF = 0;
uint8_t seq = 0;

void readTilt(float& pitch, float& roll) {
  sensors_event_t a, g, t; mpu.getEvent(&a, &g, &t);
  pitch = atan2(a.acceleration.x, sqrt(sq(a.acceleration.y) + sq(a.acceleration.z))) * 57.3;
  roll  = atan2(a.acceleration.y, a.acceleration.z) * 57.3;
}

float axis(float deg) {           // degrees -> -1..1 with deadzone
  if (fabs(deg) < DEADZONE) return 0;
  return constrain((deg - copysign(DEADZONE, deg)) / (MAX_TILT - DEADZONE), -1, 1);
}

void setup() {
  Serial.begin(115200);
  pinMode(DRIVE_BTN, INPUT_PULLUP);
  if (!mpu.begin()) { Serial.println("MPU6050 not found"); while (1) delay(10); }
  float p, r, sp = 0, sr = 0;
  for (int i = 0; i < 50; i++) { readTilt(p, r); sp += p; sr += r; delay(20); }
  pitch0 = sp / 50; roll0 = sr / 50;          // "level" = how you hold it now

  WiFi.mode(WIFI_STA);
  esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE);
  esp_now_init();
  esp_now_peer_info_t peer = {};
  memcpy(peer.peer_addr, CAR_MAC, 6);
  peer.channel = 1;
  esp_now_add_peer(&peer);
}

void loop() {
  float p, r; readTilt(p, r);
  pitchF = 0.8 * pitchF + 0.2 * (p - pitch0);   // smoothing
  rollF  = 0.8 * rollF  + 0.2 * (r - roll0);

  float throttle = -axis(pitchF);   // tilt forward = forward (flip sign if needed)
  float steer = axis(rollF);
  int left = 0, right = 0;
  if (digitalRead(DRIVE_BTN) == LOW) {
    left  = constrain((throttle + steer) * 255, -255, 255);
    right = constrain((throttle - steer) * 255, -255, 255);
  }
  DrivePacket pkt = {(int16_t)left, (int16_t)right, seq++};
  esp_now_send(CAR_MAC, (uint8_t*)&pkt, sizeof(pkt));
  delay(50);                        // 20 packets per second
}

Troubleshooting

Symptom Likely cause Check
Car never responds Wrong MAC or channel Re-copy the MAC; both sides on channel 1
Forward and back swapped Sensor mounted the other way Remove the minus in throttle = -axis(...)
Car creeps when level Calibration taken while tilted Hold flat and press reset
Stutters at range Radio range / body blocking Stay within ~10 m line of sight

Safety and limitations

All Project 08 safety notes apply. The drive button is a dead-man switch: never tape it down. Test wheels-up after every code change.

Checklist

Next project

Gesture Air Mouse: use the same tilt skills to control a computer.

Next project: Gesture Air Mouse