What is I2C?
I2C Arduino Basic Explanation – I2C (Inter-Integrated Circuit) is a communication protocol used to connect nearby electronic devices. This protocol is used to connect devices such as sensors, memory, and other devices that require fast and efficient data transfer. I2C is used in a wide variety of electronics projects, such as robotics, home automation systems, and IoT (Internet of Things) projects.
History of I2C
I2C (Inter-Integrated Circuit) is a communications protocol developed by Philips Semiconductors (now NXP Semiconductors) in 1982. Initially, I2C was developed for use in consumer audio and video systems, but later became used in a variety of other electronic applications.
I2C was originally introduced under the name “SMBus” (System Management Bus), which refers to its capability to be used in a management system. However, due to its growing popularity, I2C soon became a commonly used industry standard.
I2C address
Each I2C device has a unique address which is used to identify the device. I2C uses two types of addresses, namely 7-bit addresses and 10-bit addresses where:
- 7-bit address can handle 127 devices on one bus
This address in the range 0x00 to 0x7F - 10-bit addresses can handle 1028 devices on one bus
This address used in special cases, such as when the available 7-bit address is not sufficient for an existing device
I2C addresses are defined by the device manufacturer. Some popular devices have predefined addresses, such as:
- MPU-6050 Gyroscope and Accelerometer (Address: 0x68)
- BMP180 Barometer (Address: 0x76)
- BMP280 Barometer (Address: 0x77)
- PCA9685 Servo Motor Controller (Address: 0x40)
- DS3231 RTC (Address: 0x68)
- PCF8574 I/O Expander (Address: 0x20 – 0x27)
- 24Cxx EEPROM (Address: 0x50 – 0x57)
- SSD1306 OLED Display (Address: 0x3C – 0x3D)
- MCP23017 I/O Expander (Address: 0x20 – 0x27)
- TCA9548A I2C Multiplexer (Address: 0x70 – 0x77)
- AT24Cxx Serial EEPROM (Address: 0x50 – 0x57)
- PCA95XX I2C Switch (Address: 0x70 – 0x77)
- TMP102 Temperature Sensor (Address: 0x48 – 0x4F)
The addresses used by these devices are simply the default addresses used by certain devices. There are hundreds of devices that can be used in an I2C project and each manufacturer may assign different addresses to their devices.
You can find a list of addresses for a specific device by searching the device manufacturer’s documentation or by searching the internet.
Most devices available today can be configured with the address you want, so you can change the device address to suit your needs.
I2C Scanner
If you have an I2C device such as a sensor for example and don’t know the address of the device, you can use the Arduino I2CScanner program code to read the address of the device automatically.
Basic Concepts, How It Works?
I2C works by connecting devices that will communicate via two main cables, namely SDA and SCL. Each device connected to the I2C bus has a unique address that is used to identify that device.
When a device wants to send or receive data, it must tell the address of the device it wants to contact. After that, the device that wants to send data will send data to the device that you want to connect via the SDA bus. SCL is used to synchronize data transfers so that devices receiving data can know when the data received is the correct data.
Pull-Up Resistors
Additionally, I2C requires pull-up resistors to keep the SDA and SCL signal levels at a logic “1” level when no device is transmitting. Pull-up resistors are used to pull the SDA and SCL signal levels down to a logic “1” level when no device is transmitting a signal. Without the pull-up resistors, the SDA and SCL signals would fluctuate and be unstable, which would cause errors in communication.
Typically, pull-up resistors are specified with values of 4.7k – 10k ohms. However, in several experiments that I have done, there are two pull-up resistor values used, namely:
- 2K for 400 kbps speed
- 10K for 100 kbps speed
Why is that? A smaller value of the pull-up resistor, such as 2k Ohm, will provide greater signal gain, so that the slave device can receive signals better and more stably at higher speeds, such as 400 kbps. However, if the value of the pull-up resistor is too small, the received signal can become too strong and cause errors in communication.
Meanwhile, a larger pull-up resistor value, such as 10k Ohm, will provide a smaller signal gain, so that the slave device can receive signals better and more stably at lower speeds, such as 100 kbps. However, if the value of the pull-up resistor is too large, the received signal may become too weak and cause errors in communication.
The following is the basic program code to implement I2C communication with Arduino, both as a master and as a slave:
Basic Master Code
#include <Wire.h>
void setup() {
Wire.begin(); // Initiates I2C communication as master
}
void loop() {
Wire.beginTransmission(8); // Initiated transaction with address 8
Wire.write("Hello, slave!"); // Sending data "Hello, slave!" to slave
Wire.endTransmission(); // End the transaction
delay(1000);
}
Basic Slave Code
#include <Wire.h>
void setup() {
Wire.begin(8); // Initiates I2C communication as a slave with address 8
Wire.onReceive(receiveData); // Specifies the function to be called when receiving data
}
void loop() {
// Empty, because all activity is done in the receiveData function
}
void receiveData(int byteCount) {
while(Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
Serial.println();
}
Ability
I2C capabilities I2C has several capabilities that make it very popular among electronics project developers. Some of them are:
- Can connect up to 1028 devices in one bus depending on the type of address used.
- It has a collision detection feature, which allows the device to know if there is a conflict in data transfer.
- It has a “multi-master” feature, which allows multiple devices to be the controller of the bus at the same time.
- Has a fast data transfer speed, which is up to 3.4 Mbps.
- Can be used at relatively long distances, up to 100 meters using an extender cable.
Strengths and Weaknesses
- The advantage of I2C is that this protocol is very efficient in terms of cable and resource usage. I2C only requires two main cables for communication, namely SDA (Serial Data) and SCL (Serial Clock), thus making the circuit design simpler.
- I2C also has a fairly high data transfer rate, which is up to 3.4 Mbps.
- The drawback of I2C is that this protocol can only connect devices that are a relatively short distance away. If you want to connect devices that are far away, then you have to use an extender or converter cable.
Arduino Project
Here are example Arduino project using I2C:
- Arduino LCD 20×4 I2C Code Basic Tutorial
- DS3231 arduino RTC Tutorial Using Adafruit Library
- Knop OLED SSD1306 Arduino SimulIDE u8g2
- Tutorial MPU9250 Arduino Show Data to OLED U8g2 Library
- Arduino Digital Spirit Level Using MPU9250 & OLED
- Arduino Compass MPU9250 Magnetometer & OLED
- INA219 Arduino Basic Code, Library and Tutorial
- AMG8833 Arduino Processing Program Code Schematic
- AMG8833 ESP8266 NodeMCU Arduino Basic Code
Conclusion
In general, I2C is an efficient and easy-to-use communications protocol used to interconnect nearby electronic devices. Arduino has an I2C library that makes it easy to implement I2C communication in Arduino projects.
Source : Kini Saya Ngerti