Bird
0
0
Arduinoprogramming~20 mins

Reading I2C sensor data in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Sensor 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 sensor reading code?

Consider this Arduino code snippet that reads a byte from an I2C sensor at address 0x40. What will be printed on the Serial Monitor?

Arduino
Wire.begin();
Wire.beginTransmission(0x40);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(0x40, 1);
int data = Wire.read();
Serial.println(data);
ACompilation error due to missing setup() and loop()
BPrints the byte value read from the sensor (e.g., 42)
CPrints 0 because Wire.read() returns 0 if no data
DRuntime error because Wire.requestFrom() is missing parameters
Attempts:
2 left
💡 Hint

Wire.requestFrom() asks the sensor for bytes, and Wire.read() reads them. The sensor returns a byte, which is printed.

🧠 Conceptual
intermediate
1:30remaining
Which I2C function initiates communication with a sensor?

In Arduino I2C communication, which function starts talking to a sensor device?

AWire.endTransmission()
BWire.requestFrom(address, quantity)
CWire.read()
DWire.beginTransmission(address)
Attempts:
2 left
💡 Hint

Think about which function tells the sensor you want to send data first.

🔧 Debug
advanced
2:30remaining
Why does this I2C sensor reading code always print -1?

Look at this Arduino code snippet. It always prints -1. Why?

Arduino
Wire.begin();
Wire.requestFrom(0x50, 1);
int val = Wire.read();
Serial.println(val);
AWire.begin() must be called inside loop() to work
BWire.read() returns -1 when the sensor address is wrong
CThe sensor was not told which register to read before requesting data
DSerial.println() cannot print integers from Wire.read()
Attempts:
2 left
💡 Hint

Think about what the sensor expects before sending data.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this I2C code?

Find the option that fixes the syntax error in this Arduino I2C code snippet:

Wire.beginTransmission(0x68)
Wire.write(0x10);
Wire.endTransmission();
AAdd a semicolon after Wire.beginTransmission(0x68);
BReplace Wire.endTransmission() with Wire.endTransmission(1)
CAdd Wire.requestFrom(0x68, 1); after endTransmission()
DChange Wire.write(0x10) to Wire.write(0x10);
Attempts:
2 left
💡 Hint

Look carefully at missing punctuation in the code.

🚀 Application
expert
3:00remaining
How many bytes will be read by this I2C code snippet?

This Arduino code requests data from an I2C sensor. How many bytes will be read and printed?

Wire.beginTransmission(0x20);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(0x20, 3);
while(Wire.available()) {
  Serial.print(Wire.read());
  Serial.print(",");
}
A3 bytes
B4 bytes
C0 bytes
D1 byte
Attempts:
2 left
💡 Hint

Check the number requested in Wire.requestFrom() and how many bytes are printed.