Bird
0
0
Arduinoprogramming~10 mins

I2C scanner sketch in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - I2C scanner sketch
Start setup
Initialize I2C bus
For each address 1 to 127
Try to communicate
Print address
Next address
End loop
Print done
The sketch initializes the I2C bus, then tries to communicate with each possible device address from 1 to 127. If a device responds, it prints the address. After checking all, it prints done.
Execution Sample
Arduino
void setup() {
  Wire.begin();
  Serial.begin(9600);
  for (byte address = 1; address < 128; address++) {
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();
    if (error == 0) {
      Serial.println(address);
    }
  }
  Serial.println("Done");
}

void loop() {
  // Empty loop
}
This code scans all I2C addresses and prints those where a device responds.
Execution Table
StepAddressWire.beginTransmission(address)Wire.endTransmission() resultCondition (error==0)ActionOutput
11Start transmission to 1No device (error=4)FalseNo print
22Start transmission to 2No device (error=4)FalseNo print
33Start transmission to 3Device found (error=0)TruePrint address3
44Start transmission to 4No device (error=4)FalseNo print
.....................
126126Start transmission to 126No device (error=4)FalseNo print
127127Start transmission to 127No device (error=4)FalseNo print
128End loop---Print "Done"Done
💡 Loop ends after checking address 127; 'Done' printed to signal completion.
Variable Tracker
VariableStartAfter 1After 2After 3...After 127Final
addressN/A123...127Loop ends
errorN/A440...4N/A
Key Moments - 3 Insights
Why do we start scanning from address 1 and not 0?
Address 0 is reserved for general call in I2C, so scanning starts from 1 as shown in execution_table rows 1 and 2.
What does error code 0 mean in Wire.endTransmission()?
Error 0 means a device acknowledged the address, so the condition error==0 is true and the address is printed (see execution_table row 3).
Why do we print 'Done' after the loop?
'Done' signals the scan finished checking all addresses, as shown in the last row of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the error code when a device is found at address 3?
A4
B1
C0
D255
💡 Hint
Check the 'Wire.endTransmission() result' column at step 3 in execution_table.
At which address does the loop stop scanning?
A126
B127
C128
D1
💡 Hint
Look at the 'address' variable in variable_tracker and the last rows in execution_table.
If a device responded at address 5, what would happen in the execution_table?
Aerror would be 0 and address 5 would be printed
Berror would be 4 and no print
Cloop would stop immediately
Daddress 5 would be skipped
💡 Hint
Refer to how address 3 is handled in execution_table row 3.
Concept Snapshot
I2C Scanner Sketch
- Initialize I2C bus with Wire.begin()
- Loop addresses 1 to 127
- Use Wire.beginTransmission(address) and Wire.endTransmission()
- If endTransmission returns 0, device found
- Print found address
- Print 'Done' after scanning all
Useful to detect connected I2C devices.
Full Transcript
This I2C scanner sketch starts by initializing the I2C bus and serial communication. It then loops through all possible I2C addresses from 1 to 127. For each address, it tries to start a transmission and ends it to check if a device responds. If the response error code is 0, it prints the address to the serial monitor. After checking all addresses, it prints 'Done' to signal the scan is complete. This helps find which devices are connected on the I2C bus.