Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
arikb
Posts: 5
Joined: Fri Oct 09, 2015 11:45 pm

Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

Post by arikb » Fri Jul 08, 2016 9:16 pm

I've been trying to puzzle this thing for a while, and I'm probably doing something very wrong here.

I've connected this "LCM1602" LCD that has an I2C interface based on PCF8574T. I use GP23 and GP24 for it, which on my WiPy are SCL and SDA respectively.

First thing I did was initialise I2C and scan for the board, which returned the address successfully (0x27). So far so good.

The next thing I did was utilise slothy's lcdi2c library at https://github.com/slothyrulez/lcdi2c (announced on the forum at http://forum.micropython.org/viewtopic. ... 397&p=8634 ) - which worked fine from the REPL. The following code, however:

Code: Select all

# main.py -- put your code here!

from lcd_i2c import LCDI2C
from machine import Pin
from time import sleep_ms

disp = LCDI2C()
disp.configure()

# switch
user_switch = Pin.board.GP17
user_switch.init(mode=Pin.IN, pull=Pin.PULL_UP)

marq = [c for c in b'This is a test  ']
    
while user_switch():
    disp.setCursor(0,0)
    for c in marq:
        disp.write(c)
    marq = marq[-1:] + marq[:-1]
    sleep_ms(250)
generates the following traceback:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 19, in <module>
  File "hd44780_lcd.py", line 153, in write
  File "lcd_i2c.py", line 98, in send
  File "lcd_i2c.py", line 124, in _write4bits
  File "lcd_i2c.py", line 106, in _pulse_enable
OSError: the requested operation failed
The offending line is:

Code: Select all

self.i2c.writeto(self.addr, data & ~self.en[0])
So it's an I2C write. I should mention that this does not happen through initialisation - the code runs through between zero and 5 iterations before it fails.

My uname() result is (sysname='WiPy', nodename='WiPy', release='1.2.0', version='v1.8.1-181-ga4c8a1f on 2016-07-08', machine='WiPy with CC3200') - I updated to today's build. I use the expansion board.

I did a bit of digging in the lcd_itc code, and through the PCF8574T datasheet - but with my limited experience I can't see what's wrong.

From a hardware perspective, I run the system from USB using the WiPy expansion board, so I have the two voltage buses - 5v and 3.3v. The LCD requires 5v, and since WiPy comes with strict warnings about not tolerating anything above 3.8v I had to build bidirectional level shifters for the SDA and SCL signals. I used a simple design with a BS170 MOSFET and two 10Kohm pull-up resistors. It seems to work well as far as I'm able to test. I use GP23 and GP24 on the board.

User avatar
arikb
Posts: 5
Joined: Fri Oct 09, 2015 11:45 pm

Re: Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

Post by arikb » Sun Jul 10, 2016 2:49 am

Upon further investigation with a logic probe, it seems that when the error occurs - the LCD (or rather the PCF8574T) keeps the data (SDA) signal low. Forever. That is until I power-cycle it. There's something it really doesn't like, it seems. I tried sniffing the I2C protocol and got some data, but I'll have to dig deeper into I2C to figure out what it means.

Any suggestions would be most helpful.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

Post by pythoncoder » Sun Jul 10, 2016 8:08 am

Unless someone here happens to have the same hardware it's hard to offer help. Your code looks fine and the fact that it runs through several iterations rules out most 'newbie' errors. In all probability you're looking for a bug in the lcd_i2c library or an incompatibility of the library with the WiPy. Problems like this need some work with the actual hardware. This means examining the I2C lines with a logic analyser or 'scope and figuring out where the signal is failing to conform with the datasheet requirements.

Before getting into serious debugging it's worth noting that I2C connections should be kept short, and you need a short, direct, ground wire between the WiPy and the LCD.
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

Post by deshipu » Sun Jul 10, 2016 8:14 am

One thing that you could try -- add pullup resistors to the I2C lines. The fact that your problems are rather random suggests it might be something related to noise, and the build-in pullups are not really that good for I2C.

User avatar
arikb
Posts: 5
Joined: Fri Oct 09, 2015 11:45 pm

Re: Newbie question: HD44780 LCD, PCF8574T mux, I2C, WiPy

Post by arikb » Sun Jul 10, 2016 6:01 pm

@pythoncoder - I do have a protocol sniffer but I don't understand I2C enough to grok its output. It's a Gabatronics Xprotolab, so I can't copy and paste here. I will try to shorten all of the wires and pay close attention to the connections. The LCD and the WiPi do share a common ground but I've been feeding the LCD through a breadboard I've been using to build the level shifter, so I'll try to make everything a bit shorter and see.

@deshipu - I do have 10K pull-up resistors on both the 5v and 3.3v sides of the level shifter, and both devices use CMOS open collector so I figured it should be okay. I'll experiment with a smaller pull-up as well.

Post Reply