aboutsummaryrefslogtreecommitdiff
path: root/src/pico/test.py
blob: b267b09de0a5cf9d5555fb7732eb286b59c0fa06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import time
import board
import busio
import adafruit_mpu6050  # Import Adafruit's MPU6050 library

# Initialize the I2C bus ONCE
i2c = busio.I2C(board.GP1, board.GP0)  # Use GP1 (SCL) and GP0 (SDA)

# Initialize the MPU6050 sensor using the Adafruit library
mpu = adafruit_mpu6050.MPU6050(i2c)

# Optional: Adjust settings (Uncomment if needed)
# mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_500_DPS
# mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_4_G
# mpu.filter_bandwidth = adafruit_mpu6050.Bandwidth.BAND_94_HZ

print("MPU6050 Initialized! Starting sensor read loop...")

try:
    while True:
        # Read acceleration (m/s^2)
        accel_x, accel_y, accel_z = mpu.acceleration

        # Read gyro data (degrees/s)
        gyro_x, gyro_y, gyro_z = mpu.gyro

        # Read temperature (°C)
        temp = mpu.temperature

        # Print sensor data
        print(f"Accel: X={accel_x:.2f}, Y={accel_y:.2f}, Z={accel_z:.2f} m/s²")
        print(f"Gyro: X={gyro_x:.2f}, Y={gyro_y:.2f}, Z={gyro_z:.2f} °/s")
        print(f"Temp: {temp:.2f}°C")
        print("-" * 40)

        time.sleep(0.1)  # Adjust delay as needed
except KeyboardInterrupt:
    print("Exiting...")