An I2C scanner sketch helps you find devices connected to your Arduino's I2C bus. It tells you the addresses of all connected I2C devices so you know where to talk to them.
I2C scanner sketch in Arduino
for (byte address = 1; address < 127; address++) { Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { // Device found at this address } }
This loop tries to communicate with every possible I2C address from 1 to 126.
If the device responds without error, it means a device is present at that address.
Wire.begin(); Serial.begin(9600); for (byte address = 1; address < 127; address++) { Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { Serial.print("Device found at address 0x"); Serial.println(address, HEX); } }
Wire.begin(); Serial.begin(9600); for (byte address = 8; address < 120; address++) { Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { Serial.print("Found device at "); Serial.println(address); } }
This complete sketch scans all possible I2C addresses and prints each found device's address in hexadecimal format. It also prints how many devices were found or if none were found.
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); // Wait for Serial to be ready Serial.println("I2C Scanner starting..."); byte count = 0; for (byte address = 1; address < 127; address++) { Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); count++; } } if (count == 0) { Serial.println("No I2C devices found."); } else { Serial.print(count); Serial.println(" device(s) found."); } } void loop() { // Nothing to do here }
Make sure your I2C devices are powered and connected correctly before running the scanner.
Some devices may not respond to scanning if they require special initialization.
The scanner uses the Wire library, which is standard for Arduino I2C communication.
An I2C scanner helps find all devices connected to the I2C bus by checking each address.
It prints the addresses of devices found so you can use them in your programs.
Running this sketch is a quick way to check wiring and device presence.
Practice
I2C scanner sketch in Arduino programming?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 BQuick Check:
I2C scanner = device finder [OK]
- Thinking it sends data to devices
- Confusing it with board reset functions
- Assuming it measures electrical signals
Solution
Step 1: Recall the Wire library initialization
The Wire library usesWire.begin();to start I2C communication.Step 2: Check other options
Functions likeWire.start();,Wire.init();, andWire.connect();do not exist in the Wire library.Final Answer:
Wire.begin(); -> Option AQuick Check:
Start I2C with Wire.begin() [OK]
- Using non-existent Wire functions
- Confusing with other library methods
- Omitting Wire.begin() causing no communication
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?
Solution
Step 1: Understand the address printing format
The code prints the address in hexadecimal usingSerial.println(address, HEX);. Hex 8 is '8', hex 42 decimal is '2A'.Step 2: Check leading zeros
SinceSerial.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 AQuick Check:
Hex 8 = 8, Hex 42 = 2A [OK]
- Assuming leading zeros appear automatically
- Confusing decimal 42 with hex 42
- Expecting uppercase letters always
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?
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 CQuick Check:
Scan addresses 1 to 127 [OK]
- Including address 0 which is reserved
- Calling Wire.endTransmission() multiple times unnecessarily
- Thinking Serial.print() can't print HEX
Wire.endTransmission() returns 4 (other error) and print a summary count of devices found at the end. Which code snippet correctly implements this?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 DQuick Check:
Skip error 4 with continue, count devices [OK]
- Using break instead of continue on error 4
- Starting loop at 0 instead of 1
- Decreasing count on error instead of skipping
Ready to prove it?
Pass the test and this pattern moves unknown
