Bird
Raised Fist0
Embedded Cprogramming~5 mins

I2C bus architecture (SDA, SCL) in Embedded C

Choose your learning style10 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.

Practice

(1/5)
1. Which two lines are used in the I2C bus architecture for communication?
easy
A. CLK for data and DAT for clock
B. TX for data and RX for clock
C. SDA for data and SCL for clock
D. MOSI for data and MISO for clock

Solution

  1. Step 1: Recall I2C bus lines

    I2C uses two specific lines: SDA and SCL.
  2. Step 2: Identify roles of lines

    SDA carries data, and SCL carries the clock signal.
  3. Final Answer:

    SDA for data and SCL for clock -> Option C
  4. Quick Check:

    I2C lines = SDA + SCL [OK]
Hint: Remember: SDA = data, SCL = clock [OK]
Common Mistakes:
  • Confusing I2C lines with SPI lines
  • Mixing data and clock line names
  • Assuming more than two lines are used
2. Which of the following correctly describes the state of the SDA and SCL lines when no device is pulling them low on an I2C bus?
easy
A. SDA is high, SCL is low
B. Both lines are low
C. SDA is low, SCL is high
D. Both lines are high

Solution

  1. Step 1: Understand line behavior on I2C bus

    Lines are open-drain and pulled high by resistors when released.
  2. Step 2: Determine line state when idle

    When no device pulls lines low, both SDA and SCL remain high.
  3. Final Answer:

    Both lines are high -> Option D
  4. Quick Check:

    Idle I2C lines = high [OK]
Hint: Lines float high unless pulled low [OK]
Common Mistakes:
  • Assuming lines default to low
  • Confusing line states during communication
  • Ignoring pull-up resistor effect
3. Consider this snippet controlling I2C lines in embedded C:
if (SDA == 1 && SCL == 1) {
    // Start condition detected
} else if (SDA == 0 && SCL == 1) {
    // Stop condition detected
}
What is the output behavior of this code snippet?
medium
A. Detects start when SDA goes low while SCL is high
B. Detects stop when SDA goes high while SCL is low
C. Detects start when SDA and SCL are both low
D. Detects stop when SDA and SCL are both high

Solution

  1. Step 1: Recall I2C start and stop conditions

    Start condition: SDA falls low while SCL is high. Stop condition: SDA rises high while SCL is high.
  2. Step 2: Analyze code conditions

    The code checks if SDA and SCL are both high for start, which is incorrect; start is when SDA falls low while SCL is high.
  3. Final Answer:

    Detects start when SDA goes low while SCL is high -> Option A
  4. Quick Check:

    Start = SDA low + SCL high [OK]
Hint: Start = SDA falls low while SCL high [OK]
Common Mistakes:
  • Mixing start and stop conditions
  • Checking both lines high for start
  • Ignoring SDA line change direction
4. This embedded C code snippet is intended to detect an I2C stop condition:
if (SDA == 1 && SCL == 0) {
    // Stop condition detected
}
What is the error in this code?
medium
A. Stop condition requires SCL to be high, not low
B. SDA should be low, not high for stop condition
C. Both SDA and SCL should be low for stop condition
D. No error; code correctly detects stop condition

Solution

  1. Step 1: Recall stop condition timing

    Stop condition occurs when SDA rises from low to high while SCL is high.
  2. Step 2: Check code condition

    The code checks SDA high and SCL low, which is incorrect; SCL must be high for stop.
  3. Final Answer:

    Stop condition requires SCL to be high, not low -> Option A
  4. Quick Check:

    Stop = SDA high + SCL high [OK]
Hint: Stop needs SCL high, not low [OK]
Common Mistakes:
  • Confusing clock line state for stop
  • Assuming SDA low means stop
  • Ignoring line state transitions
5. You want to implement a function in embedded C that waits for the I2C start condition before proceeding. Which logic correctly detects the start condition on SDA and SCL lines?
hard
A. Wait until SDA changes from low to high while SCL is low
B. Wait until SDA changes from high to low while SCL is high
C. Wait until both SDA and SCL are low simultaneously
D. Wait until SCL changes from low to high while SDA is low

Solution

  1. Step 1: Understand start condition in I2C

    Start condition is when SDA line falls from high to low while SCL line is high.
  2. Step 2: Match logic to condition

    Wait until SDA changes from high to low while SCL is high correctly waits for SDA falling edge with SCL high, matching start condition.
  3. Final Answer:

    Wait until SDA changes from high to low while SCL is high -> Option B
  4. Quick Check:

    Start = SDA high-to-low + SCL high [OK]
Hint: Start = SDA falls low while SCL high [OK]
Common Mistakes:
  • Waiting for SDA rising edge instead of falling
  • Checking SCL low instead of high
  • Confusing start with stop condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard