Challenge - 5 Problems
I2C Scanner 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 scanner sketch?
Consider this Arduino I2C scanner sketch. What will it print to the Serial Monitor when no I2C devices are connected?
Arduino
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices = 0; Serial.println("Scanning..."); for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); }
Attempts:
2 left
💡 Hint
Think about what happens when no devices respond to the I2C address scan.
✗ Incorrect
The sketch scans all addresses from 1 to 126. If no device responds (error != 0), it prints "No I2C devices found". So with no devices connected, only that message appears after scanning.
❓ Predict Output
intermediate2:00remaining
What is the output if a device is connected at address 0x3C?
Using the same I2C scanner sketch, what will the Serial Monitor show if a device is connected at address 0x3C?
Arduino
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices = 0; Serial.println("Scanning..."); for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); }
Attempts:
2 left
💡 Hint
Check the address range and how the address is printed in hexadecimal.
✗ Incorrect
The device at 0x3C responds, so the scanner prints that address with leading zero if less than 0x10. 0x3C is 60 decimal, so no leading zero is added. The output shows the found device and then "done".
🧠 Conceptual
advanced2:00remaining
Why does the scanner skip address 0x00 and 0x7F?
In the I2C scanner sketch, the for loop scans addresses from 1 to 126. Why does it skip 0x00 and 0x7F?
Attempts:
2 left
💡 Hint
Think about special reserved addresses in the I2C protocol.
✗ Incorrect
Address 0x00 is the general call address used to address all devices simultaneously. Address 0x7F is reserved and not used for devices. Scanning these can cause issues or meaningless results.
🔧 Debug
advanced2:00remaining
What error does this modified I2C scanner code produce?
This code snippet is a modified I2C scanner. What error will it cause when compiled or run?
#include
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("I2C Scanner");
}
void loop() {
byte error, address;
int nDevices = 0;
Serial.println("Scanning...");
for(address = 0; address <= 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device at 0x");
Serial.println(address, HEX);
nDevices++;
}
}
delay(5000);
}
Attempts:
2 left
💡 Hint
Check the address range and the data type used for address variable.
✗ Incorrect
The byte type can hold values 0 to 255, so no overflow occurs. The code compiles fine. Addresses 0 and 127 are special in I2C and may cause unexpected responses, but no runtime error occurs.
🚀 Application
expert3:00remaining
How to modify the I2C scanner to detect multiple devices and print their addresses in ascending order?
You want to modify the I2C scanner sketch to store all found device addresses in an array and then print them sorted ascending after scanning completes. Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Arduino C++ does not support std::sort by default. qsort requires a function declared before use.
✗ Incorrect
Option A implements a manual bubble sort to order the addresses ascending, then prints them. Option A prints unsorted. Option A uses qsort but the compare function is declared after usage, causing compile error. Option A uses std::sort which is not available in Arduino environment by default.
