How to use MPU6050 to generate interrupts and wake up STM32 in sleep mode?
Using the MPU6050 to generate an interrupt and wake up an STM32 microcontroller from sleep mode is a common technique in low-power motion detection applications. Here's a complete step-by-step guide:
Goal:
-
Configure MPU6050 to generate an interrupt on motion.
-
Connect the INT pin of MPU6050 to an EXTI (external interrupt) pin on the STM32.
-
Wake up the STM32 from sleep (or stop/standby) mode on motion detection.
Step 1: Hardware Connections
MPU6050 | STM32 (e.g., STM32F103) |
---|---|
VCC | 3.3V |
GND | GND |
SDA | I2C SDA (e.g., PB7) |
SCL | I2C SCL (e.g., PB6) |
INT | GPIO with EXTI (e.g., PA0) |
Step 2: MPU6050 Configuration (Motion Interrupt)
Use I2C to configure MPU6050:
Step 3: STM32 Configuration
1. Configure I2C Peripheral
Use STM32CubeMX or HAL to initialize I2C (for communication with MPU6050).
2. Configure EXTI on MPU6050 INT Pin
In stm32f1xx_it.c
:
Enable interrupt in main.c
:
Step 4: Enter Low-Power Mode on STM32
In your main loop:
STM32 wakes up automatically when the EXTI line is triggered by MPU6050 INT.
Step 5: Optional – Confirm INT Cleared
After waking up, you can read the INT_STATUS
register from the MPU6050 to confirm the source:
Notes
-
Use STOP mode instead of SLEEP for more power saving.
-
Don’t forget to reconfigure system clocks after STOP mode.
-
Use I2C pull-ups and shield the MPU from vibration if testing is unstable.
评论
发表评论