STM32 HAL ADC Tutorial: Read Sensor Data Easily
Learn STM32 HAL I2C setup and communication with CubeIDE. Step-by-step tutorial, code examples, and testing explained in simple words.

Many beginners face problems when working with STM32 I2C communication. Common issues include wrong wiring, clock speed mismatch, and unclear code. This often makes I2C harder than UART or SPI. The good news is that using the STM32 HAL I2C library in CubeIDE makes setup easier and more reliable.

In this tutorial, we will go through I2C basics, STM32 HAL I2C setup, code examples, and testing methods. By the end, you will know how to use STM32 microcontrollers for I2C communication with sensors or other devices.

What is I2C Communication?

I2C (Inter-Integrated Circuit) is a two-wire communication protocol.

Key features of I2C:

  • Uses two lines: SDA (data) and SCL (clock).

  • Supports multiple masters and slaves on the same bus.

  • Uses device addresses (7-bit or 10-bit) to communicate.

  • Common speeds: 100 kHz (Standard Mode), 400 kHz (Fast Mode), 1 MHz (Fast Mode Plus).

Why Use STM32 HAL I2C?

  • HAL library makes configuration easier.

  • Built-in functions for transmit, receive, and memory operations.

  • Handles clock and timing automatically.

  • Supports blocking, interrupt, and DMA modes.

STM32 I2C Pinout Example

For STM32F103C8 (Blue Pill):

  • I2C1_SCL = PB6

  • I2C1_SDA = PB7

For STM32F407 Discovery:

  • I2C1_SCL = PB8

  • I2C1_SDA = PB9

⚠️ Always check your chip’s datasheet or CubeMX pinout.

STM32 HAL I2C Setup in CubeIDE

Step 1: Create a Project

  • Open STM32CubeIDE.

  • Select your STM32 board or chip.

Step 2: Enable I2C Peripheral

  • In Pinout & Configuration, select I2C1.

  • CubeIDE will assign default pins for SDA and SCL.

Step 3: Configure I2C Parameters

  • Mode: I2C

  • Speed Mode: Standard (100 kHz) or Fast (400 kHz)

  • Addressing: 7-bit (default)

  • Enable internal pull-ups if needed

Step 4: Generate Code

  • Click Generate Code.

  • CubeIDE creates initialization functions in i2c.c.

STM32 HAL I2C Example 1: Write to a Slave

Example: Sending data to an I2C device at address 0x50.

 
#include "main.h" uint8_t data[] = {0x01, 0x02, 0x03}; // sample data HAL_I2C_Master_Transmit(&hi2c1, 0x50 << 1, data, sizeof(data), HAL_MAX_DELAY);

Explanation:

  • hi2c1 is the I2C handle.

  • Address shifted left by 1 (HAL requires 8-bit addressing).

  • Transmits 3 bytes to the slave.


STM32 HAL I2C Example 2: Read from a Slave

Example: Reading 2 bytes from a sensor at 0x50.

 
uint8_t buffer[2]; HAL_I2C_Master_Receive(&hi2c1, 0x50 << 1, buffer, 2, HAL_MAX_DELAY);

Explanation:

  • Reads 2 bytes from device 0x50.

  • Stores result in buffer.


STM32 HAL I2C Example 3: Memory Read (EEPROM)

Many devices like EEPROMs use register-based access.

 
uint8_t mem_address = 0x10; uint8_t buffer[4]; HAL_I2C_Mem_Read(&hi2c1, 0x50 << 1, mem_address, I2C_MEMADD_SIZE_8BIT, buffer, 4, HAL_MAX_DELAY);

Explanation:

  • Reads 4 bytes starting at memory address 0x10.

  • I2C_MEMADD_SIZE_8BIT means 8-bit register address.


STM32 HAL I2C Example 4: Using Interrupts

 
uint8_t buffer[5]; HAL_I2C_Master_Receive_IT(&hi2c1, 0x50 << 1, buffer, 5);

In callback:

 
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) { // Data received successfully }

This allows non-blocking I2C communication.


STM32 HAL I2C Example 5: DMA Mode

For high-speed transfers:

 
uint8_t buffer[10]; HAL_I2C_Master_Receive_DMA(&hi2c1, 0x50 << 1, buffer, 10);

Testing I2C in STM32

  1. Connect STM32 to an I2C device (e.g., EEPROM, MPU6050, or OLED).

  2. Check wiring: SDA ↔ SDA, SCL ↔ SCL, GND ↔ GND.

  3. Use pull-up resistors (4.7kΩ recommended).

  4. Open a terminal or debugger to check received data.


Common STM32 I2C Errors and Fixes

  • No ACK received: Wrong address or wiring issue.

  • Bus stuck: Missing pull-ups or wrong clock speed.

  • Garbled data: Clock speed mismatch.

  • HAL_BUSY error: Ongoing transmission not completed.


Applications of STM32 I2C

  • Reading sensors (temperature, accelerometer, gyroscope).

  • Communicating with EEPROMs.

  • Driving OLED and LCD displays.

  • Connecting multiple devices on a single bus.


Pros and Cons of STM32 HAL I2C

Pros

  • Simple and fast to configure.

  • Built-in support for memory devices.

  • Multiple communication modes (blocking, interrupt, DMA).

Cons

  • Requires pull-up resistors.

  • Limited range (short-distance communication only).

FAQ on STM32 HAL I2C

What is I2C in STM32?
I2C is a two-wire communication protocol used by STM32 for sensors and memory devices.

Which pins are used for I2C in STM32?
Usually PB6 (SCL) and PB7 (SDA), but depends on the chip.

What is the default I2C speed in STM32 HAL?
100 kHz (Standard Mode), but you can configure 400 kHz or 1 MHz.

Do I need pull-up resistors for STM32 I2C?
Yes, 4.7kΩ is recommended for SDA and SCL.

How do I read from a sensor using STM32 HAL I2C?
Use HAL_I2C_Mem_Read() with the device address and register address.

Can STM32 support multiple I2C devices?
Yes, each device must have a unique address.

Does STM32 HAL I2C support DMA?
Yes, for high-speed communication.

What happens if two devices have the same I2C address?
You cannot connect them on the same bus unless one supports address change.

Can I2C be used for long-distance communication?
No, I2C is designed for short distances (a few meters max).

Which STM32 boards support I2C?
Almost all STM32 boards support at least one I2C peripheral.

Conclusion

In this tutorial, we explained STM32 HAL I2C setup, communication basics, and code examples for transmit, receive, memory read, interrupt, and DMA modes. Using the HAL library makes it easier for beginners to configure and use I2C in their projects.

Start with a simple write and read example, then move on to sensors like MPU6050 or OLED displays. With I2C, you can connect multiple devices to STM32 using just two wires, making it one of the most useful communication protocols in embedded systems.

For more hands-on STM32 tutorials and examples, check ControllersTech and start building your own projects today.

disclaimer

What's your reaction?