from machine import Pin, I2C, ADC
import utime
# --- Hardware Setup ---
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
MPU_ADDR = 0x68
# The "Manual Trigger" Button on GP16
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
# The "Physical Sensor" Piezo on GP26
piezo = ADC(26)
def init_mpu():
i2c.writeto_mem(MPU_ADDR, 0x6B, b'\x00') # Wake up
i2c.writeto_mem(MPU_ADDR, 0x1C, b'\x18') # Set to ±16g
def read_accel():
data = i2c.readfrom_mem(MPU_ADDR, 0x3B, 6)
def conv(high, low):
val = (high << 8) | low
return (val - 65536) / 2048.0 if val > 32767 else val / 2048.0
return conv(data[0], data[1]), conv(data[2], data[3]), conv(data[4], data[5])
init_mpu()
print("System Live. Use the Button or Sliders to test!")
while True:
gx, gy, gz = read_accel()
# Trigger if Button is pressed OR Piezo spikes OR Motion is high
if button.value() == 1:
print("--- MANUAL BUTTON TRIGGER ---")
print(f"Current Swing Speed: {abs(gx):.2f}g")
print("----------------------------")
utime.sleep_ms(300) # Prevents spamming
utime.sleep_ms(10)Loading
pi-pico-w
pi-pico-w