Challenge - 5 Problems
I2C ACK/NACK Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
I2C Master Transmit: What happens after sending address?
Consider this embedded C code snippet for an I2C master transmitting a slave address. What is the expected behavior if the slave acknowledges (ACK) the address?
What happens after
i2c_start();
i2c_send_address(0x50, I2C_WRITE);
if (i2c_check_ack()) {
// Slave acknowledged
i2c_send_data(0xA5);
} else {
// Slave did not acknowledge
i2c_stop();
}What happens after
i2c_send_address if the slave sends an ACK?Embedded C
i2c_start(); i2c_send_address(0x50, I2C_WRITE); if (i2c_check_ack()) { i2c_send_data(0xA5); } else { i2c_stop(); }
Attempts:
2 left
π‘ Hint
Remember, an ACK means the slave is ready to receive data.
β Incorrect
When the slave acknowledges the address, the master proceeds to send data bytes. If the slave does not acknowledge, the master stops communication.
β Predict Output
intermediate2:00remaining
I2C Slave Receive: What does NACK indicate?
In an I2C slave device, after receiving a data byte from the master, the slave sends an ACK or NACK. What does it mean if the slave sends a NACK after a data byte?
Choose the correct interpretation.
Choose the correct interpretation.
Attempts:
2 left
π‘ Hint
NACK usually means stop or error in communication.
β Incorrect
A NACK from the slave after a data byte means it cannot accept more data or there was a problem, so the master should stop or handle the error.
π§ Debug
advanced3:00remaining
Why does this I2C master code hang after sending data?
Examine this I2C master code snippet. The code hangs after sending the first data byte. Identify the cause.
i2c_start();
i2c_send_address(0x68, I2C_WRITE);
if (!i2c_check_ack()) {
i2c_stop();
return;
}
i2c_send_data(0x12);
while (!i2c_check_ack()) {
// wait for ACK
}
i2c_send_data(0x34);
i2c_stop();Attempts:
2 left
π‘ Hint
Check the loop waiting for ACK after sending data.
β Incorrect
The code waits indefinitely for an ACK after sending the first data byte. If the slave does not acknowledge, the loop never ends, causing a hang.
π§ Conceptual
advanced2:30remaining
I2C NACK on last byte: What is the masterβs role?
In an I2C read operation, the master reads multiple bytes from the slave. According to the I2C protocol, the master must send an ACK after each byte except the last one, after which it sends a NACK.
Why does the master send a NACK after the last byte?
Why does the master send a NACK after the last byte?
Attempts:
2 left
π‘ Hint
Think about how the master tells the slave it is done reading.
β Incorrect
The master sends a NACK after the last byte to tell the slave it will not read more data, so the slave stops sending bytes.
β Predict Output
expert3:00remaining
What is the output of this I2C slave ACK/NACK simulation code?
This embedded C code simulates an I2C slave device sending ACK or NACK based on received data. What is the output?
#include <stdio.h>
int i2c_slave_receive_byte(int byte) {
if (byte == 0xFF) {
// Invalid data, send NACK
return 0; // NACK
} else {
// Valid data, send ACK
return 1; // ACK
}
}
int main() {
int data[] = {0x10, 0x20, 0xFF, 0x30};
for (int i = 0; i < 4; i++) {
int ack = i2c_slave_receive_byte(data[i]);
if (ack) {
printf("ACK ");
} else {
printf("NACK ");
}
}
return 0;
}Embedded C
#include <stdio.h> int i2c_slave_receive_byte(int byte) { if (byte == 0xFF) { return 0; // NACK } else { return 1; // ACK } } int main() { int data[] = {0x10, 0x20, 0xFF, 0x30}; for (int i = 0; i < 4; i++) { int ack = i2c_slave_receive_byte(data[i]); if (ack) { printf("ACK "); } else { printf("NACK "); } } return 0; }
Attempts:
2 left
π‘ Hint
Check which byte triggers NACK in the function.
β Incorrect
Only the byte 0xFF causes the function to return NACK (0). All other bytes cause ACK (1). The output matches the sequence of ACKs and one NACK.