0
0
Power-electronicsConceptBeginner · 3 min read

What is I2C Address in Embedded C: Explanation and Example

An I2C address is a unique 7-bit or 10-bit number assigned to each device on the I2C bus to identify it during communication. In embedded C, this address is used to specify which device the microcontroller wants to talk to when sending or receiving data over the I2C protocol.
⚙️

How It Works

The I2C bus is like a shared conversation line where multiple devices talk to each other. Each device has a unique I2C address, similar to a house number on a street. When the microcontroller wants to send data to a device, it first calls out the device's address to get its attention.

Think of it like sending a letter: the address tells the post office exactly where to deliver it. In embedded C, this address is a number usually 7 bits long, which the microcontroller uses to select the right device before sending or receiving data. This ensures that only the device with the matching address responds.

💻

Example

This example shows how to define and use an I2C address in embedded C to communicate with a device. The address is set as a constant, and the code sends a simple command to that device.

c
#include <stdint.h>
#include <stdio.h>

#define I2C_DEVICE_ADDRESS 0x3C  // Example 7-bit I2C address

// Mock function to simulate I2C start condition
void i2c_start() {
    printf("I2C Start Condition\n");
}

// Mock function to simulate sending address
void i2c_send_address(uint8_t address) {
    printf("Sending I2C Address: 0x%X\n", address);
}

// Mock function to simulate sending data
void i2c_send_data(uint8_t data) {
    printf("Sending Data: 0x%X\n", data);
}

// Mock function to simulate I2C stop condition
void i2c_stop() {
    printf("I2C Stop Condition\n");
}

int main() {
    uint8_t command = 0xA0;  // Example command to send

    i2c_start();
    i2c_send_address((I2C_DEVICE_ADDRESS << 1) | 0); // Shift left for write mode
    i2c_send_data(command);
    i2c_stop();

    return 0;
}
Output
I2C Start Condition Sending I2C Address: 0x78 Sending Data: 0xA0 I2C Stop Condition
🎯

When to Use

You use an I2C address in embedded C whenever you want your microcontroller to communicate with a specific device on the I2C bus. This is common in projects involving sensors, displays, memory chips, or other peripherals that connect via I2C.

For example, if you have a temperature sensor and an LCD screen both connected to the same I2C bus, each will have its own unique address. Your code must use the correct address to read temperature data or send text to the display without confusion.

Key Points

  • I2C addresses uniquely identify devices on the I2C bus.
  • Addresses are usually 7 bits, sometimes 10 bits.
  • In embedded C, addresses are used to select devices before data transfer.
  • Shifting the address left by 1 bit is common to add the read/write bit.
  • Correct addressing prevents communication errors on the bus.

Key Takeaways

I2C address is a unique number used to identify devices on the I2C bus in embedded C.
You must use the correct I2C address to communicate with the intended device.
Addresses are typically 7 bits and shifted left to include the read/write bit.
Using I2C addresses prevents data mix-up between multiple devices on the same bus.
Embedded C code uses these addresses to send commands or read data from devices.