dhylands / python_lcd

Python based library for talking to character based LCDs.
MIT License
310 stars 119 forks source link

Adding I2C Micropython code #20

Open paulohumberto42 opened 6 years ago

paulohumberto42 commented 6 years ago

Hello! I added some code to use I2C over a ESP32 using micropython. Instead of using smbus I am using Machine.I2C

remcoder commented 6 years ago

Hey @paulohumberto42, I just want to chime in and say I've run your test on a Huzzah32 and it works perfectly.

I happen to have an lcd backpack based on a PCF8574A and the only thing I noticed that its address is in between 0x30 and 0x3F, so it might be helpful to add that to the comment inline.

In terms of code, I noticed that DEFAULT_I2C_ADDR was defined twice: both in the actual driver and once the test. But it is never used directly in the driver code so it can safely be removed there.

Also, to be consistent, the filenames should probably be changed to esp32_i2c_lcd*

In any case, I'd like to thank you b/c now I can happily use my esp32 with that lcd ;-)

paulohumberto42 commented 6 years ago

Hello @remcoder ! I'm glad it helped you, and thanks for the feedbacks! I will update the code ASAP ;)

aseelye commented 3 years ago

Thank you, @paulohumberto42 ! FWIW this code is working in the mainline 1.16 release (esp32-20210623-v1.16.bin from micropython.org site).

One note, it looks like things have changed with how micropython calls the I2C code, and your sda/scl pins are backwards. Referncing https://docs.micropython.org/en/latest/esp32/quickref.html#hardware-i2c-bus, sda should be 19, scl is pin 18. You can initiate the bus with

i2c = machine.I2C(0, freq=100000)

or

sda = machine.Pin(19)
scl = machine.Pin(18)
i2c = machine.I2C(0, sda=sda, scl=scl, freq=100000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 4, 20)

if you want to specify the pins. Obviously alternate pins would require using the secondary I2C bus, or using machine.SofI2C. Either way, thank you for your PR, it sped my day up considerably.