Bird
Raised Fist0
Embedded Cprogramming~20 mins

Start and stop conditions in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
I2C Start and Stop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this I2C start condition code snippet?
Consider the following embedded C code that attempts to generate an I2C start condition. What will be the value of the variable status after execution?
Embedded C
volatile int SDA = 1; // Data line
volatile int SCL = 1; // Clock line
int status = 0;

// Generate start condition
SDA = 1;
SCL = 1;
if (SDA == 1 && SCL == 1) {
    SDA = 0; // Pull SDA low while SCL high
    if (SDA == 0 && SCL == 1) {
        status = 1; // Start condition successful
    } else {
        status = -1; // Failed
    }
} else {
    status = -2; // Bus not idle
}
Astatus = 1
Bstatus = -1
Cstatus = -2
Dstatus = 0
Attempts:
2 left
💡 Hint
Remember, the I2C start condition is when SDA goes low while SCL is high.
❓ Predict Output
intermediate
2:00remaining
What does this code print when generating an I2C stop condition?
This embedded C code tries to generate an I2C stop condition. What will be printed?
Embedded C
#include <stdio.h>

volatile int SDA = 0; // Data line
volatile int SCL = 1; // Clock line

int main() {
    // Generate stop condition
    SDA = 0;
    SCL = 1;
    SDA = 1; // Release SDA while SCL high

    if (SDA == 1 && SCL == 1) {
        printf("STOP_OK\n");
    } else {
        printf("STOP_FAIL\n");
    }
    return 0;
}
ACompilation error
BSTOP_FAIL
CNo output
DSTOP_OK
Attempts:
2 left
💡 Hint
The stop condition is when SDA goes from low to high while SCL is high.
🔧 Debug
advanced
2:30remaining
Why does this I2C start condition code fail to detect the start?
This code is intended to detect an I2C start condition but incorrectly sets start_detected to 1. What is the bug?
Embedded C
volatile int SDA = 1;
volatile int SCL = 1;
int start_detected = 0;

void detect_start() {
    if (SDA == 1 && SCL == 1) {
        SDA = 0; // Pull SDA low
    }
    if (SDA == 0 && SCL == 1) {
        start_detected = 1;
    } else {
        start_detected = 0;
    }
}

int main() {
    detect_start();
    return start_detected;
}
Astart_detected should be initialized to 1, not 0.
BThe code changes SDA before checking the condition, so the first if is always true and start_detected is never set correctly.
CSCL should be pulled low before SDA to detect start condition.
DThe function detect_start() never returns a value, causing undefined behavior.
Attempts:
2 left
💡 Hint
Think about the order of operations and when the condition is checked.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this I2C stop condition code?
Given the following code snippet, which option will cause a syntax error when compiling?
Embedded C
volatile int SDA = 0;
volatile int SCL = 1;

void generate_stop() {
    SDA = 0;
    SCL = 1;
    SDA = 1; // Release SDA while SCL high
}
Avoid generate_stop() { SDA = 0; SCL = 1 SDA = 1; }
Bvoid generate_stop() { SDA = 0; SCL = 1; SDA = 1; }
Cvoid generate_stop() { SDA = 0; SCL = 1; SDA = 1; return; }
D} ;1 = ADS ;1 = LCS ;0 = ADS { )(pots_etareneg diov
Attempts:
2 left
💡 Hint
Look carefully for missing semicolons.
🚀 Application
expert
3:00remaining
How many times does the start condition get detected in this signal sequence?
Given the following signal changes over time, how many start conditions are detected? A start condition is when SDA goes from high to low while SCL is high. Time steps (SDA, SCL): 1: (1,1) 2: (0,1) 3: (0,0) 4: (1,0) 5: (1,1) 6: (0,1) 7: (0,1) 8: (1,1) Assume the detection code checks transitions at each time step.
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Look for moments when SDA changes from 1 to 0 while SCL is 1.

Practice

(1/5)
1.

What does a start condition indicate in embedded C communication protocols?

easy
A. It signals the beginning of data transmission.
B. It signals the end of data transmission.
C. It resets the device.
D. It pauses the communication temporarily.

Solution

  1. Step 1: Understand start condition role

    The start condition marks when a device begins sending data on the communication line.
  2. Step 2: Differentiate from stop condition

    Unlike the stop condition which ends communication, the start condition initiates it.
  3. Final Answer:

    It signals the beginning of data transmission. -> Option A
  4. Quick Check:

    Start condition = beginning signal [OK]
Hint: Start means begin communication, stop means end it [OK]
Common Mistakes:
  • Confusing start with stop condition
  • Thinking start resets the device
  • Assuming start pauses communication
2.

Which of the following is the correct syntax to generate a stop condition in embedded C I2C communication?

void i2c_stop() {
    // Which line correctly generates stop condition?
    ?
}
easy
A. SDA = 1; SCL = 1;
B. SDA = 1; SCL = 0;
C. SDA = 0; SCL = 0;
D. SDA = 0; SCL = 1;

Solution

  1. Step 1: Recall stop condition signal

    Stop condition occurs when SDA line goes from low to high while SCL is high.
  2. Step 2: Match with code lines

    Setting SDA=1 and SCL=1 matches the stop condition signal.
  3. Final Answer:

    SDA = 1; SCL = 1; -> Option A
  4. Quick Check:

    Stop condition = SDA rising while SCL high [OK]
Hint: Stop condition: SDA rises while SCL is high [OK]
Common Mistakes:
  • Setting SDA low instead of high for stop
  • Setting SCL low during stop condition
  • Confusing start and stop line states
3.

What will be the output of this code snippet simulating I2C start and stop conditions?

#include <stdio.h>
int start = 0, stop = 0;

void i2c_start() {
    start = 1;
    stop = 0;
}

void i2c_stop() {
    stop = 1;
    start = 0;
}

int main() {
    i2c_start();
    i2c_stop();
    if (start == 0 && stop == 1) {
        printf("Communication ended\n");
    } else {
        printf("Communication ongoing\n");
    }
    return 0;
}
medium
A. No output
B. Communication ongoing
C. Communication ended
D. Compilation error

Solution

  1. Step 1: Trace function calls and variable changes

    Calling i2c_start() sets start=1, stop=0; then i2c_stop() sets stop=1, start=0.
  2. Step 2: Evaluate if condition in main

    The condition start==0 and stop==1 is true, so it prints "Communication ended".
  3. Final Answer:

    Communication ended -> Option C
  4. Quick Check:

    start=0 & stop=1 means communication ended [OK]
Hint: Last called condition sets flags; check their final values [OK]
Common Mistakes:
  • Assuming start remains 1 after stop call
  • Confusing variable assignments order
  • Expecting output before function calls
4.

Identify the error in this code snippet that tries to generate a start condition:

void i2c_start() {
    SDA = 1;
    SCL = 1;
    SDA = 0;
}
medium
A. SCL should be set to 0 after SDA goes low.
B. SCL should be low before SDA goes low to start communication.
C. SDA should go from low to high, not high to low.
D. SDA should go from high to low while SCL is high, so code is correct.

Solution

  1. Step 1: Recall start condition definition

    Start condition requires SDA to go from high to low while SCL is high.
  2. Step 2: Analyze code sequence

    The code first sets SDA=1, SCL=1 (establishing high-high state), then SDA=0 while SCL remains high, correctly generating the start condition.
  3. Final Answer:

    SDA should go from high to low while SCL is high, so code is correct. -> Option D
  4. Quick Check:

    Start condition = SDA high->low while SCL high [OK]
Hint: Start condition: SDA falls while SCL high [OK]
Common Mistakes:
  • Confusing start (high->low) with stop (low->high)
  • Thinking SCL must be low before SDA transition
  • Ignoring that the code correctly times the transition
5.

You want to design a function that sends multiple data bytes over I2C with proper start and stop conditions. Which sequence correctly ensures communication integrity?

void send_data() {
    // Which sequence is correct?
    ?
}
hard
A. i2c_start(); send_byte(0x50); i2c_stop(); send_byte(0xA0);
B. i2c_start(); send_byte(0x50); send_byte(0xA0); i2c_stop();
C. send_byte(0x50); i2c_start(); send_byte(0xA0); i2c_stop();
D. i2c_stop(); send_byte(0x50); send_byte(0xA0); i2c_start();

Solution

  1. Step 1: Understand I2C communication flow

    Communication must start with a start condition, then data bytes are sent, and finally a stop condition ends it.
  2. Step 2: Evaluate each option sequence

    i2c_start(); send_byte(0x50); send_byte(0xA0); i2c_stop(); correctly starts communication, sends bytes, then stops. Others start or stop in wrong order.
  3. Final Answer:

    i2c_start(); send_byte(0x50); send_byte(0xA0); i2c_stop(); -> Option B
  4. Quick Check:

    Start -> data -> stop sequence is correct [OK]
Hint: Always start before sending data and stop after all bytes [OK]
Common Mistakes:
  • Sending data before start condition
  • Calling stop before all data sent
  • Mixing order of start and stop calls
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