Quick answer: to scan the I2C bus with MicroPython on an ESP32, run this in the Thonny shell:
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.
| Item | ESP32 default |
|---|---|
| SDA | GPIO21 |
| SCL | GPIO22 |
| Controllers | I2C(0) and I2C(1); any free GPIO can be routed to either |
| Speeds | 100 kHz (standard), 400 kHz (fast) |
| Logic level | 3.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 pin | ESP32 |
|---|---|
| VCC / VIN | 3.3 V (or the module's rated supply) |
| GND | GND |
| SDA | GPIO21 |
| SCL | GPIO22 |
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:
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:
SDA=GPIO21 SCL=GPIO22 at 100 kHz: 2 device(s)
0x3C SSD1306/SH1106 OLED
0x76 BME280/BMP280Checking 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
| Address | Device | Change it with |
|---|---|---|
| 0x27 / 0x3F | 16×2 / 20×4 LCD with PCF8574 backpack | A0–A2 solder pads |
| 0x3C / 0x3D | SSD1306 OLED | Resistor or jumper on the back |
| 0x68 / 0x69 | MPU6050, MPU9250 | AD0 pin LOW / HIGH |
| 0x68 | DS3231 RTC (clashes with MPU6050!) | Fixed: put one on the second bus |
| 0x76 / 0x77 | BME280, BMP280 | SDO pin to GND / VCC |
| 0x23 / 0x5C | BH1750 | ADDR pin LOW / HIGH |
| 0x48–0x4B | ADS1115 | ADDR pin to GND, VDD, SDA or SCL |
| 0x40 | INA219, HTU21D, SHT21 | INA219: A0/A1 pads |
| 0x57 | MAX30102 | Fixed |
| 0x29 | VL53L0X | Software-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
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
| Situation | Frequency | Pull-ups | Notes |
|---|---|---|---|
| One or two breakouts on a breadboard | 400 kHz | On-board (usually 4.7–10 kΩ) | Fast refresh for displays |
| Several modules, jumper wires up to ~30 cm | 100 kHz | Keep only one or two boards' pull-ups | Remove extra pull-up resistors if the bus gets flaky |
| Wires longer than ~50 cm | 100 kHz or lower | 2.2–4.7 kΩ to 3.3 V | Twist SDA and SCL with GND; consider an I2C bus extender (e.g. P82B715) |
| Sensor in another room | Don'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:
# 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
| Symptom | Likely cause | Fix |
|---|---|---|
[] and both lines read 1 | Device unpowered, SDA/SCL swapped, wrong pins in code | Check VCC/GND; swap SDA and SCL; match the GPIOs in I2C(...) |
[] and a line reads 0 | Short to GND, wiring mistake, or a device holding SDA low | Unplug devices one by one; power-cycle to release a hung device |
| Every address from 0x08 to 0x77 found | SDA and SCL shorted together, or no pull-ups at all | Check wiring; add 4.7 kΩ pull-ups to 3.3 V |
| Device appears only sometimes | Long wires, weak pull-ups, 400 kHz too fast | Shorter wires, 100 kHz, 4.7 kΩ pull-ups |
OSError: [Errno 19] ENODEV in the driver | Driver uses a different address than the scan shows | Pass the scanned address to the driver |
OSError: [Errno 116] ETIMEDOUT | Bus stuck or clock stretching too long | Lower the frequency; power-cycle the devices |
| Works alone, fails with several modules | Too many pull-ups in parallel, or address clash | Remove extra pull-up resistors; check addresses |
| 5 V module not found | 5 V pull-ups or a module needing 5 V logic | Use 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
- ESP32 I2C pins and multiple devices (Arduino-side guide).
- I2C vs SPI: which to use.
- MicroPython SSD1306 OLED.
- MicroPython troubleshooting: common errors.
Official reference: MicroPython machine.I2C.




