Sub topic 5
Finding Your Display's I2C Address
This chapter assumes you can upload a sketch with the Arduino IDE and open the Serial Monitor. It also assumes you have wired a breadboard with power, ground, and signal wires, and that you know the basic idea of I2C from the previous chapter: two shared wires (SDA for data, SCL for the clock) and one address per device. Here you will use a scanner sketch to find the address of an LCD backpack or an OLED. You will also learn how to read the scanner's output and what to do when it finds nothing.
Why you need the address
Every I2C device on a bus answers to a 7-bit address, a number from 0 to 127. The controller (your Arduino) starts each conversation by sending an address. Only the device that owns that address responds.
Display libraries need this number. In the I2C LCD sketch you write LiquidCrystal_I2C lcd(0x27, 16, 2);, and in the OLED sketch you pass 0x3C to display.begin(). If the number in your code does not match the number the hardware answers to, the display stays blank. The code compiles and uploads without errors, so nothing tells you the address is wrong.
Datasheets and product pages give typical addresses, but they are not reliable for a specific board. The same-looking module can ship with different chips or jumper settings. A scanner removes the guesswork by asking the bus which addresses respond.
Common addresses
The values below are typical, not guaranteed.
| Device | Typical address | Notes |
|---|---|---|
| LCD backpack with PCF8574 chip | 0x27 | Most common for generic modules |
| LCD backpack with PCF8574A chip | 0x3F | Same function, different chip variant |
| SSD1306 OLED | 0x3C | Some modules use 0x3D |
| DS3231 real-time clock | 0x68 | Useful to recognize on a shared bus |
A PCF8574 is an I/O expander chip. It turns the two I2C wires into eight output pins that drive the LCD. The PCF8574 family uses addresses 0x20 to 0x27, and the PCF8574A family uses 0x38 to 0x3F. Solder jumpers labeled A0, A1, and A2 on the backpack change the low three bits of the address, so a module with none of them bridged and one with all of them bridged answer differently.
How the scanner works
The scanner does not need to know what is connected. It loops through every possible address and tries to start a transmission to each one. If a device with that address is present, it pulls the data line low for one clock pulse. This is called an acknowledge, or ACK. If nobody answers, the line stays high, which is a NACK (no acknowledge).
The Wire library reports this result through the return value of Wire.endTransmission(). A return value of 0 means the transmission succeeded and a device acknowledged. Other values are error codes.
The scanner sketch
This sketch tries addresses 1 through 126 and prints each address that acknowledges.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print("Device found at 0x");
Serial.println(address, HEX);
}
}
}
void loop() {}Here is what each part does:
Wire.begin()starts the Arduino as the bus controller and sets up the SDA and SCL pins.Wire.beginTransmission(address)queues a transmission to one address. Nothing is sent yet.Wire.endTransmission()sends the queued bytes. Because none were queued, only the address goes out, and the return value tells you whether anyone acknowledged it.Serial.println(address, HEX)prints the number in hexadecimal, the format libraries and datasheets use.- The scan runs once in
setup(), andloop()is empty. To scan again, press the reset button.
Running it
- Wire the display to the Arduino's power, ground, SDA, and SCL pins. On an Uno or Nano these are A4 (SDA) and A5 (SCL). On an Uno R3 they are also broken out next to AREF. Other boards use different pins, so check your board's pinout.
- Upload the sketch.
- Open the Serial Monitor and set the baud rate to 9600.
- Press reset if the output has already scrolled by or you opened the monitor late.
A successful run with an LCD backpack looks like this:
Device found at 0x27One quirk to know: the output does not pad hex values, so address 0x08 prints as 0x8. This is harmless, and you still write it as 0x08 or 0x8 in code.
Reading the results
The scanner can give four kinds of results. Each means something different.
| Output | Meaning | Next step |
|---|---|---|
| One address found | The display is present and answering | Copy the address into your sketch |
| Two or more addresses | Several devices share the bus | Identify which belongs to the display |
| Nothing found | No device acknowledged | Check wiring and power |
| Every address found | The bus is held in a bad state | Check for a short or a missing pull-up problem |
Several devices on the bus
I2C allows many devices on the same two wires, as long as their addresses differ. If you have an OLED at 0x3C and an RTC at 0x68, the scanner prints both. To tell which is which, unplug one device and run the scan again. The address that disappears belongs to the device you removed.
If two devices have the same address, they will both answer at once and corrupt each other's replies. The scanner shows one address, not two, so a clash can go unnoticed until the display misbehaves. Many modules offer address jumpers or solder pads for this reason. Changing one device's address resolves the clash.
Nothing found
A blank result is the most common problem. Work through these causes in order:
- Power and ground. Confirm that VCC goes to the correct supply (5 V for most LCD backpacks, 3.3 V or 5 V for OLED modules, so check yours) and that GND is connected to the Arduino's GND.
- SDA and SCL swapped. Swapping them produces no damage but no response either. Swap the wires back.
- Wrong pins. On a board that is not an Uno or Nano, SDA and SCL are on different pins. Boards also differ in whether the pins are labeled.
- Loose breadboard contacts. Reseat each jumper wire. A wire that looks seated may not touch the metal strip.
- Missing pull-up resistors. I2C needs pull-up resistors on both lines, which hold them high when no one is pulling them low. Most breakout boards include them. A bare chip on a breadboard does not.
- Dead module. If you have another module, try it.
Edge cases
The LCD backpack finds an address but the screen is blank
The address found by the scanner proves the PCF8574 chip works. It says nothing about the LCD behind it. A blank screen with a correct address usually has one of these causes:
- The contrast potentiometer on the backpack is set too low or too high. Turn the small blue screw slowly while the sketch runs.
- The backlight jumper is removed.
- The LCD is a 3.3 V part on a 5 V supply, or the reverse.
The chapter on troubleshooting covers these in detail.
The scanner finds 0x3C but your OLED sketch fails
The scanner and the OLED library are checking different things. The scanner only confirms the chip acknowledges. display.begin() also checks the library's configuration and tries to allocate the frame buffer in RAM. If the OLED is found at 0x3C but begin() returns false, check:
- The address in
begin()matches the scanner result exactly. - The width and height in the constructor match the panel (128x64 or 128x32).
- The board has enough free RAM for the buffer. A later chapter explains how much is needed.
Addresses above 0x77
Addresses 0x00 to 0x07 and 0x78 to 0x7F are reserved by the I2C standard. The scanner in this chapter checks 1 to 126 to keep its range simple. Some references use 8 to 119 (0x08 to 0x77) as the safe range for ordinary devices. Hardware you buy for hobby use should fall in that range, so a result outside it suggests a noisy bus or a wiring fault.
7-bit and 8-bit address notation
Some datasheets list an 8-bit address such as 0x4E or 0x7E. This value includes the read/write bit as the lowest bit. To get the 7-bit address that Arduino's Wire library expects, shift it right by one bit, which means dividing by 2. So 0x4E becomes 0x27, and 0x78 becomes 0x3C. If the number in your datasheet looks too large, this is probably why.
Bus speed and long wires
The Wire library defaults to 100 kHz. Long jumper wires add capacitance and can make scanning unreliable, which shows up as addresses appearing and vanishing between runs. Keep wires short during testing. If a scan gives different results on each run, shorten the wires before you suspect the module.
Using the result
Once you have the address, put it where the library expects it.
For the I2C LCD:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Use the address found by the scanner; 0x27 and 0x3F are common.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C LCD ready");
}
void loop() {}For the OLED:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {} // display not found: stop here
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Hello, OLED"));
display.display();
}
void loop() {}The LCD library takes the address in the constructor. The Adafruit OLED library takes it in begin(). Library forks differ in where they want the address and in method names such as init(), so check the example sketches that ship with the library you installed.
A quick recap of the workflow
Exercise
Your scanner prints "Device found at 0x3F" for an LCD backpack, but your sketch uses 0x27 and the screen stays blank. What do you change, and why does the sketch still compile and upload without errors?
Show answer
Change the address in the constructor to LiquidCrystal_I2C lcd(0x3F, 16, 2);. The compiler only checks that the number is valid C++, not that a device answers to it. The mismatch appears at run time, when the library sends data to an address nobody acknowledges.
Exercise
A datasheet for an OLED module lists its address as 0x78. What value do you pass to display.begin(), and how did you get it?
Show answer
Pass 0x3C. The datasheet gives the 8-bit form, which includes the read/write bit. Shifting right by one bit (dividing 0x78 by 2) gives the 7-bit address that the Wire library uses.
Exercise
You run the scanner with an OLED and an RTC connected, and it prints two addresses: 0x3C and 0x68. How can you confirm which one is the OLED without reading any datasheet?
Show answer
Unplug the OLED and run the scan again. The address that no longer appears (0x3C) belongs to the OLED. You can also reconnect it and unplug the RTC to confirm that 0x68 disappears.