Python script for Raspberry Pi 5 (Ubuntu 24.04) using the GY-521 (MPU6050) over I²C.
The script performs automatic calibration at startup and then continuously outputs:
- Accelerations (m/s²) in the three axes
- Integrated velocities (m/s)
in a two-line layout:
X(Forward/Backward): +0.45 m/s² | Y(Left/Right): +0.59 m/s² | Z(Up/Down): -10.76 m/s²
X(Forward/Backward): +0.12 m/s | Y(Left/Right): +0.05 m/s | Z(Up/Down): -0.01 m/s
- Requirements
- Wiring
- Prepare Ubuntu 24.04
- Clone Repository
- Installation
- Run & Usage
- How Calibration Works
- Drift Reduction Tips
- Troubleshooting
- FAQ
- Raspberry Pi 5 running Ubuntu 24.04
- GY-521 (MPU-6050 breakout board)
- Jumper cables (M–F)
- Python 3
GY-521 → Raspberry Pi 5 GPIO header:
| GY-521 Pin | Raspberry Pi 5 | Note |
|---|---|---|
| VCC | 5V | Board includes regulator & level shifters → 5V safe |
| GND | GND | Common ground |
| SDA | GPIO 2 (SDA1) | I²C data |
| SCL | GPIO 3 (SCL1) | I²C clock |
| AD0 | (floating/LOW) | Address 0x68; HIGH → 0x69 |
sudo apt update
sudo apt install -y raspi-config
sudo raspi-config- Go to Interface Options → I2C → Enable
- Reboot if prompted
sudo adduser $USER i2c
# log out/in or rebootcd ~
git clone https://github.com/NEX108/raspberrypi5-motion-sensor.git
cd raspberrypi5-motion-sensorUbuntu 24.04 protects system Python (PEP 668). Use APT packages (recommended) or a virtual environment.
sudo apt update
sudo apt install -y python3-smbus python3-smbus2 i2c-toolssudo apt install -y python3-venv i2c-tools
python3 -m venv .venv
source .venv/bin/activate
pip install smbus2- Check I²C device:
i2cdetect -y 1Expected: 0x68 (or 0x69 if AD0 is HIGH).
- Run script (auto-calibration + measurement):
python3 accel_velocity_autocal.py- Place the sensor/car still for ~2 seconds at startup (for calibration & gravity detection).
- Then it will continuously output two lines per cycle:
- Line 1: Accelerations (m/s²)
- Line 2: Velocities (m/s)
Files created:
accel_velocity_autocal.py– main scriptmpu6050_offsets.json– calibration file
Stop with Ctrl+C.
- Collects ~250 samples while still.
- Offsets chosen so that X≈0 g, Y≈0 g, Z≈±1 g.
- Detects gravity direction (±Z) and subtracts g accordingly.
- Saves offsets to
mpu6050_offsets.json.
- Pure accelerometer integration → drift is inevitable.
- Mitigation:
- Add deadbands (ignore tiny noise).
- Use zero-velocity updates (reset velocity when clearly at rest).
- Better results:
- Use IMU fusion (Madgwick/Mahony) for orientation + gravity compensation.
- Combine with encoders or GPS.
“Remote I/O error (121)”
- Check wiring (VCC, GND, SDA=GPIO2, SCL=GPIO3).
- Verify with
i2cdetect -y 1. - Reload I²C driver:
sudo modprobe -r i2c_bcm2835 sudo modprobe i2c_bcm2835
- Power-cycle sensor.
i2cdetect shows nothing
- Ensure I²C is enabled in
raspi-config. - Check soldering/wires.
Wrong address
- AD0=HIGH → 0x69. Script automatically checks 0x68/0x69.
Velocities drift away
- Normal for raw integration. Use resets or fusion.
Units printed?
- Line 1: Accelerations (m/s²)
- Line 2: Velocities (m/s)
VCC 5V or 3.3V?
- Use 5V (GY-521 includes regulator & level shifting).
Scaling factor?
- ±2 g → 16,384 LSB/g, 1 g ≈ 9.81 m/s².
Recalibration?
- Delete
mpu6050_offsets.jsonand restart script.