10-20-2025, 08:24 PM
Make sure you're using a reliable library like smbus2 or Adafruit_CircuitPython_MPU6050.
Here's a simple example using smbus2:
Here's a simple example using smbus2:
Code:
import smbus2
import time
bus = smbus2.SMBus(1)
address = 0x68
# Wake up MPU6050
bus.write_byte_data(address, 0x6B, 0)
def read_word(adr):
high = bus.read_byte_data(address, adr)
low = bus.read_byte_data(address, adr+1)
val = (high << 8) + low
return val
def read_word_2c(adr):
val = read_word(adr)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val
while True:
accel_x = read_word_2c(0x3B)
print("Accel X: ", accel_x)
time.sleep(0.5)
