Bird
0
0
Arduinoprogramming~20 mins

Connecting multiple I2C devices in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when scanning I2C devices?

This Arduino code scans the I2C bus for connected devices and prints their addresses. What will it print if two devices with addresses 0x3C and 0x68 are connected?

Arduino
void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Scanning I2C devices...");
  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("Device found at 0x");
      if (address < 16) Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  Serial.println("Scan complete.");
}

void loop() {}
AScanning I2C devices...\nDevice found at 0x3C\nDevice found at 0x68\nScan complete.
BScanning I2C devices...\nDevice found at 0x3C\nScan complete.
CScanning I2C devices...\nDevice found at 0x68\nScan complete.
DScanning I2C devices...\nScan complete.
Attempts:
2 left
💡 Hint

Think about how the code checks all addresses from 1 to 126 and prints each device found.

🧠 Conceptual
intermediate
1:30remaining
Why do multiple I2C devices need unique addresses?

When connecting multiple I2C devices to the same bus, why must each device have a unique address?

ATo prevent data collisions and ensure the master communicates with the correct device.
BBecause devices with the same address will increase the bus speed automatically.
CTo allow devices to share power lines without interference.
DBecause the master can only communicate with one device at a time regardless of address.
Attempts:
2 left
💡 Hint

Think about how the master identifies devices on the bus.

🔧 Debug
advanced
2:30remaining
Why does this code fail to properly detect the second I2C device?

The code below is intended to scan for two I2C devices at addresses 0x20 and 0x21. However, it only properly detects the device at 0x20. What is the problem?

Arduino
void setup() {
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Scanning I2C devices...");
  Wire.beginTransmission(0x20);
  if (Wire.endTransmission() == 0) {
    Serial.println("Device found at 0x20");
  }
  Wire.beginTransmission(0x21);
  Wire.endTransmission();
  Serial.println("Device found at 0x21");
}

void loop() {}
AThe Wire.beginTransmission(0x21) call is missing, so the device at 0x21 is never checked.
BThe Serial.begin(9600) is called too late, causing output issues.
CThe code prints 'Device found at 0x21' regardless of device presence because it does not check the return value of endTransmission for 0x21.
DThe code should use Wire.requestFrom() instead of beginTransmission to detect devices.
Attempts:
2 left
💡 Hint

Look carefully at how the code checks for the device at 0x21.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly initializes two I2C devices with different addresses?

Choose the code snippet that correctly initializes two I2C devices with addresses 0x40 and 0x41 using the Wire library.

AWire.begin();\nWire.requestFrom(0x40, 1);\nWire.requestFrom(0x41, 1);
BWire.begin(0x40);\nWire.begin(0x41);
CWire.begin();\nWire.begin(0x40);\nWire.begin(0x41);
DWire.begin();\nWire.beginTransmission(0x40);\nWire.endTransmission();\nWire.beginTransmission(0x41);\nWire.endTransmission();
Attempts:
2 left
💡 Hint

Remember that Wire.begin() initializes the master and beginTransmission starts communication with a device.

🚀 Application
expert
3:00remaining
How to handle two I2C devices with the same fixed address?

You have two I2C sensors that both use the fixed address 0x50 and cannot be changed. How can you connect and use both sensors on the same Arduino I2C bus?

AChange the Arduino's I2C address to avoid conflicts with the devices.
BUse an I2C multiplexer to switch between the two devices, selecting one at a time.
CConnect both devices directly to the bus; the Arduino will automatically handle address conflicts.
DUse two separate Arduino boards, each connected to one sensor, and communicate between Arduinos.
Attempts:
2 left
💡 Hint

Think about hardware solutions to address conflicts on the I2C bus.