0
0
Embedded Cprogramming~5 mins

I2C bus architecture (SDA, SCL) in Embedded C

Choose your learning style9 modes available
Introduction

I2C bus uses two wires to let devices talk to each other simply and clearly.

Connecting sensors to a microcontroller with few wires
Communicating between multiple chips on a small circuit board
Reading data from memory chips like EEPROM
Controlling small displays or input devices
Sharing information between a master device and many slaves
Syntax
Embedded C
SDA - Serial Data Line (bi-directional data)
SCL - Serial Clock Line (clock signal from master)

SDA carries the data bits one by one.

SCL controls when data bits are sent or read.

Examples
This sets up the SDA and SCL pins for I2C communication.
Embedded C
// Example: Initialize I2C pins
#define SDA_PIN 21
#define SCL_PIN 22

void i2c_init() {
    // Configure SDA and SCL pins as open-drain with pull-up
    pinMode(SDA_PIN, INPUT_PULLUP);
    pinMode(SCL_PIN, INPUT_PULLUP);
}
Start condition signals the beginning of communication.
Embedded C
// Example: Start condition on I2C bus
void i2c_start() {
    // SDA goes low while SCL is high
    digitalWrite(SDA_PIN, LOW);
    digitalWrite(SCL_PIN, HIGH);
}
Sample Program

This program shows how the SDA and SCL lines change to create a start condition on the I2C bus.

Embedded C
#include <stdio.h>

// Simulate I2C lines
int SDA = 1; // 1 means high, 0 means low
int SCL = 1;

void i2c_start() {
    SDA = 1;
    SCL = 1;
    printf("SDA=%d, SCL=%d - Bus idle\n", SDA, SCL);
    SDA = 0; // SDA goes low while SCL is high
    printf("SDA=%d, SCL=%d - Start condition\n", SDA, SCL);
    SCL = 0; // Pull clock low to prepare for data
    printf("SDA=%d, SCL=%d - Ready to send data\n", SDA, SCL);
}

int main() {
    i2c_start();
    return 0;
}
OutputSuccess
Important Notes

Both SDA and SCL lines use pull-up resistors to stay high when not driven low.

Devices on the bus can only pull lines low; they never drive them high directly.

Start and stop conditions are special signals to control communication flow.

Summary

I2C uses two wires: SDA for data and SCL for clock.

SDA is bi-directional; SCL is controlled by the master device.

Start condition means SDA goes low while SCL is high, signaling communication start.