
Get full C++ & Python code + lifetime access
โ๏ธ Tilt Pilot โ Motion-Controlled 3D Flight Game Fly a plane through a 3D city by tilting a breadboard. An ESP32S reads an MPU6050 motion sensor and streams its tilt to a browser game over USB. No joystick and no extra apps: just a microcontroller, one sensor and a web page.
Wiring diagram
๐ Overview Microcontroller ESP32S (NodeMCU-32S, 38-pin) Sensor MPU6050 6-axis gyro + accelerometer (GY-521 module) Link to PC USB serial at 115200 baud, read with the Web Serial API Game A single HTML file: 3D city built with three.js, runs in Chrome or Edge Update rate 100 tilt readings per second Cost About $3โ5 in parts How it works in one line: MPU6050 โ (IยฒC) โ ESP32S โ (USB serial) โ Chrome (Web Serial) โ 3D game
๐งฐ Components Qty Part Notes 1 ESP32S NodeMCU-32S Any 38-pin ESP32 dev board with the same pinout works 1 MPU6050 module (GY-521) 3.3 Vโ5 V tolerant breakout 1 Breadboard A half-size board fits in your hand 4 Jumper wires Red, black, yellow and blue keep it easy to follow 1 Micro-USB data cable Must be a data cable, not charge-only ๐ Wiring MPU6050 pin ESP32S pin Wire colour Purpose VCC 3V3 ๐ด Red Power (3.3 V) GND GND โซ Black Ground SCL GPIO 22 ๐ก Yellow IยฒC clock SDA GPIO 21 ๐ต Blue IยฒC data XDA, XCL, AD0, INT โ โ Leave unconnected text ESP32S NodeMCU-32S MPU6050 (GY-521) โโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ 3V3 โโโโโโโโโโโโโโโผโโโโ red โโโโโโโโบโโ VCC โ โ โ โ โ โ GND โโโผโโโโ black โโโโโโบโโ GND โ โ GPIO 22 โโโผโโโโ yellow โโโโโบโโ SCL โ โ GPIO 21 โโโผโโโโ blue โโโโโโโบโโ SDA โ โ โ โโ XDA (n/c) โ โ [EN] [BOOT] โ โโ XCL (n/c) โ โ โโ BOOT = start / re-centre โโ AD0 (n/c) โ โโโโโโโโโโฌโโโโโโโโโโโ โโ INT (n/c) โ โ micro-USB โ PC โโโโโโโโโโโโโโโโ Tip: Power the MPU6050 from 3V3, not 5V, so the IยฒC lines stay at 3.3 V logic for the ESP32. The on-board BOOT button (GPIO 0) is used as the game button, so no extra button is needed.
๐ ๏ธ Making Process
// 2. Calibrate: average ~400 gyro readings while the board is still gyroBiasX = sumX / samples;
// 3. Every 10 ms: read accel + gyro in one 14-byte burst readMotion(ax, ay, az, gx, gy, gz);
// 4. Complementary filter: fast gyro + drift-free accelerometer roll = 0.98 * (roll + gyroX * dt) + 0.02 * accRoll; pitch = 0.98 * (pitch + gyroY * dt) + 0.02 * accPitch;
// 5. Send to the PC Serial.printf("T,%.2f,%.2f\n", roll, pitch); Why a complementary filter?
The gyroscope is smooth and fast, but slowly drifts over time. The accelerometer always knows where "down" is, but it is noisy. Mixing 98% gyro with 2% accelerometer gives a reading that is fast, smooth and doesn't drift. Serial protocol
Message Meaning T,<roll>,<pitch> Tilt angles in degrees, 100ร per second BTN BOOT button pressed READY Sensor found and calibrated ERR,<message> Sensor missing; the firmware retries every second The firmware also detects a loose wire. If the sensor stops answering, it reports ERR and reconnects automatically once the wire is fixed.
Game (tilt_pilot.html) js // Read lines from the ESP32 with the Web Serial API const port = await navigator.serial.requestPort(); await port.open({ baudRate: 115200 });
// "T,12.40,-3.10" -> roll / pitch input between -1 and +1 input.roll = clamp((roll - zeroRoll) / 35, -1, 1); input.pitch = clamp((pitch - zeroPitch) / 35, -1, 1);
// Flight model: bank turns the plane, pitch climbs or dives yaw -= bank * TURN_RATE * dt; pos += forward * speed * dt; City: generated endlessly around the plane. Buildings, roads and windows are drawn with custom shaders for good performance. Collisions: checked against the same building data used for drawing, so what you see is what you hit. Re-centring: whatever pose you hold when you press BOOT or C becomes "level". Settings are saved in the browser, so you only set them once. ๐๏ธ Settings (in-game) Setting Default What it does Swap roll and pitch On For an MPU6050 mounted 90ยฐ rotated on the breadboard Invert left / right Off Flip the bank direction Invert up / down Off Flip climb and dive Tilt for full deflection 35ยฐ Lower = more sensitive Flight speed 120% 80โ180% Up / down strength 150% How steep and fast the plane climbs or dives The live dot in Settings shows your tilt. It should move right when you tilt right, and up when you raise the nose. If it doesn't, use the swap and invert switches.
๐งฏ Troubleshooting Problem Fix ERR,MPU6050 not found Check VCC โ 3V3, GND โ GND, SDA โ 21, SCL โ 22. Push the jumpers in firmly. No port in the Arduino IDE Use a data USB cable and install the CP210x driver. "Could not open the port" in the game Close the Arduino Serial Monitor or any other app using the port. Connect button does nothing Use Chrome or Edge on a computer. Safari and Firefox don't support Web Serial. Plane slowly turns on its own Hold the board level and press BOOT or C to re-centre. Directions are swapped or reversed Use Swap and Invert in Settings. Upload fails with "Failed to connect" Hold BOOT while the IDE says Connectingโฆ, then release. Game is black or blank The page needs internet the first time it opens, to load three.js. ๐ Project Structure text tilt-pilot/ โโโ tilt_pilot_controller/ โ โโโ tilt_pilot_controller.ino # ESP32S firmware โโโ tilt_pilot.html # 3D flight game (open in Chrome / Edge) โโโ tilt_pilot_wiring.png # Wiring diagram โโโ tilt_pilot_wiring.svg # Wiring diagram (vector) โโโ README.md ๐ Ideas to Extend Wireless: send the tilt over Wi-Fi or Bluetooth, so the controller runs on a LiPo battery with no cable. Throttle: add a potentiometer on an analog pin to control speed. Rudder: use the gyro's Z axis to yaw with a twist of the wrist. Haptics: add a small vibration motor that buzzes on near-misses and crashes. 3D-printed shell: turn the breadboard into a proper handheld controller. Other games: the same T,roll,pitch stream can drive a racing game, a marble maze or a space shooter. ๐ง What You'll Learn Reading a sensor over IยฒC by writing directly to its registers Sensor fusion with a complementary filter Talking from a microcontroller to a web page with the Web Serial API Building a real-time 3D game in the browser with three.js