STM32F411 I2C Communication for Micromouse - Basic Features , I2C Communication Sequence, STM32 HAL I2C Functions, I2C Devices, Common Problems , I2C vs SPI, I2C in Micromouse
STM32F411 I2C Communication for Micromouse - Basic Features , I2C Communication Sequence, STM32 HAL I2C Functions, I2C Devices, Common Problems , I2C vs SPI, I2C in Micromouse
I2C (Inter-Integrated Circuit) is one of the most widely used communication protocols in embedded systems. The STMicroelectronics STM32F411 microcontroller includes dedicated I2C hardware peripherals that allow communication with sensors, displays, EEPROMs, RTC modules, and many other external devices. I2C is popular because it uses only two wires and supports multiple devices on the same communication bus. Part placement is easy because it communicates with chips within 3 to 5 cm of the CPU using only two wires.
What Is I2C?
I2C is a synchronous serial communication protocol developed for short-distance communication between integrated circuits.
The protocol uses:
- SDA → Serial Data Line
- SCL → Serial Clock Line
These two lines allow multiple devices to communicate efficiently.
Typical I2C connection:
Basic Features of I2C
Two-Wire Communication
Only two signal lines are required:
- SDA for data
- SCL for clock
This reduces PCB complexity and saves GPIO pins.
Master-Slave Architecture
I2C operates using:
- Master device
- Slave devices
The master:
- Generates clock signals
- Starts communication
- Selects slave devices
The slave:
Responds when addressed
STM32F411 can operate as:
- I2C master
- I2C slave
Multi-Device Support
Multiple devices can share the same I2C bus.
Each device has a unique address.
Example:
- OLED display
- MPU6050 sensor
- EEPROM
- RTC module
All connected on the same SDA and SCL lines.
I2C Communication Lines
Transfers:
- Addresses
- Commands
- Data
Bidirectional communication line.
SCL — Serial Clock
Generated by the master device.
Synchronizes communication timing.
Pull-Up Resistors
I2C lines require pull-up resistors because the bus uses open-drain outputs.
Typical values:
- 4.7 kΩ
- 10 kΩ
Without pull-up resistors:
- Communication may fail
- Signals become unstable
Typical wiring:
I2C Communication Sequence
The I2C protocol follows a strict sequence.
Step 1 — Start Condition
The master creates a START condition.
This happens when:
- SDA goes LOW
- While SCL remains HIGH
This tells all devices:
Communication is starting
Step 2 — Slave Address Transmission
The master sends:
- 7-bit or 10-bit address
- Read/write bit
Example:
OLED display address = 0x3C
Step 3 — Acknowledge Bit
The slave responds with ACK.
ACK means:
- Device detected
- Ready to communicate
Step 4 — Data Transfer
Master and slave exchange data bytes.
Each byte contains:
8 bits
After every byte:
Receiver sends ACK
Step 5 — Stop Condition
Master creates STOP condition.
This happens when:
- SDA goes HIGH
- While SCL remains HIGH
Communication ends.
I2C Timing Diagram
Basic I2C timing:
I2C Speeds
STM32F411 supports several I2C speed modes.
| Mode | Speed |
|---|---|
| Standard Mode | 100 kHz |
| Fast Mode | 400 kHz |
| Fast Mode Plus | 1 MHz |
Most sensors use:
- 100 kHz
- 400 kHz
GPIO Configuration for I2C
I2C pins require:
- Alternate function mode
- Open-drain output
- Pull-up resistors
High-speed mode
Example pins:
- PB6 → SCL
- PB7 → SDA
STM32 HAL I2C Functions
The STM32 HAL library simplifies I2C communication.
Common functions:
| Function | Purpose |
|---|---|
| HAL_I2C_Master_Transmit() | Send data |
| HAL_I2C_Master_Receive() | Receive data |
| HAL_I2C_Mem_Read() | Read memory device |
| HAL_I2C_Mem_Write() | Write memory device |
Example: Sending Data to OLED Display
Basic transmit example:
uint8_t data = 0xAA;
HAL_I2C_Master_Transmit(&hi2c1, 0x3C << 1, &data, 1, 100);
Explanation:
- hi2c1 → I2C peripheral
- 0x3C → slave address
- &data → transmit buffer
- 1 → data length
- 100 → timeout
static void platform_i2c_send_buffer(const uint8_t *data, uint16_t len)
{
// ... Send len bytes to i2c communication channel here
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Master_Transmit(&hi2c1, (s_i2c_addr << 1), (uint8_t *) data, len, 100);
//status = HAL_I2C_Master_Transmit_DMA(&hi2c1, (s_i2c_addr << 1), (uint8_t *) data, len); //, 100);
if(status != HAL_OK)
{
// Error handling, for example re-initialization of the I2C peripheral
i2c_err_ = status ;
print_i2c_error();
}
}
void ssd1306_platform_i2cInit(int8_t busId, uint8_t addr, ssd1306_platform_i2cConfig_t * cfg)
{
HAL_StatusTypeDef status = HAL_OK;
status= HAL_I2C_IsDeviceReady(&hi2c1, (s_i2c_addr << 1), 1, 20000);
if(status != HAL_OK)
{
// Error handling, for example re-initialization of the I2C peripheral
i2c_err_ = status ;
print_i2c_error();
}
ssd1306_intf.spi = 0;
ssd1306_intf.start = &platform_i2c_start;
ssd1306_intf.stop = &platform_i2c_stop;
//ssd1306_intf.send = &platform_i2c_send;
ssd1306_intf.close = &platform_i2c_close;
ssd1306_intf.send_buffer = &platform_i2c_send_buffer;
// init your interface here
//...
}
Reading Sensor Data
Example:
- MPU6050 accelerometer
- Temperature sensors
- EEPROM memory
Read sequence:
- Send register address
- Receive data bytes
STM32 HAL provides easy memory access functions.
Real coding as follow:
// Read an 8-bit register
uint8_t readReg(uint8_t reg) {
uint8_t value;
i2cStat = HAL_I2C_Mem_Read(&VL53L0X_I2C_Handler, g_i2cAddr | I2C_READ, reg, 1, msgBuffer, 1, I2C_TIMEOUT);
value = msgBuffer[0];
return value;
}
I2C Addressing
7-Bit Addressing
Most common method.
Supports: 128 addresses
10-Bit Addressing
Used for large systems with many devices.
Less common.
Multi-Master I2C
I2C allows multiple masters on the same bus.
Features:
- Arbitration
- Collision handling
However, most embedded systems use:
Single master configuration
DMA-Based I2C
DMA allows:
- Automatic data transfer
- Reduced CPU load
Useful for:
- Large displays
- Continuous sensor reading
- High-speed applications
Interrupt-Based I2C
Interrupt mode improves efficiency.
Advantages:
- Non-blocking communication
- Better multitasking
Common in:
- RTOS systems
- Robotics
- Real-time applications
Common I2C Devices
OLED Displays
Popular modules:
SSD1306 OLED
IMU Sensors
Examples:
- MPU6050
- MPU9250
Used in:
- Robotics
- Drones
- Motion tracking
EEPROM Memory
Stores:
- Calibration data
- Settings
- Logs
RTC Modules
Examples:
DS3231
Provides:
Real-time clock functions
Distance Sensor
Examples:
VL53L0X series
Provides:
Read the distance by mm using Lidar
statInfo_t_VL53L0X distanceStr;
//initVL53L0X(1, &hi2c2);
// Configure the sensor for high accuracy and speed in 20 cm.
setSignalRateLimit(200);
setVcselPulsePeriod(VcselPeriodPreRange, 10);
setVcselPulsePeriod(VcselPeriodFinalRange, 14);
setMeasurementTimingBudget(300 * 1000UL);
Common Problems in I2C
No ACK Response
Possible causes:
- Wrong address
- Missing pull-up resistors
- Wiring errors
Bus Busy Error
Occurs when:
SDA or SCL stuck LOW
Noise Problems
Long cables can introduce communication errors.
Bus Capacitance Limitations
Long wires may cause signal problems.
Shared Bus Conflicts
Address conflicts can occur.
I2C vs SPI
| Feature | I2C | SPI |
|---|---|---|
| Wires | 2 | 4+ |
| Speed | Moderate | High |
| Complexity | Simple | Moderate |
| Multi-device | Easy | More complex |
| Addressing | Yes | No |
I2C in Micromouse Robots
I2C is heavily used in Micromouse robots for:
- Gyroscope sensors
- OLED displays
- Distance sensors
- EEPROM storage
The protocol reduces wiring complexity and conserves GPIO pins.
Conclusion
I2C is one of the most important communication protocols in embedded systems and is fully supported by STM32F411 microcontrollers. With only two wires, I2C enables efficient communication between the STM32 and many external devices such as sensors, displays, memory chips, and RTC modules.
Understanding I2C communication is essential for robotics, IoT, automation, and professional embedded system development.
Comments
Post a Comment