Ads

Get STM32 tutorial using HAL at $10 for a limited time!

Thursday, March 17, 2016

ARM Cortex-M3 (STM32F103) Tutorial - I2C Master

In this tutorial, I will explain about I2C protocol and how to use it on STM32F103 microcontroller. I2C (Inter-Integrated Circuit) or sometimes called TWI (Two Wire Interface) is a synchronous serial protocol that only need 2 wire for communication. These wire are SCL for clock line and SDA for data line. Using I2C bus, you can connect devices like temperature sensor, EEPROM, RTC, etc (up to 112 device) just using two wire (plus GND wire). For accessing these device, I2C use 7-bit address. Theoretically 7-bit address space allows 128 addresses, however some addresses are reserved for special purpose. Thus, only 112 address can be used. There is also special method to use I2C with 10-bit address.


I2C is multi-master and multi-slave bus. I2C bus can consists of one or more master device, but only one master device can have an access to I2C bus each time. To communicate with a slave device, master is responsible for sending clock to the slave device. Clock signal is always generated by master. I2C drivers are open-drain, means that they can pull the signal low, but can't drive it high. On each signal line (SCL and SDA) must have a pull-up resistor to restore the signal high when no device is asserting it low.