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

Modules for interfacing to other devices or the outside world
andrew
Site Admin
Posts: 826
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: 826
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: 826
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.

Post Reply