guild icon
Toit
#PCF8574 and HD44780
Thread channel in help
exklibur
exklibur 06/09/2024 04:38 PM
I'm trying to use an LCD2004 with and PCF8574. Any help would be appreciated. I've thought of using VirtualPins is this the right direction ?

import hd44780 show *
import gpio

import i2c
import pcf8574 show *

main:
sda := gpio.Pin 2
scl := gpio.Pin 15
bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000

device := bus.device 0x27
pcf := PCF8574 device

ret := pcf.read // Reads status of all port and returns list.
print "PCF read $ret"

EN-PIN ::= gpio.VirtualPin ::
pcf.set --pin=1
RS-PIN ::= gpio.VirtualPin ::
pcf.set --pin=0
D4-PIN ::= gpio.VirtualPin ::
pcf.set --pin=4
D5-PIN ::= gpio.VirtualPin ::
pcf.set --pin=5
D6-PIN ::= gpio.VirtualPin ::
pcf.set --pin=6
D7-PIN ::= gpio.VirtualPin ::
pcf.set --pin=7

display := Hd44780
--type = Hd44780.LCD-20x4
--en = EN-PIN
--rs = RS-PIN
--d4 = D4-PIN
--d5 = D5-PIN
--d6 = D6-PIN
--d7 = D7-PIN

// Write text on third line, 6th column.
display.write --row=2 --column=5
Hd44780.translate-to-rom-a-00 "β†’toit←" // Special characters requires translate_to_rom_a_00.
sleep --ms=2000
(edited)
floitsch
floitsch 06/09/2024 04:43 PM
I haven't played with the VirtualPin yet, but I think your approach should work.(edited)
floitsch
floitsch 06/09/2024 04:43 PM
You probably need to change the lambdas though
floitsch
floitsch 06/09/2024 04:44 PM
They don't use the value parameter that is given to them. (At least I assume they get one)
floitsch
floitsch 06/09/2024 04:46 PM
Probably something like:
EN-PIN gpio.VirtualPin :: if it: pcf.set --pin=1 else: pcf.clear --pin=1
exklibur
exklibur 06/09/2024 05:04 PM
That makes sense. I added that, but still no characters showing. I need to check the pinout maybe.
floitsch
floitsch 06/09/2024 05:07 PM
I hope it's the pin out. The code looks otherwise correct
exklibur
exklibur 06/14/2024 05:38 PM
There seems to be an issue passing the pcf object to the class Hd44780. When i trigger a a virtual pin outside it works but the methods inside the class do not set or clear the pins.
floitsch
floitsch 06/14/2024 05:39 PM
Ok. I will try to have a look at the code a bit later
exklibur
exklibur 06/14/2024 06:05 PM
That d be great. I'm operating the PCF8574 with 3.3V and watching the pins output with a logic analyzer. I see the lambdas are being called, but the call doesn't reach the pcf. I looked at the code of the Hd44780 driver, but it looks good to me. Not sure what could be the problem.
floitsch
floitsch 06/14/2024 06:15 PM
Can you show the way you do the virtual pins?
exklibur
exklibur 06/14/2024 08:23 PM
mport .hd44780 import gpio import i2c import pcf8574 show * main: sda := gpio.Pin 21 scl := gpio.Pin 22 bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000 device := bus.device 0x27 pcf ::= PCF8574 device devices := bus.scan devices.do: print "0x$(%02x it)" print pcf.read test-pin := gpio.VirtualPin :: if it: pcf.set --pin=2 else: pcf.clear --pin=2 test-pin.set 1 sleep --ms=500 EN-PIN := gpio.VirtualPin :: print "enable $it" if it: pcf.clear --pin=2 else: pcf.set --pin=2 sleep --ms=200 RS-PIN ::= gpio.VirtualPin :: print "register $it" if it: pcf.set --pin=0 else: pcf.clear --pin=0 D4-PIN ::= gpio.VirtualPin :: if it: pcf.set --pin=4 else: pcf.clear --pin=4 D5-PIN ::= gpio.VirtualPin :: if it: pcf.set --pin=5 else: pcf.clear --pin=5 D6-PIN ::= gpio.VirtualPin :: if it: pcf.set --pin=6 else: pcf.clear --pin=6 D7-PIN ::= gpio.VirtualPin :: if it: pcf.set --pin=7 else: pcf.clear --pin=7 display := Hd44780 --type = Hd44780.LCD-20x4 --en = EN-PIN --rs = RS-PIN --d4 = D4-PIN --d5 = D5-PIN --d6 = D6-PIN --d7 = D7-PIN // Write text on third line, 6th column. display.write --row=2 --column=1 "like a toiger" sleep --ms=2000
exklibur
exklibur 06/14/2024 08:25 PM
that is my code right now
floitsch
floitsch 06/14/2024 08:25 PM
That's on me: you need to check if it == 1:
floitsch
floitsch 06/14/2024 08:29 PM
You can also make your code simpler:
pcf-pin pcf/PCF8574 n/int -> Lambda: return :: if it == 1: pcf.set n else: PCF.clear n

and then in your code:
D4-PIN ::= pcf-pin pcf 4 D5-PIN :: = ...
exklibur
exklibur 06/14/2024 08:33 PM
ok, but i dont think thats it. test-pin works, but the hd44780 driver doesnt seem to be able to trigger any state. i will try though with the other lambda form
floitsch
floitsch 06/14/2024 08:37 PM
Can you add a print to the lambda to see if it is called by the hd44780?
floitsch
floitsch 06/14/2024 08:38 PM
Oh. The print is already there
exklibur
exklibur 06/14/2024 08:39 PM
scratch that, it seems to work now
exklibur
exklibur 06/14/2024 08:39 PM
maybe
floitsch
floitsch 06/14/2024 08:39 PM
The constructor calls the pin immediately.
exklibur
exklibur 06/14/2024 08:40 PM
maybe it had something to do with the lambda
exklibur
exklibur 06/14/2024 08:40 PM
it seems to work now, i will try with all pins and share results
floitsch
floitsch 06/14/2024 08:40 PM
It shouldn't. But as long as it works now :πŸ™‚:
exklibur
exklibur 06/14/2024 09:12 PM
Code below works. I'd probably add a level shifter since the LCD I'm working with works with 5V, but surprinsingly it works. I had to invert the trigger in my case also.
import gpio import i2c import hd44780 show * import pcf8574 show * pcf-pin pcf/PCF8574 n/int -> Lambda: return :: if it == 1: pcf.clear --pin=n else: pcf.set --pin=n main: sda := gpio.Pin 21 scl := gpio.Pin 22 bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000 device := bus.device 0x27 pcf ::= PCF8574 device EN-PIN ::= gpio.VirtualPin (pcf-pin pcf 2) RS-PIN ::= gpio.VirtualPin (pcf-pin pcf 0) D4-PIN ::= gpio.VirtualPin (pcf-pin pcf 4) D5-PIN ::= gpio.VirtualPin (pcf-pin pcf 5) D6-PIN ::= gpio.VirtualPin (pcf-pin pcf 6) D7-PIN ::= gpio.VirtualPin (pcf-pin pcf 7) display := Hd44780 --type = Hd44780.LCD-20x4 --en = EN-PIN --rs = RS-PIN --d4 = D4-PIN --d5 = D5-PIN --d6 = D6-PIN --d7 = D7-PIN display.write --row=0 --column=1 "Test line" sleep --ms=2000
(edited)
floitsch
floitsch 06/14/2024 09:14 PM
Often 3.3V is enough to count as 1 for 5V devices.
floitsch
floitsch 06/14/2024 09:15 PM
You could create the virtual pin inside the pcf-pin function.
That's what I intended to do...
exklibur
exklibur 06/14/2024 09:16 PM
Yes, that is a lot of duplicate code. I'll refactor this later. Thank you for the help @floitsch
πŸ‘1
floitsch
floitsch 06/14/2024 09:17 PM
Asked chatgpt: looks like .7*VCC is often accepted as 1. So 3.3V is borderline.
exklibur
exklibur 06/14/2024 09:19 PM
Good to know. I'm worrying also because I just measured the pins on the ESP and I get 5V there. That could potentially damage the ESP... maybe
exklibur
exklibur 06/14/2024 09:20 PM
The PCF module has pull ups to 5V
floitsch
floitsch 06/14/2024 09:20 PM
Strange.
floitsch
floitsch 06/14/2024 09:21 PM
I have read conflicting statements of whether the esp32 supports 5v on its pins.
exklibur
exklibur 06/14/2024 09:22 PM
It work right now, not sure for how long though
34 messages in total