How to Scan I2C Address of Sensors with Arduino
How to Scan I2C Address of Sensors with Arduino
Why Finding I2C Address Matters
I2C communication requires specifying the device address before sending data. Multiple devices on the same bus are distinguished by their unique addresses. This is why you need to know the I2C address before using any sensor or module.
Hardware Wiring
For Arduino Uno, I2C uses pin A4 (SDA) and A5 (SCL). Check the table below for wiring details:
Requirements:
- Arduino Uno
- I2C Module or Sensor to scan
- 4 Jumper wires (male-to-female)
I2C Scanner Code
This code scans addresses from 8 to 119 and displays found addresses on the Serial Monitor.
#include <Wire.h>
void setup() {
Serial.begin(115200);
// Wait for serial connection (for Leonardo/Micro)
while (!Serial);
Serial.println("I2C Scanner Starting...");
Wire.begin();
byte deviceCount = 0;
// Scan addresses 8-119 (0x08 - 0x77)
for (byte address = 8; address < 120; address++) {
Wire.beginTransmission(address);
byte response = Wire.endTransmission();
// Response = 0 means device responded
if (response == 0) {
Serial.print("Found address: ");
Serial.print(address, DEC);
Serial.print(" (0x");
Serial.print(address, HEX);
Serial.println(")");
deviceCount++;
delay(10);
}
}
Serial.println("Scan Complete");
Serial.print("Total devices found: ");
Serial.println(deviceCount);
}
void loop() {
// Nothing to do
}
Step-by-Step Instructions
Step 1 Connect I2C wires according to the table above. Make sure VCC connects to 5V (unless your device supports only 3.3V)
Step 2 Upload the I2C Scanner code to Arduino
Step 3 Open Serial Monitor and set Baud Rate to 115200
Step 4 Press the Reset button on Arduino once, then wait for results
Reading the Results
If wiring is correct, Serial Monitor displays output like this:
I2C Scanner Starting...
Found address: 39 (0x27)
Scan Complete
Total devices found: 1
The address appears in both Decimal and Hexadecimal formats. For actual coding, use the Hexadecimal value such as 0x27
Practical Example
After finding the address, use it with libraries. Here’s an example with I2C LCD:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Use the address you scanned
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin();
lcd.print("Hello I2C");
}
void loop() {}
[image: Circuit diagram showing Arduino Uno connected to I2C LCD 1602 Module with all 4 wires connected]
Summary
Finding the I2C address is a fundamental step before using any I2C device. Keep in mind that some devices have jumpers to change the address. When connecting multiple devices on the same bus, make sure each device has a unique address.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย