Challenge - 5 Problems
I2C Start and Stop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 }
Attempts:
2 left
💡 Hint
Remember, the I2C start condition is when SDA goes low while SCL is high.
✗ Incorrect
The code first ensures both SDA and SCL are high (bus idle). Then it pulls SDA low while SCL remains high, which is the start condition. So status is set to 1.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
The stop condition is when SDA goes from low to high while SCL is high.
✗ Incorrect
The code sets SDA low, then SCL high, then SDA high while SCL is high. This matches the stop condition, so it prints STOP_OK.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Think about the order of operations and when the condition is checked.
✗ Incorrect
The code sets SDA to 0 inside the first if, then immediately checks if SDA is 0 and SCL is 1. Because SDA was just changed, the condition is always true, but the logic flow causes start_detected to be set incorrectly. The detection logic should check the condition before changing SDA.
📝 Syntax
advanced2: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 }
Attempts:
2 left
💡 Hint
Look carefully for missing semicolons.
✗ Incorrect
Option A is missing a semicolon between 'SCL = 1' and 'SDA = 1', causing a syntax error. The others are syntactically correct.
🚀 Application
expert3: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.
Attempts:
2 left
💡 Hint
Look for moments when SDA changes from 1 to 0 while SCL is 1.
✗ Incorrect
Start conditions occur at time steps 2 (SDA 1->0 while SCL=1) and 6 (SDA 1->0 while SCL=1). So total 2 start conditions.