Read raw acceleration and rotation (gyroscope) data from an MPU6050 6-axis motion sensor over I2C serial connection.
I2C (Inter-Integrated Circuit) is a 2-wire serial bus — SDA (data) and SCL (clock) — that lets a microcontroller talk to one or many peripheral chips over just those two shared lines. Each device on the bus has its own address, so the controller can address them individually.
The MPU6050 combines a 3-axis gyroscope and a 3-axis accelerometer (plus an internal temperature sensor) in one package, communicating over I2C at the default address 0x68. It’s the kind of sensor used to keep drones, self-balancing robots, and phones aware of their orientation and movement. See reference/Class_mpu6050.md for details about the driver module for this sensor.

| Pin | Description |
|---|---|
| VCC | Power, 5V |
| GND | Ground |
| SCL | I2C clock |
| SDA | I2C data |
| XDA | Auxiliary I2C data (for chaining other I2C devices through the MPU6050) |
| XCL | Auxiliary I2C clock |
| AD0 | I2C address select — LOW = 0x68, HIGH = 0x69 |
| INT | Interrupt output |
The MPU6050’s I2C signal is sensitive to connection quality — make sure jumper wires are seated firmly, or readings may fail intermittently.

The MPU6050 runs on 5V in this circuit, not 3.3V.
Disconnect all power before building the circuit. Reconnect once verified.

Connections:

File: 03_sensors/code/Acceleration.py
Module: 03_sensors/code/mpu6050.py
from mpu6050 import MPU6050
import time
mpu=MPU6050(14,13) #attach the IIC pin(sclpin,sdapin)
mpu.MPU_Init() #initialize the MPU6050
G = 9.8
time.sleep_ms(1000)#waiting for MPU6050 to work steadily
try:
while True:
accel=mpu.MPU_Get_Accelerometer()#gain the values of Acceleration
gyro=mpu.MPU_Get_Gyroscope() #gain the values of Gyroscope
print("\na/g:\t")
print(accel[0],"\t",accel[1],"\t",accel[2],"\t",
gyro[0],"\t",gyro[1],"\t",gyro[2])
print("a/g:\t")
print(accel[0]/16384,"g",accel[1]/16384,"g",accel[2]/16384,"g",
gyro[0]/131,"d/s",gyro[1]/131,"d/s",gyro[2]/131,"d/s")
time.sleep_ms(1000)
except:
pass
03_sensors/code/.mpu6050.py → Upload to / — wait for it to finish uploading to the ESP32-S3.Acceleration.py.mpu=MPU6050(14,13) #attach the IIC pin(sclpin,sdapin)
mpu.MPU_Init() #initialize the MPU6050
time.sleep_ms(1000)#waiting for MPU6050 to work steadily
Associates GPIO14 (SCL) and GPIO13 (SDA) with the MPU6050 over I2C, initializes its internal registers, then waits a second for readings to stabilize.
accel=mpu.MPU_Get_Accelerometer()#gain the values of Acceleration
gyro=mpu.MPU_Get_Gyroscope() #gain the values of Gyroscope
Both return a 3-element list — one raw value per axis (X, Y, Z).
print(accel[0]/16384,"g",accel[1]/16384,"g",accel[2]/16384,"g",
gyro[0]/131,"d/s",gyro[1]/131,"d/s",gyro[2]/131,"d/s")
The sensor outputs raw 16-bit numbers, not physical units. At the MPU6050’s default sensitivity settings, dividing the accelerometer’s raw value by 16384 converts it to g (multiples of gravity), and dividing the gyroscope’s raw value by 131 converts it to degrees/second. These divisors come from the sensor’s datasheet and depend on its configured sensitivity range.
0x68)16384 LSB/g and 131 LSB/(°/s) are specific to the MPU6050’s default range settings; changing the sensor’s configured range would change these divisorsSee Class mpu6050 for the full API reference.
Adapted from Python_Tutorial.pdf Project 26.1