📖 Scenario: You have connected several devices to your Arduino's I2C bus. You want to find out which devices are connected and their addresses.
🎯 Goal: Build a simple Arduino sketch that scans all possible I2C addresses and prints the addresses of connected devices to the Serial Monitor.
📋 What You'll Learn
Create a variable to store the starting I2C address
Create a variable to store the ending I2C address
Use a for loop to scan all addresses from start to end
Use Wire.beginTransmission() and Wire.endTransmission() to check if a device responds
Print the found device addresses in hexadecimal format to the Serial Monitor
💡 Why This Matters
🌍 Real World
This sketch helps you find the addresses of I2C devices connected to your Arduino, which is useful when setting up sensors or displays.
💼 Career
Understanding how to scan and communicate with I2C devices is important for embedded systems and hardware interfacing jobs.
Progress0 / 4 steps
1
Setup I2C scanning range
Create two integer variables called startAddress and endAddress. Set startAddress to 1 and endAddress to 127.
Arduino
Hint
Use int to create the variables and assign the numbers 1 and 127 exactly.
2
Initialize I2C and Serial communication
Add the Wire.h library include. In the setup() function, start the I2C bus with Wire.begin() and start serial communication at 9600 baud with Serial.begin(9600).
Arduino
Hint
Remember to include Wire.h at the top. Use Wire.begin() and Serial.begin(9600) inside setup().
3
Scan I2C addresses
Inside the setup() function, add a for loop that uses address from startAddress to endAddress. For each address, call Wire.beginTransmission(address) and then Wire.endTransmission(). If Wire.endTransmission() returns 0, print "Found device at 0x" followed by the address in hexadecimal format using Serial.print(address, HEX) and then print a new line with Serial.println().
Arduino
Hint
Use a for loop with address from startAddress to endAddress. Check if Wire.endTransmission() returns 0 to detect a device.
4
Print scan completion message
After the for loop inside setup(), add a line to print "Scan complete." to the Serial Monitor using Serial.println().
Arduino
Hint
Use Serial.println("Scan complete.") after the loop to show the scan is done.
Practice
(1/5)
1. What is the main purpose of an I2C scanner sketch in Arduino programming?
easy
A. To reset the Arduino board automatically
B. To detect and list all devices connected on the I2C bus
C. To send data to all I2C devices simultaneously
D. To measure voltage levels on the I2C pins
Solution
Step 1: Understand the role of I2C scanner
An I2C scanner checks each possible address on the I2C bus to find connected devices.
Step 2: Identify the main function
The main function is to detect and list all devices so you know which addresses are in use.
Final Answer:
To detect and list all devices connected on the I2C bus -> Option B
Quick Check:
I2C scanner = device finder [OK]
Hint: I2C scanner finds devices by checking addresses [OK]
Common Mistakes:
Thinking it sends data to devices
Confusing it with board reset functions
Assuming it measures electrical signals
2. Which of the following is the correct way to start the I2C communication in an Arduino I2C scanner sketch?
easy
A. Wire.begin();
B. Wire.start();
C. Wire.init();
D. Wire.connect();
Solution
Step 1: Recall the Wire library initialization
The Wire library uses Wire.begin(); to start I2C communication.
Step 2: Check other options
Functions like Wire.start();, Wire.init();, and Wire.connect(); do not exist in the Wire library.
Final Answer:
Wire.begin(); -> Option A
Quick Check:
Start I2C with Wire.begin() [OK]
Hint: Use Wire.begin() to start I2C communication [OK]
Common Mistakes:
Using non-existent Wire functions
Confusing with other library methods
Omitting Wire.begin() causing no communication
3. Consider this snippet from an I2C scanner sketch:
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device found at 0x");
Serial.println(address, HEX);
}
}
What will this code print if devices are found at addresses 8 and 42?
medium
A. Device found at 0x8\nDevice found at 0x2A
B. Device found at 0x08\nDevice found at 0x2A
C. Device found at 0x08\nDevice found at 0x42
D. Device found at 0x8\nDevice found at 0x42
Solution
Step 1: Understand the address printing format
The code prints the address in hexadecimal using Serial.println(address, HEX);. Hex 8 is '8', hex 42 decimal is '2A'.
Step 2: Check leading zeros
Since Serial.println() with HEX does not pad zeros, 8 prints as '8', 42 prints as '2A'.
Final Answer:
Device found at 0x8\nDevice found at 0x2A -> Option A
Quick Check:
Hex 8 = 8, Hex 42 = 2A [OK]
Hint: Hex print does not add leading zeros by default [OK]
Common Mistakes:
Assuming leading zeros appear automatically
Confusing decimal 42 with hex 42
Expecting uppercase letters always
4. You wrote this I2C scanner code but it never finds any devices:
for (byte address = 0; address <= 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Found device at ");
Serial.println(address, HEX);
}
}
What is the likely problem?
medium
A. Wire.endTransmission() must be called twice
B. Address 0 is reserved and should not be scanned
C. The loop should start from 1, not 0
D. Serial.print() cannot print HEX values
Solution
Step 1: Check the address range in the loop
The I2C valid addresses range from 1 to 127. Starting at 0 is invalid and may cause issues.
Step 2: Understand scanning addresses
Starting from 1 avoids reserved addresses and ensures proper scanning.
Final Answer:
The loop should start from 1, not 0 -> Option C
Quick Check:
Scan addresses 1 to 127 [OK]
Hint: Start scanning from address 1, not 0 [OK]
Common Mistakes:
Including address 0 which is reserved
Calling Wire.endTransmission() multiple times unnecessarily
Thinking Serial.print() can't print HEX
5. You want to modify an I2C scanner sketch to skip addresses where Wire.endTransmission() returns 4 (other error) and print a summary count of devices found at the end. Which code snippet correctly implements this?
hard
A. int count = 0;
for (byte addr = 1; addr < 128; addr++) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device at 0x"); Serial.println(addr, HEX);
count++;
} else if (error == 4) {
count--;
}
}
Serial.print("Total devices: "); Serial.println(count);
B. int count = 0;
for (byte addr = 0; addr <= 127; addr++) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device at 0x"); Serial.println(addr, HEX);
count++;
} else if (error == 4) {
break;
}
}
Serial.print("Total devices: "); Serial.println(count);
C. int count = 0;
for (byte addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device at 0x"); Serial.println(addr, HEX);
count++;
} else if (error == 4) {
Serial.println("Error 4 at address " + addr);
}
}
Serial.print("Total devices: "); Serial.println(count);
D. int count = 0;
for (byte addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device at 0x"); Serial.println(addr, HEX);
count++;
} else if (error == 4) {
continue;
}
}
Serial.print("Total devices: "); Serial.println(count);
Solution
Step 1: Set correct address range and initialize count
The loop must scan addresses 1 to 126 (less than 127). Initialize count to zero before loop.
Step 2: Handle error 4 by skipping and count devices found
If error is 0, print address and increment count. If error is 4, skip with continue to avoid stopping the scan.
Step 3: Print total devices after loop
After scanning, print the total count of devices found.
Final Answer:
Code snippet A correctly implements the requirements -> Option D
Quick Check:
Skip error 4 with continue, count devices [OK]
Hint: Use continue to skip error 4 and count devices found [OK]
Common Mistakes:
Using break instead of continue on error 4
Starting loop at 0 instead of 1
Decreasing count on error instead of skipping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.