By Kevin McAleer, 8 Minutes
One of the most useful tools in your I2C toolkit is the ability to scan the bus and discover what devices are connected. This lesson teaches you how scanning works, what those hexadecimal addresses mean, and how to use scanning to troubleshoot projects.
Scanning the I2C bus helps you:
Think of it like: Network scanning on a computer network, but for electronics. You’re asking “who’s out there?” and devices respond with their address.
The scanning process is simple:
MicroPython makes this incredibly easy with the scan() method:
from machine import I2C, Pin
# Initialize I2C
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
# Scan for devices
devices = i2c.scan()
if devices:
print(f"Found {len(devices)} device(s):")
for device in devices:
print(f" - Decimal address: {device}")
print(f" - Hex address: 0x{device:02X}")
else:
print("No I2C devices found")
Example output with BME280 connected:
Found 1 device(s):
- Decimal address: 118
- Hex address: 0x76
You’ll notice addresses appear in two formats:
Hexadecimal (base-16) is used in electronics because:
The “0x” prefix means “this number is in hexadecimal”:
| Decimal | Hexadecimal | Binary |
|---|---|---|
| 0 | 0x00 | 0000 0000 |
| 16 | 0x10 | 0001 0000 |
| 118 | 0x76 | 0111 0110 |
| 119 | 0x77 | 0111 0111 |
| 127 | 0x7F | 0111 1111 |
Note: I2C uses 7-bit addresses, so the maximum is 0x7F (127 in decimal). There are technically 128 possible addresses, but some are reserved.
Here are addresses you’ll frequently encounter:
| Device | Address | Hex |
|---|---|---|
| BME280 | 118 or 119 | 0x76 or 0x77 |
| SSD1306 OLED | 60 or 61 | 0x3C or 0x3D |
| MPU6050 | 104 or 105 | 0x68 or 0x69 |
| DS3231 RTC | 104 | 0x68 |
| PCF8574 Port Expander | 32-39 | 0x20-0x27 |
| ADS1115 ADC | 72-75 | 0x48-0x4B |
Let’s create a more informative I2C scanner that shows the full address range:
from machine import I2C, Pin
def scan_i2c():
"""Scan I2C bus and display results in a table"""
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
print("\nScanning I2C bus...")
print("=" * 40)
devices = i2c.scan()
if not devices:
print("No devices found!")
return
print(f"Found {len(devices)} device(s):\n")
print("Address | Decimal | Binary")
print("-" * 40)
for device in devices:
binary = format(device, '08b')
print(f"0x{device:02X} | {device:3d} | {binary}")
print("=" * 40)
# Run the scanner
scan_i2c()
Example output:
Scanning I2C bus...
========================================
Found 2 device(s):
Address | Decimal | Binary
----------------------------------------
0x3C | 60 | 00111100
0x76 | 118 | 01110110
========================================
This shows you have an OLED display (0x3C) and a BME280 sensor (0x76) connected.
Not all addresses can be used by devices. Some are reserved by the I2C specification:
| Address Range | Hex | Purpose |
|---|---|---|
| 0x00-0x07 | 0x00-0x07 | Reserved (bus control) |
| 0x78-0x7F | 0x78-0x7F | Reserved (10-bit addressing) |
Regular devices use addresses from 0x08 to 0x77 (8 to 119 decimal).
Let’s build a scanner that recognizes common devices:
from machine import I2C, Pin
# Database of common I2C devices
KNOWN_DEVICES = {
0x3C: "SSD1306 OLED Display (128x64)",
0x3D: "SSD1306 OLED Display (128x32)",
0x68: "MPU6050 IMU / DS3231 RTC",
0x69: "MPU6050 IMU (ALT address)",
0x76: "BME280 / BMP280 Sensor",
0x77: "BME280 / BMP280 Sensor (ALT)",
0x48: "ADS1115 ADC",
0x20: "PCF8574 Port Expander",
}
def smart_scan():
"""Scan and identify devices"""
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
print("\n🔍 Smart I2C Scanner")
print("=" * 50)
devices = i2c.scan()
if not devices:
print("❌ No devices found")
print("\nTroubleshooting tips:")
print(" - Check SDA and SCL connections")
print(" - Verify power (VCC and GND)")
print(" - Ensure pull-up resistors present")
return
print(f"✅ Found {len(devices)} device(s):\n")
for device in devices:
hex_addr = f"0x{device:02X}"
device_name = KNOWN_DEVICES.get(device, "Unknown device")
print(f"📍 Address: {hex_addr} ({device})")
print(f" Likely: {device_name}")
print()
print("=" * 50)
# Run the smart scanner
smart_scan()
Example output:
🔍 Smart I2C Scanner
==================================================
✅ Found 1 device(s):
📍 Address: 0x76 (118)
Likely: BME280 / BMP280 Sensor
==================================================
Experiment with I2C scanning:
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
devices = i2c.scan()
print(f"BME280 address: 0x{devices[0]:02X}")
Temporarily disconnect SDA - Run the scan. You should see no devices
Reconnect and scan again - Device should reappear
Scan at different speeds - Does it affect detection?
# Try slower speed
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
print(f"100kHz scan: {i2c.scan()}")
# Try faster speed
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
print(f"400kHz scan: {i2c.scan()}")
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
print("Full address scan (0x00 to 0x7F):")
for addr in range(0x00, 0x80):
if addr in i2c.scan():
print(f"0x{addr:02X}: DEVICE FOUND")
else:
print(f"0x{addr:02X}: empty")
What happens if two devices have the same address? They conflict and communication fails.
Many devices let you change their address:
Hardware jumpers - Solder pads or physical jumpers
Software configuration - Some devices allow address changes via commands
If your scanner shows no devices but you know hardware is connected:
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
# Try to read from suspected address
address = 0x76
try:
i2c.readfrom(address, 1)
print(f"Device at 0x{address:02X} is responding")
except OSError:
print(f"No device or error at 0x{address:02X}")
Before running your main program, verify devices are present:
from machine import I2C, Pin
def check_devices(expected_addresses):
"""Verify expected devices are present"""
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
found = i2c.scan()
for addr in expected_addresses:
if addr in found:
print(f"✓ Found device at 0x{addr:02X}")
else:
print(f"✗ Missing device at 0x{addr:02X}")
return False
return True
# Check for BME280 before running main code
if check_devices([0x76]):
print("All devices present - starting main program")
else:
print("ERROR: Check your wiring!")
Let your code adapt to what’s actually connected:
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
devices = i2c.scan()
# Check which BME280 address is present
if 0x76 in devices:
BME280_ADDR = 0x76
print("Using BME280 at 0x76")
elif 0x77 in devices:
BME280_ADDR = 0x77
print("Using BME280 at 0x77")
else:
print("No BME280 found!")
BME280_ADDR = None
Problem: i2c.scan() returns empty list []
Solutions:
Problem: Device appears at wrong address
This is normal if:
Solution: Note the actual address and use it in your code
Problem: Unexpected devices appear
Possible causes:
Solution: Disconnect devices one at a time to identify each address
In this lesson, you mastered:
i2c.scan() to detect devices on the busYou now have powerful diagnostic tools to work with any I2C device. Scanning is your first step in every I2C project - it confirms hardware is working before you write complex code.
Your BME280 is connected, verified, and you know its address. Now it’s time to actually read data from it! In the next lesson, we’ll communicate with the sensor, read temperature, humidity, and pressure values, and display them in real-time.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments