How to fix MicroPython I2C no data
Problem:
You’ve configured MicroPython’s I2C similar to this (in my case on the ESP8266 but this applies to many MCUs):
i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4))
but you can’t find any devices on the bus:
>>> i2c.scan()
[]
Solution
Likely you forgot to configure the pins as pullups. I2C needs pullups to work, and many MCUs (like the ESP8266) provide support for integrated (weak) pull-ups.
p4 = machine.Pin(4, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
p5 = machine.Pin(5, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
i2c = machine.I2C(-1, p5, p4)
i2c.scan() # [47]
You can also verify this by checking with a multimeter or an oscilloscope: When no communication is going on on the I2C bus, the voltage should be equivalent to the supply voltage of your MCU (usually 3.3V or 5V - 0V indicates a missing pullup or some other error).