Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

raspberrypi5-motion-sensor

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

Table of Contents


Requirements

  • Raspberry Pi 5 running Ubuntu 24.04
  • GY-521 (MPU-6050 breakout board)
  • Jumper cables (M–F)
  • Python 3

Wiring

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

Prepare Ubuntu 24.04

1) Enable I²C

sudo apt update
sudo apt install -y raspi-config
sudo raspi-config
  • Go to Interface Options → I2C → Enable
  • Reboot if prompted

2) Add user to I²C group

sudo adduser $USER i2c
# log out/in or reboot

Clone Repository

cd ~
git clone https://github.com/NEX108/raspberrypi5-motion-sensor.git
cd raspberrypi5-motion-sensor

Installation

Ubuntu 24.04 protects system Python (PEP 668). Use APT packages (recommended) or a virtual environment.

Option A — System-wide (recommended)

sudo apt update
sudo apt install -y python3-smbus python3-smbus2 i2c-tools

Option B — Virtual Environment

sudo apt install -y python3-venv i2c-tools
python3 -m venv .venv
source .venv/bin/activate
pip install smbus2

Run & Usage

  1. Check I²C device:
i2cdetect -y 1

Expected: 0x68 (or 0x69 if AD0 is HIGH).

  1. 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 script
  • mpu6050_offsets.json – calibration file

Stop with Ctrl+C.


How Calibration Works

  • 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.

Drift Reduction Tips

  • 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.

Troubleshooting

“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.

FAQ

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.json and restart script.

About

Python script for Raspberry Pi 5 (Ubuntu 24.04) using the GY-521 (MPU6050) over I²C, with auto-calibration to output accelerations (m/s²) and integrated velocities (m/s).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages