Bird
0
0
Arduinoprogramming~20 mins

Wire library for I2C in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Wire 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 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() {}
A0 0
BThe code will not compile due to missing slave initialization
CThe code will hang waiting for data
DRandom bytes printed depending on slave device
Attempts:
2 left
💡 Hint
Think about what happens if no slave device responds at address 8.
🧠 Conceptual
intermediate
1: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?
AIt resets the Arduino board
BIt initializes the I2C bus and sets the device as a master by default
CIt configures the serial port for debugging
DIt sends data to the slave device immediately
Attempts:
2 left
💡 Hint
Think about what must happen before you can communicate over I2C.
🔧 Debug
advanced
2: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");
}
AWire.write() inside requestEvent must send bytes, not a string literal
BWire.write("Hi") sends 2 bytes but master expects 1 byte, causing mismatch
CThe requestEvent function must be declared as static
DWire.begin(9) must be called after Wire.onRequest()
Attempts:
2 left
💡 Hint
Check what Wire.write() accepts as input in slave mode.
📝 Syntax
advanced
1: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?
AWire.send(5, 0x42);
B
Wire.requestFrom(5, 1);
Wire.write(0x42);
C
Wire.beginTransmission(5);
Wire.write(0x42);
Wire.endTransmission();
D
Wire.begin(5);
Wire.write(0x42);
Attempts:
2 left
💡 Hint
Remember the sequence to send data as a master.
🚀 Application
expert
2: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?
A3
B0
CThe code will not compile due to missing parameter type
D1
Attempts:
2 left
💡 Hint
How many bytes does Wire.available() report after receiving data?