Introduction
In this tutorial, you will learn how to scan I2C devices on an STM32 microcontroller and retrieve data from the MPU6050 accelerometer/gyroscope using the Hardware Abstraction Layer (HAL) library. This step-by-step guide will walk you through the prerequisites, required parts, and necessary steps to successfully communicate with the MPU6050 over I2C.
Prerequisites
- Basic understanding of STM32 microcontrollers
- STM32 development board (e.g., STM32F4, STM32F1)
- MPU6050 module
- ST-Link programmer or USB to UART converter
- STM32CubeIDE installed
- Wiring tools (jumper wires, breadboard)
Parts/Tools
- STM32 Development Board
- MPU6050 Module
- Jumper Wires
- ST-Link Programmer
- STM32CubeIDE
Steps
- Set up your STM32 project:
- Open STM32CubeIDE and create a new STM32 project.
- Select your microcontroller or development board.
- In the “Pinout & Configuration” tab, enable the I2C interface (e.g., I2C1).
- Configure the I2C settings (e.g., 100kHz or 400kHz).
- Generate the project code.
- Connect the MPU6050 to the STM32:
- Connect the VCC pin of the MPU6050 to 3.3V on the STM32.
- Connect the GND pin of the MPU6050 to GND on the STM32.
- Connect the SDA pin of the MPU6050 to the SDA pin of the STM32.
- Connect the SCL pin of the MPU6050 to the SCL pin of the STM32.
STM32 MPU6050 --------------------- 3.3V <--- VCC GND <--- GND SDA <--- SDA SCL <--- SCL
- Write the I2C scanning code:
- In your main.c file, include the necessary headers:
- Create a function to scan for I2C devices:
#include "stm32f4xx_hal.h" #include "i2c.h"
void I2C_Scan(void) { uint8_t address; for(address = 1; address < 127; address++) { if(HAL_I2C_IsDeviceReady(&hi2c1, address << 1, 2, 10) == HAL_OK) { // Device found printf("I2C device found at address 0x%02Xn", address); } } }
- Retrieve data from the MPU6050:
- Define the MPU6050 address and registers:
- Create a function to read accelerometer data:
#define MPU6050_ADDR 0x68 << 1 #define ACCEL_XOUT_H 0x3B
void MPU6050_Read_Accel(int16_t *ax, int16_t *ay, int16_t *az) { uint8_t data[6]; HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, ACCEL_XOUT_H, I2C_MEMADD_SIZE_8BIT, data, 6, HAL_MAX_DELAY); *ax = (data[0] << 8) | data[1]; *ay = (data[2] << 8) | data[3]; *az = (data[4] << 8) | data[5]; }
- Initialize and run your program:
- In your main loop, call the I2C_Scan and MPU6050_Read_Accel functions:
int main(void) { HAL_Init(); SystemClock_Config(); MX_I2C1_Init(); I2C_Scan(); int16_t ax, ay, az; while (1) { MPU6050_Read_Accel(&ax, &ay, &az); // Process data HAL_Delay(100); } }
Troubleshooting
- No devices found:
- Check your wiring connections.
- Ensure the I2C interface is correctly configured in STM32CubeIDE.
- Verify that the MPU6050 is powered correctly.
- Incorrect data readings:
- Ensure you’re reading from the correct registers.
- Make sure the correct I2C address is used.
- Check if the MPU6050 is properly initialized.
Conclusion
In this tutorial, you have successfully set up an STM32 project to scan for I2C devices and retrieve data from an MPU6050 accelerometer/gyroscope. With this foundation, you can now expand your project by processing the accelerometer data or integrating additional I2C devices. Happy coding!