PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Modules for interfacing to other devices or the outside world
andrew
Site Admin
Posts: 1411
Joined: Sun Aug 05, 2012 4:15 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by andrew » Sun Nov 26, 2017 9:13 am

Thanks for posting the example code. I've taken a look at the library but I can't see any obvious reason why the order would make a difference. I'll set one up on Monday when I'm back in the office to see if I can replicate the issue.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Paul van de Veen
Posts: 8
Joined: Tue Oct 31, 2017 4:32 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by Paul van de Veen » Sun Nov 26, 2017 2:23 pm

Would be great if this issue can be solved.
I too checked the library and neither do I see any explanation why the order makes any difference.

Of course knowing the issue is also the way to get around. But I included this Port Expander in a course for my students.
And it is difficult to explain to them that in an Arduino program ledpinA=1; ledpinB=0; is wrong while ledpinA=0; ledpinB=1; is correct.

Edit: I tested also lines like
Port.pinMode(4, OUTPUT);
Port.pinMode(3, OUTPUT);

Gives the same result.

andrew
Site Admin
Posts: 1411
Joined: Sun Aug 05, 2012 4:15 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by andrew » Mon Nov 27, 2017 11:04 am

I've now been able to test the code which allowed me to understand the issue better. I've fixed the problem and uploaded a new version (V0.2) of the library to the software section of the forum. If you just want to fix your current library rather than downloading the new one just open the HCPCF8574.cpp file in a text editor (don't use Windows Notepad as it messes up the formatting) and change this line:

Code: Select all

_Dir = (_Dir & ~(1 << Pin)) | (~Mode << Pin);
to this:

Code: Select all

_Dir = (_Dir & ~(1 << Pin)) | (!Mode << Pin);
Edit: if you can confirm it's fixed your issue I'll move these posts to the library HCPCF8574 thread what with it being an issue with the library not the module.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Paul van de Veen
Posts: 8
Joined: Tue Oct 31, 2017 4:32 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by Paul van de Veen » Mon Nov 27, 2017 3:54 pm

Hi Andrew,

Many thanks. Now it works as a charm.
I and my friends completely overlooked that Mode is a Boolean, not a byte, so the bit manipulation in the previous .cpp were not allowed.
A small but important difference. (It would be great if the Arduino compiler would have complained..)
My sincere compliments!

Paul van de Veen
Posts: 8
Joined: Tue Oct 31, 2017 4:32 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by Paul van de Veen » Sat Jan 06, 2018 7:35 pm

I attached a 4x3 keypad to this Port Expander. Works nice with the standard i2ckeypad.h library
I also attached an LCD with I2C Backpack SHield. In this way an I2C keypad is achieved.
But.... if I have NO LCD attached, this fails after a couple of keypresses. It seems that the I2C bus is messed up.
I wonder, are there any pullup resistors in this Port Expander?
I think they are required. They are present in this LCD Backpack shield so when it is present, the problem is solved.

Code: Select all

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
#include <i2ckeypad.h>
// Pin 2-3-4-5-6-7-8 connects to P0-P1-P2-P3-P4-P5-P6. P7 and Int Not Connected

#define ROWS 4
#define COLS 3
#define PCF8574_ADDR 0x20 // With A0, A1 and A2 of PCF8574 to ground I2C address is 0x20
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);

void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("I2C Druktoetspad");
  Wire.begin();
  kpd.init();
}

void loop()
{
  char key = kpd.get_key();
  if (key != '\0') {
    lcd.setCursor(0, 1);
    lcd.print(key);
    lcd.print(" pressed!");
  }
}

andrew
Site Admin
Posts: 1411
Joined: Sun Aug 05, 2012 4:15 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by andrew » Mon Jan 08, 2018 9:39 am

Yes the PCF8574 doesn't have built in pullups so you will need something between 4.7K and 10K on the SDA and SCL lines. It's likely that's what's causing your issue.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by RetroBoy » Mon Aug 22, 2022 12:58 pm

Hello,

I have 2 of these linked to control 2 8xRelay Modules ( 16 Relays )- working great!

Q1: Is it possible to link a 3x4 Keypad ( HCPROJ0004 ) onto the PCF8574 and get the same OUTPUT as linking directly to the NANO? ( I need Pins 4, 10, 12 to connect a MAX7219 to display the Keypad Output ).

Q2: If Q1 is not possible, any suggestions for the MAX7219 connections.

Regards S.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by RetroBoy » Mon Aug 22, 2022 1:30 pm

Hi Andrew,

Please disregard my previous post as I have read Paul van de Veen Post, so I have downloaded the I2CKeypad Library and all is working OK ... ( Age issue again ).

I do have another question regarding the PCF8574:

Q1: Will I need Pullup Resistors ( 5.1K ) for the 8xRelay Boards and Keypad ?

Regards S.

andrew
Site Admin
Posts: 1411
Joined: Sun Aug 05, 2012 4:15 pm

Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)

Post by andrew » Mon Aug 22, 2022 2:26 pm

Q1: Will I need Pullup Resistors ( 5.1K ) for the 8xRelay Boards and Keypad ?
Assuming you mean when interfacing the PCF8574 to them, then you shouldn't need pullups...

For interfacing to the relay the PCF8574 has push pull outputs so you can just drive them high or low depending on the relay state you require,.

For the keypad I assume you'll be using a combination of inputs and outputs. For the inputs you can just set them high (default state) and they can then just be pulled low. This of course assumes whatever library you're using supports the bidirectional feature of the PCF8574.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Interface”