Search code examples
buttonesp8266i2cmicropython

Can't read I2C buttons in ESP8266 with MicroPython


I have a Lolin D1 mini ESP8266 and a 0.66" oled screen soldered on top. I have some running micropython (1.12) examples using the screen to show some text, but I would like to use the screen's I2C buttons as well.

According to the documentation, the screen has a default i2c address 0x3C (60) and buttons are 0x31 (49). If I connect to the REPL and run this:

from machine import Pin, I2C
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
i2c.scan()

I get [49, 60] as expected.

Now I would like to read the buttons status, so having a look at Wemos' i2c-button-library, it seems that I have to proceed in the following way:

  • Send i2c command GET_KEY_VALUE (0x04) (1 byte) and get 1 ACK
  • Read i2c response (value from 0x00 to 0x04) (1 byte)

So I do:

i2c.writeto(49, b'4')  # returns 1, so I get 1 ACK, correct.
i2c.readfrom(49, 1)    # returns \x02

But all I get is \x02. Even if I read more bytes, I get \x02\xff\xff\xff\xff\xff\xff\xff\xff\xff. This response does not change when I hold the buttons.

What am I doing wrong?


Solution

  • Problem has been solved. I should have sent b’\x04’ instead of b’4’.