Tuesday , November 4 2025

#1 – How to use HAL library I2C functions?

When it comes to read data from a target device by using Stm32F4 Hal library I2C functions, how to do it? In this example, MPU6050 sensor is used as target device. The sensor ID value in the WHO_AM_I register(0x75) should be read as 104 (decimal). MPU6050 AD0 pin is for slave address. If AD0 pin is connected to LOW(GND), slave address is 0xD0. If it is connected to HIGH(3.3V), slave adres is 0xD2.

In the sample code below;
HAL_I2C_IsDeviceReady() function is for controlling I2C device connection.
HAL_I2C_Master_Transmit() function is for transmitting slave device address and register.
HAL_I2C_Master_Receive() function is for reading the data regarding transmitted register from slave device.
HAL_UART_Transmit() function is for transmitting the received value of WHO_AM_I ID to serial buffer.

The connections between both Stm32F4 and MPU6050(I2C1);

Stm32F4 MPU6050
PB6 (SCL) SCL
PB7 (SDA) SDA
3.3V VCC
GND GND
GND AD0

The sample code;

 

Check Also

#2 – How to Connect HC-06 Bluetooth Module?

Notes; The HC-06 Tx pin is able to work with 5V but Rx pin not …