This project captures orientation data (roll, pitch, and yaw) from an MPU6050 Inertial Measurement Unit (IMU) connected to an ESP8266 (or other Arduino-compatible board). The data is sent via a serial connection to a Python script, which uses PyOpenGL and PyGame to render a real-time 3D visualization of the sensor's orientation.
Additionally, the Arduino code demonstrates how to control a servo motor based on the calculated roll angle.
(A sample GIF showing the 3D model rotating in sync with the physical IMU board)
- Real-time Sensor Fusion: Reads accelerometer and gyroscope data from the MPU6050.
- Complementary Filter: A simple and effective filter is used on the Arduino to combine sensor readings for stable angle calculations.
- 3D Visualization: A Python script uses PyOpenGL to draw a 3D cuboid that visually represents the IMU's current orientation.
- Serial Communication: Efficient request/response communication between the Arduino and the Python script.
- Interactive Controls: Toggle yaw tracking on the fly and quit the application gracefully.
- Servo Control: The Arduino sketch includes code to control a servo motor based on the roll angle, perfect for projects like self-balancing robots or camera gimbals.
-
Arduino/ESP8266:
- The microcontroller continuously reads raw data from the MPU6050 sensor.
- It applies a complementary filter to the raw values to calculate stable roll, pitch, and yaw angles.
- It listens for a
.
character over the serial port. Upon receiving it, it sends back the latest calculated angles as a comma-separated string (e.g.,-10.24, 5.12, 95.80
). - It also drives a connected servo motor to an angle corresponding to the current roll value.
-
Python Script:
- Initializes a PyGame window with an OpenGL context.
- In a loop, it sends a
.
character to the specified serial port to request data from the Arduino. - It reads and parses the incoming line of data to get the roll, pitch, and yaw angles.
- It uses these angles to apply rotations (
glRotatef
) to the 3D model, updating its orientation on the screen. - It handles user input for quitting the application or toggling yaw mode.
- An Arduino-compatible board (the code is configured for an ESP8266 like a Wemos D1 Mini or NodeMCU).
- MPU6050 Gyroscope/Accelerometer Module.
- A standard servo motor (optional).
- Jumper wires.
- MPU6050 VCC -> 3.3V
- MPU6050 GND -> GND
- MPU6050 SCL -> D1 (GPIO5)
- MPU6050 SDA -> D2 (GPIO4)
- Servo Signal -> D4 (GPIO2)
- Servo VCC -> 5V / VIN
- Servo GND -> GND
- Arduino IDE
- Board support for your microcontroller (e.g., ESP8266 Core).
Wire.h
andServo.h
libraries (typically included with the board core).
- Python 3.x
- PyOpenGL
- PyGame
- PySerial
You can install the required Python packages using pip:
pip install PyOpenGL PyGame pyserial
- Hardware: Connect the components as described in the wiring diagram above.
- Arduino:
- Open the
.ino
sketch in the Arduino IDE. - Select your board (e.g., "LOLIN(WEMOS) D1 R2 & mini") and the correct COM port from the
Tools
menu. - Upload the sketch to your board.
- Open the
- Python:
- Open the
main.py
file. - Crucially, find this line and change
'COM3'
to the COM port your Arduino is using:ser = serial.Serial('COM3', 115200)
- Open the
- Run:
- Execute the Python script from your terminal:
python main.py
- A window should appear showing the 3D model. Tilt your MPU6050 sensor to see it move!
- Execute the Python script from your terminal:
z
key: Toggles yaw tracking on or off. When toggled on, it also resets the current yaw angle to zero.Esc
key: Closes the application.