Reply to topic  [ 16 posts ]  Go to page Previous  1, 2
PCF8574 I2C to 8-bit digital port expander (HCMODU0120) 
Author Message
Site Admin

Joined: Sun Aug 05, 2012 4:15 pm
Posts: 724
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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.


Sun Nov 26, 2017 9:13 am
Profile

Joined: Tue Oct 31, 2017 4:32 pm
Posts: 8
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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.


Sun Nov 26, 2017 2:23 pm
Profile
Site Admin

Joined: Sun Aug 05, 2012 4:15 pm
Posts: 724
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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:
_Dir = (_Dir & ~(1 << Pin)) | (~Mode << Pin);


to this:

Code:
_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.


Mon Nov 27, 2017 11:04 am
Profile

Joined: Tue Oct 31, 2017 4:32 pm
Posts: 8
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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!


Mon Nov 27, 2017 3:54 pm
Profile

Joined: Tue Oct 31, 2017 4:32 pm
Posts: 8
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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:
#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!");
  }
}


Sat Jan 06, 2018 7:35 pm
Profile
Site Admin

Joined: Sun Aug 05, 2012 4:15 pm
Posts: 724
Post Re: PCF8574 I2C to 8-bit digital port expander (HCMODU0120)
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.


Mon Jan 08, 2018 9:39 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page Previous  1, 2

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Thank you to phpBB.com & phpBB.ro.
Design created by Florea Cosmin Ionut.
© 2011 All contents of this site are © Hobby Components LTD