Bird
Raised Fist0
Arduinoprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main reason multiple I2C devices can share the same two wires on an Arduino?
easy
A. They use different clock speeds to avoid conflicts
B. Each device uses a separate data wire
C. Each device has a unique address to identify it on the bus
D. They communicate one at a time using different power pins

Solution

  1. Step 1: Understand I2C wiring basics

    I2C devices share two wires: SDA (data) and SCL (clock).
  2. Step 2: Identify how devices are distinguished

    Devices use unique addresses so the master knows which device to talk to.
  3. Final Answer:

    Each device has a unique address to identify it on the bus -> Option C
  4. Quick Check:

    Unique addresses allow multiple devices on same wires [OK]
Hint: Remember: unique addresses let devices share wires [OK]
Common Mistakes:
  • Thinking devices use separate wires
  • Assuming different clock speeds separate devices
  • Believing devices use different power pins to communicate
2. Which Arduino code snippet correctly initializes the I2C bus for multiple devices?
easy
A. Wire.init();
B. Wire.begin();
C. Wire.begin(0x3C);
D. Wire.start();

Solution

  1. Step 1: Recall the correct function to start I2C

    The Arduino Wire library uses Wire.begin() to start the I2C bus as master.
  2. Step 2: Check other options for correctness

    Wire.begin(0x3C) sets device as slave with address 0x3C, not for master init. Wire.init() and Wire.start() do not exist in Wire library.
  3. Final Answer:

    Wire.begin(); -> Option B
  4. Quick Check:

    Wire.begin() starts I2C master bus [OK]
Hint: Use Wire.begin() once to start I2C bus [OK]
Common Mistakes:
  • Using Wire.begin(address) to start master
  • Calling non-existent Wire.init() or Wire.start()
  • Not calling Wire.begin() at all
3. Given this Arduino code snippet, what will be the output on the Serial Monitor if device at address 0x40 returns 25 and device at 0x41 returns 30?
Wire.begin();
Wire.beginTransmission(0x40);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(0x40, 1);
int temp1 = Wire.read();
Wire.beginTransmission(0x41);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(0x41, 1);
int temp2 = Wire.read();
Serial.print(temp1);
Serial.print(", ");
Serial.println(temp2);
medium
A. Compilation error
B. 30, 25
C. 0, 0
D. 25, 30

Solution

  1. Step 1: Understand the communication sequence

    The code requests one byte from device 0x40, reads it as temp1 (25), then requests one byte from 0x41, reads it as temp2 (30).
  2. Step 2: Check the Serial output commands

    Serial.print(temp1) prints 25, then ", ", then Serial.println(temp2) prints 30 with newline.
  3. Final Answer:

    25, 30 -> Option D
  4. Quick Check:

    Read values match device addresses in order [OK]
Hint: Match device address to read value order [OK]
Common Mistakes:
  • Mixing up device addresses and values
  • Assuming output is reversed
  • Thinking code causes compile error
4. This Arduino code tries to read from two I2C devices but always reads zero. What is the likely error?
Wire.begin();
Wire.beginTransmission(0x50);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(0x50, 1);
int val1 = Wire.read();
Wire.beginTransmission(0x50);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(0x51, 1);
int val2 = Wire.read();
Serial.println(val1 + val2);
medium
A. Requesting from 0x51 instead of 0x50 causes no data
B. Missing Wire.begin() call
C. Using Wire.write() before Wire.requestFrom() is wrong
D. Serial.println() cannot print integers

Solution

  1. Step 1: Analyze device addresses used

    The code writes to 0x50 twice but requests data from 0x50 first, then 0x51 second. If 0x51 device is not connected, no data is returned.
  2. Step 2: Understand impact on read values

    Reading from 0x51 returns no data, so Wire.read() returns 0, causing sum to be zero or incorrect.
  3. Final Answer:

    Requesting from 0x51 instead of 0x50 causes no data -> Option A
  4. Quick Check:

    Address mismatch causes no data read [OK]
Hint: Check device addresses match in all calls [OK]
Common Mistakes:
  • Confusing write and requestFrom addresses
  • Forgetting to call Wire.begin() once
  • Thinking Wire.write() before requestFrom() is invalid
5. You want to connect three I2C temperature sensors with addresses 0x48, 0x49, and 0x4A to an Arduino. Which of the following is the best approach to read all sensors reliably?
hard
A. Call Wire.begin() once, then read each sensor by its unique address with Wire.requestFrom()
B. Call Wire.begin() before reading each sensor separately
C. Use separate SDA and SCL wires for each sensor to avoid conflicts
D. Assign the same address to all sensors and read them one by one

Solution

  1. Step 1: Understand I2C bus initialization

    Wire.begin() must be called once to initialize the bus as master before any communication.
  2. Step 2: Read sensors by unique addresses on shared bus

    Each sensor has a unique address; read each by calling Wire.requestFrom() with that address on the same SDA and SCL lines.
  3. Step 3: Evaluate other options

    Calling Wire.begin() multiple times is unnecessary. Using separate wires defeats I2C design. Assigning same address causes conflicts.
  4. Final Answer:

    Call Wire.begin() once, then read each sensor by its unique address with Wire.requestFrom() -> Option A
  5. Quick Check:

    One Wire.begin(), unique addresses, shared wires [OK]
Hint: Initialize once, read each device by unique address [OK]
Common Mistakes:
  • Calling Wire.begin() multiple times
  • Trying separate wires for each I2C device
  • Using same address for multiple devices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard