Skip to content

Tutorial

MicroPython I2C Scan on ESP32: Find Devices, Read Addresses and Fix Empty Scans

Find every I2C device on an ESP32 with MicroPython: a diagnostic scanner that names devices, an address table, second bus and SoftI2C, chip-ID checks and fixes.

  • 11 min read
  • Updated November 14, 2026
MicroPython I2C Scan on ESP32: Find Devices, Read Addresses and Fix Empty Scans

Quick answer: to scan the I2C bus with MicroPython on an ESP32, run this in the Thonny shell:

main.py
from machine import Pin, I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
print([hex(addr) for addr in i2c.scan()])

Each address in the list is a device that answered, for example ['0x3c'] for an SSD1306 OLED or ['0x76'] for a BME280. An empty list means nothing answered: check power, swapped SDA/SCL, missing pull-up resistors or a loose wire. scan() returns plain integers (60 = 0x3C), so print them as hex to match datasheets. The address lookup table, a full diagnostic script and a fix table are below.

ItemESP32 default
SDAGPIO21
SCLGPIO22
ControllersI2C(0) and I2C(1); any free GPIO can be routed to either
Speeds100 kHz (standard), 400 kHz (fast)
Logic level3.3 V

How I2C and scanning work

I2C uses two shared wires: SDA (data) and SCL (clock), plus ground. Every device on the bus has a 7-bit address. To scan, the ESP32 sends each address from 0x08 to 0x77 and listens for an acknowledgement (ACK). Anything that answers is added to the list. Nothing is read or written, so a scan is always safe.

Both lines are open-drain: devices only pull them low, and pull-up resistors pull them back high. Most breakout boards include pull-ups (usually 4.7–10 kΩ); a bare chip needs them added. Too many boards on one bus, each with its own pull-ups, can make the combined pull-up too strong, which is a common cause of flaky buses with five or more modules.

1. Wire one known device first

Module pinESP32
VCC / VIN3.3 V (or the module's rated supply)
GNDGND
SDAGPIO21
SCLGPIO22

Start with a single module you know is good. Once it shows up, add the others one at a time. All devices share the same SDA and SCL wires; each just needs a different address. Pin details: ESP32 I2C pins and multiple devices.

2. A full diagnostic scan script

Save as i2c_scan.py and run it whenever something on the bus misbehaves:

main.py
from machine import Pin, I2C
import time

KNOWN = {
    0x20: "PCF8574 I/O expander", 0x27: "PCF8574 LCD backpack", 0x3F: "PCF8574A LCD backpack",
    0x23: "BH1750 light sensor", 0x29: "VL53L0X distance / TSL2591", 0x38: "AHT10/AHT20",
    0x3C: "SSD1306/SH1106 OLED", 0x3D: "SSD1306 OLED (alt)", 0x40: "INA219 / HTU21D / SHT21",
    0x44: "SHT31/SHT40", 0x48: "ADS1115 / TMP102", 0x50: "EEPROM (AT24Cxx)",
    0x57: "MAX30102 / DS3231 EEPROM", 0x5A: "MLX90614 / CCS811", 0x68: "MPU6050 / DS3231 RTC",
    0x69: "MPU6050 (AD0 high)", 0x76: "BME280/BMP280", 0x77: "BME280/BMP280 (alt) / BMP180",
}

def scan(sda=21, scl=22, freq=100000):
    i2c = I2C(0, scl=Pin(scl), sda=Pin(sda), freq=freq)
    found = i2c.scan()
    print("SDA=GPIO{} SCL=GPIO{} at {} kHz: {} device(s)".format(sda, scl, freq // 1000, len(found)))
    for a in found:
        print("  0x{:02X}  {}".format(a, KNOWN.get(a, "unknown")))
    if not found:
        sda_level, scl_level = Pin(sda, Pin.IN).value(), Pin(scl, Pin.IN).value()
        print("  No devices. Idle levels: SDA={} SCL={} (both should be 1)".format(sda_level, scl_level))
        if sda_level == 0 or scl_level == 0:
            print("  A line is stuck low: short, wrong wiring or a hung device")
        else:
            print("  Lines are high: check power, SDA/SCL swap and the device address")
    return found

scan()

Example output:

Output
SDA=GPIO21 SCL=GPIO22 at 100 kHz: 2 device(s)
  0x3C  SSD1306/SH1106 OLED
  0x76  BME280/BMP280

Checking the idle level of each line is the quickest way to tell "no pull-ups or stuck line" (a 0) from "device not powered or wrong address" (both 1).

3. Common I2C addresses

AddressDeviceChange it with
0x27 / 0x3F16×2 / 20×4 LCD with PCF8574 backpackA0–A2 solder pads
0x3C / 0x3DSSD1306 OLEDResistor or jumper on the back
0x68 / 0x69MPU6050, MPU9250AD0 pin LOW / HIGH
0x68DS3231 RTC (clashes with MPU6050!)Fixed: put one on the second bus
0x76 / 0x77BME280, BMP280SDO pin to GND / VCC
0x23 / 0x5CBH1750ADDR pin LOW / HIGH
0x48–0x4BADS1115ADDR pin to GND, VDD, SDA or SCL
0x40INA219, HTU21D, SHT21INA219: A0/A1 pads
0x57MAX30102Fixed
0x29VL53L0XSoftware-changeable at runtime

Two devices with the same fixed address? Put the second one on the other controller, e.g. I2C(1, scl=Pin(26), sda=Pin(25)), or use a TCA9548A I2C multiplexer (address 0x70). The MPU6050 vs ICM-20948 comparison covers IMU addresses in more depth.

4. Second bus, other pins and SoftI2C

main.py
from machine import Pin, I2C, SoftI2C

bus0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)   # hardware bus 0
bus1 = I2C(1, scl=Pin(26), sda=Pin(25), freq=100000)   # hardware bus 1, any free pins
soft = SoftI2C(scl=Pin(19), sda=Pin(18), freq=100000)  # bit-banged, any pins
print(bus0.scan(), bus1.scan(), soft.scan())

The ESP32 has two hardware controllers, and both can use (almost) any GPIO. SoftI2C drives the pins in software: slower and less tolerant of timing, but useful as a third bus or for testing. Avoid input-only GPIO34–39 (they can't drive the bus) and the boot-strapping pins 0, 2, 12 and 15.

Raspberry Pi Pico note: on the RP2040, I2C only works on fixed pin pairs, e.g. I2C(0, scl=Pin(5), sda=Pin(4)). See the Pico W pinout table.

Bus speed, wire length and pull-ups

SituationFrequencyPull-upsNotes
One or two breakouts on a breadboard400 kHzOn-board (usually 4.7–10 kΩ)Fast refresh for displays
Several modules, jumper wires up to ~30 cm100 kHzKeep only one or two boards' pull-upsRemove extra pull-up resistors if the bus gets flaky
Wires longer than ~50 cm100 kHz or lower2.2–4.7 kΩ to 3.3 VTwist SDA and SCL with GND; consider an I2C bus extender (e.g. P82B715)
Sensor in another roomDon't use plain I2C—Use a microcontroller at the sensor and send data over Wi-Fi, RS485 or MQTT

I2C was designed for short connections on one circuit board. Every centimetre of wire adds capacitance, which rounds off the signal edges until the ESP32 misreads bits. If a scan finds a device at 100 kHz but not at 400 kHz, the bus is telling you it's at its limit: slow down, shorten the wires or strengthen the pull-ups.

5. After the scan: talk to the device

A device in the scan list is alive. The next step is its driver, or a direct register read to prove it's the chip you expect. Many sensors have a WHO_AM_I or chip-ID register:

main.py
# BME280 / BMP280: chip ID register 0xD0 (0x60 = BME280, 0x58 = BMP280)
chip = i2c.readfrom_mem(0x76, 0xD0, 1)[0]
print(hex(chip))

# MPU6050: WHO_AM_I register 0x75 should read 0x68
print(hex(i2c.readfrom_mem(0x68, 0x75, 1)[0]))

That BME280 check is how you spot boards sold as "BME280" that are really BMP280s (no humidity). Then load the driver: SSD1306 OLED, or a BME280 driver from mip.

6. Troubleshooting

SymptomLikely causeFix
[] and both lines read 1Device unpowered, SDA/SCL swapped, wrong pins in codeCheck VCC/GND; swap SDA and SCL; match the GPIOs in I2C(...)
[] and a line reads 0Short to GND, wiring mistake, or a device holding SDA lowUnplug devices one by one; power-cycle to release a hung device
Every address from 0x08 to 0x77 foundSDA and SCL shorted together, or no pull-ups at allCheck wiring; add 4.7 kΩ pull-ups to 3.3 V
Device appears only sometimesLong wires, weak pull-ups, 400 kHz too fastShorter wires, 100 kHz, 4.7 kΩ pull-ups
OSError: [Errno 19] ENODEV in the driverDriver uses a different address than the scan showsPass the scanned address to the driver
OSError: [Errno 116] ETIMEDOUTBus stuck or clock stretching too longLower the frequency; power-cycle the devices
Works alone, fails with several modulesToo many pull-ups in parallel, or address clashRemove extra pull-up resistors; check addresses
5 V module not found5 V pull-ups or a module needing 5 V logicUse a level shifter, or a 3.3 V-compatible module

Diagnose an I2C bus methodically

When a scan returns an empty list, change one thing at a time. Start with one known-good breakout, short jumper wires and the default 100 kHz clock. Confirm the module's pin labels rather than assuming its connector order: boards that look similar can place VCC, GND, SDA and SCL differently.

Read the idle bus levels

With the devices connected and powered, SDA and SCL should normally idle HIGH. If either line stays LOW, disconnect modules one at a time to find a device or wiring fault that is holding the bus down. Check for a short between SDA and GND, SCL and GND, or between the two signal lines. A line stuck LOW is different from a device that simply does not acknowledge its address.

If both lines remain HIGH but the scan is empty, check that the module is powered, that the code uses the pins actually wired, and that the device is using the address you expect. Some boards expose an address-select jumper; others have a fixed address. A scan identifies responding addresses, not the exact chip model, so use a chip-ID register or the manufacturer's driver when you need to distinguish compatible devices.

Understand pull-ups and voltage levels

I2C devices use open-drain outputs, so pull-up resistors are required. Many sensor breakouts already include them. When several modules are connected, their pull-ups are effectively in parallel: two 4.7 kΩ resistors produce about 2.35 kΩ, and adding more can make the total resistance too low for the devices. Too-strong pull-ups increase sink current; too-weak pull-ups make rising edges slow. If the bus is unreliable, inspect the breakout schematics and calculate the combined resistance rather than adding resistors blindly.

The ESP32 GPIOs are 3.3 V logic and are not 5 V tolerant. A module powered from 5 V may pull SDA and SCL up to 5 V even if its controller accepts 3.3 V signals. Check the board documentation and measure the idle voltage. Use a proper bidirectional I2C level shifter when the bus sides use different logic voltages; do not connect a 5 V pull-up directly to an ESP32 pin.

Recover from a stuck bus safely

First remove power from the sensor modules and ESP32, then reconnect them after checking for shorts. If a peripheral was interrupted halfway through a transaction, it may hold SDA LOW. Some systems can recover by pulsing SCL up to nine times and then generating a STOP condition, but this is a bus-recovery technique, not a substitute for fixing the wiring or power problem. Only attempt it when the board's electrical design and the connected devices permit it.

Once the bus scans reliably at 100 kHz, try 400 kHz if the devices support it and the wiring is short. If the scan works at 100 kHz but fails at 400 kHz, keep the lower speed or improve the physical bus. Long jumper wires, breadboard contacts and multiple breakout boards add capacitance; I2C is intended for short on-board connections, not long cables between rooms.

FAQ

Why does i2c.scan() return numbers like 60 instead of 0x3C? They're the same value in decimal. Print with hex() to match datasheets.

How many devices can share one I2C bus? Up to about 100 addresses in theory; in practice a handful of breakouts, limited by wire length, capacitance and combined pull-ups.

Do I need pull-up resistors? Almost every breakout board already has them. Bare chips need 4.7–10 kΩ from SDA and SCL to 3.3 V.

What's the difference between I2C and SoftI2C? I2C uses the hardware controller (faster, reliable); SoftI2C toggles pins in software (any pins, slower).

Next steps

Official reference: MicroPython machine.I2C.

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.