from machine import Pin, I2C
import ssd1306
import mpu6050
import time
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
mpu_pir_pin = Pin(14, Pin.IN)
mpu = mpu6050.MPU6050(i2c)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
mpu.wake()
gyro=""
accel=""
def mpu_pir(pin):
new_gyro = mpu.read_gyro_data()
new_accel = mpu.read_accel_data()
if new_gyro != gyro or new_accel != accel:
gyro = new_gyro
accel = new_accel
print("Gyro:" + str(gyro) + ",Accel:" + str(accel))
oled.fill(0)
oled.text("Gyro", 0, 2)
oled.text("------------", 0, 10)
oled.text("x:" + str(round(gyro['x'], 2)), 0, 15)
oled.text("y:" + str(round(gyro['y'], 2)), 0, 25)
oled.text("z:" + str(round(gyro['z'], 2)), 0, 35)
oled.text("Accel", 70, 2)
oled.text("x:" + str(round(accel['x'], 2)), 70, 15)
oled.text("y:" + str(round(accel['y'], 2)), 70, 25)
oled.text("z:" + str(round(accel['z'], 2)), 70, 35)
oled.show()
mpu_pir_pin.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=mpu_pir)
while True:
time.sleep(0.1)