0
0
Embedded Cprogramming~5 mins

Why I2C is used in Embedded C

Choose your learning style9 modes available
Introduction

I2C is used to connect multiple devices using only two wires. It helps devices talk to each other easily and saves space.

When you want to connect sensors and microcontrollers with few wires.
When you need to add many devices but have limited pins on your microcontroller.
When devices need to share data quickly and simply.
When you want to reduce wiring complexity in small electronics.
When you want to control devices like displays or memory chips from one controller.
Syntax
Embedded C
I2C uses two lines:
- SDA (data line)
- SCL (clock line)
Devices have unique addresses.
Master sends clock and data.
Slaves respond when addressed.

I2C uses only two wires regardless of how many devices are connected.

Each device has a unique address to avoid confusion.

Examples
This shows how the master sends data to a slave device using I2C.
Embedded C
// Example: Master writes data to a slave device
start_condition();
send_address(slave_address, WRITE);
send_data(data_byte);
stop_condition();
This shows how the master reads data from a slave device using I2C.
Embedded C
// Example: Master reads data from a slave device
start_condition();
send_address(slave_address, READ);
read_data(&data_byte);
stop_condition();
Sample Program

This program simulates sending a character 'A' to a device with address 0x50 using I2C steps.

Embedded C
#include <stdio.h>

// Simple simulation of I2C communication
void i2c_start() {
    printf("Start condition sent\n");
}

void i2c_stop() {
    printf("Stop condition sent\n");
}

void i2c_send_address(int address, int read) {
    printf("Address 0x%X sent with %s\n", address, read ? "read" : "write");
}

void i2c_send_data(char data) {
    printf("Data '%c' sent\n", data);
}

int main() {
    int slave_address = 0x50; // Example slave address
    char data_to_send = 'A';

    i2c_start();
    i2c_send_address(slave_address, 0); // write
    i2c_send_data(data_to_send);
    i2c_stop();

    return 0;
}
OutputSuccess
Important Notes

I2C is slower than some other methods but very simple and uses few wires.

Devices must have unique addresses to avoid conflicts.

Summary

I2C uses two wires to connect many devices easily.

It saves pins and wiring space in electronics.

Devices communicate by sending addresses and data over shared lines.