Raspberry Pi I2C Communication Tutorial

Click the above “Mechanical and Electronic Engineering Technology” to follow us

1. What is I2C Communication

I2C (Inter-Integrated Circuit) is a serial communication protocol used for transferring data between integrated circuits (ICs). It was developed by Philips (now NXP Semiconductors) in the 1980s and has become a widely adopted communication standard in various electronic devices.
I2C communication relies on two transmission lines: SDA (Serial Data Line) and SCL (Serial Clock Line). The SDA line is used for data transmission, while the SCL line generates a clock signal to synchronize data transfer. This master-slave architecture allows multiple slave devices to communicate with a master device over the same pair of transmission lines.
In I2C communication, each device has a unique address for identification and communication with other devices. The master device initializes communication and controls the timing on the bus. During communication, the master device sends a start signal to initiate communication and specifies the address of the slave device to communicate with. Then, in each transmission cycle, the master device sends data to the slave or receives data from the slave, and sends a stop signal at the end of the transmission. The slave responds to the master based on the received commands or data.
I2C communication has several characteristics:
Two-Wire: Only two transmission lines are needed for full-duplex communication, making hardware connections simple and cost-effective.
Multiple Slaves: Multiple slave devices can communicate with the master device through shared transmission lines, with each slave being identified by a unique address.
Clock Synchronization: The master device generates a clock signal to synchronize data transmission, ensuring data stability.
Data Transfer Rates: I2C supports different transfer rates, with typical rates including 100 kHz, 400 kHz, and 3.4 MHz.

Raspberry Pi I2C Communication Tutorial

Raspberry Pi I2C Communication Tutorial

2. Raspberry Pi I2C Communication Interface

Many sensors currently support the I2C interface, such as MPU6050, PCF8591, and ADS1115. When using the Raspberry Pi I2C bus to control peripherals or sensors, the Raspberry Pi typically operates in master mode. The Raspberry Pi I2C bus adheres to the SMBus (System Management Bus) protocol. From a type perspective, the SMBus protocol can be viewed as a subclass of the I2C bus protocol, and the SMBus library/SMBus module can be used directly to access I2C devices in Python.

It is important to note that the Raspberry Pi uses pins 3 and 5 to communicate with external I2C devices. Pins 27 and 28 also have I2C communication capabilities, but they are not commonly used.

Raspberry Pi I2C Communication Tutorial

3. Steps for Raspberry Pi to Perform I2C Communication
For the Raspberry Pi, it has built-in hardware support for I2C communication. To use the Raspberry Pi for I2C communication, the following steps are required:
Hardware Connection: Ensure your target device is connected to the Raspberry Pi via the appropriate interface. Generally, I2C communication is connected through the GPIO pins SDA (data line) and SCL (clock line).
Enable I2C Interface: Enable the I2C interface by configuring the Raspberry Pi system. This can be accomplished through the following steps:
Open the terminal and enter the following command to edit the configuration file:
sudo raspi-config

    Use the up and down arrow keys to select “Interfacing Options,” then press Enter.

    Select “I2C,” then press Enter.

    Select “Yes” to enable the I2C interface, then press Enter.

    After completion, select “Finish” and reboot the Raspberry Pi.

    Install Necessary Packages: Enter the following command in the terminal to install the relevant packages:
    sudo apt-get install python3-smbus sudo apt-get install i2c-tools
    This will install the SMBus library for Python and I2C tools.
    Verify I2C Devices: Open the terminal and run the following command to detect connected I2C devices:
    sudo i2cdetect -y 1
    This will display a list of addresses for all connected I2C devices.
    Use Python Code for I2C Communication: Create a Python script using the SMBus library for I2C communication. Below is an example code snippet to send commands to the I2C device and receive responses:
    import smbus
    # I2C device address
    address = 0x68
    # Open I2C bus
    bus = smbus.SMBus(1)
    # Send command to device and wait for a while
    bus.write_byte(address, 0x01)
    time.sleep(0.1)
    # Read response data from device
    data = bus.read_byte(address)
    print(data)
    # Close I2C bus
    bus.close()
Raspberry Pi I2C Communication Tutorial

Want to learn more?

Quickly scan the code to follow us

Leave a Comment