Challenge - 5 Problems
I2C Wire 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 master request?
Consider this Arduino code snippet using the Wire library as an I2C master. What will be printed on the Serial Monitor?
Arduino
void setup() {
Wire.begin();
Serial.begin(9600);
Wire.requestFrom(8, 2); // Request 2 bytes from slave address 8
while (Wire.available()) {
int c = Wire.read();
Serial.print(c);
Serial.print(' ');
}
}
void loop() {}Attempts:
2 left
💡 Hint
Think about what happens if no slave device responds at address 8.
✗ Incorrect
The Wire.requestFrom() asks for 2 bytes from slave address 8. If no slave is connected or responding, the master may receive no data or garbage values, so the output depends on the slave device's response.
🧠 Conceptual
intermediate1:00remaining
What does Wire.begin() do in Arduino I2C?
In Arduino's Wire library, what is the main purpose of calling Wire.begin() in your sketch?
Attempts:
2 left
💡 Hint
Think about what must happen before you can communicate over I2C.
✗ Incorrect
Wire.begin() initializes the I2C hardware and sets the Arduino as a master device by default, preparing it to send or receive data.
🔧 Debug
advanced2:30remaining
Why does this I2C slave code not respond correctly?
This Arduino code is intended to act as an I2C slave device at address 9. Why does it fail to respond to master requests?
Arduino
void setup() {
Wire.begin(9);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {
// Nothing here
}
void requestEvent() {
Wire.write("Hi");
}Attempts:
2 left
💡 Hint
Check what Wire.write() accepts as input in slave mode.
✗ Incorrect
Wire.write() expects bytes or byte arrays. Passing a string literal like "Hi" sends the pointer address, not the characters. To send characters, use Wire.write((uint8_t*)"Hi", 2).
📝 Syntax
advanced1:30remaining
Which option correctly writes data to an I2C slave?
You want to send the byte 0x42 to a slave device at address 5 using the Wire library. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember the sequence to send data as a master.
✗ Incorrect
To send data as a master, you start transmission to the slave address, write bytes, then end transmission. requestFrom is for reading, Wire.send does not exist, and Wire.begin(5) sets device as slave.
🚀 Application
expert2:00remaining
How many bytes are received by this I2C slave?
An Arduino is set as an I2C slave at address 10. The master sends 3 bytes: 0x01, 0x02, 0x03. The slave code uses this handler:
void receiveEvent(int howMany) {
int count = 0;
while (Wire.available()) {
Wire.read();
count++;
}
Serial.println(count);
}
What will be printed on the Serial Monitor when the master sends data?
Attempts:
2 left
💡 Hint
How many bytes does Wire.available() report after receiving data?
✗ Incorrect
The receiveEvent handler is called with howMany = 3. Wire.available() returns the number of bytes received. The loop reads all bytes and counts them, so count will be 3.
