Go Down

Topic: i2c port expander PCF8574 as input (Read 12171 times) previous topic - next topic


To only get one output each time you change the button state then change the lines:-

attachInterrupt(5, expanderInterrupt, LOW);
attachInterrupt(5, expanderInterrupt, CHANGE);


The pullup resistor was ok i think. In doubt check datasheet. :)

Fig.13  Application of multiple PCF8574s with interrupt.



Yei! I have double checked every connection again and I have ended up with a pair of codes in output what I press my button. I was able to idendify this to change the data on my lcd screen. I have still to semplify my code but more or less is working now. I will post it as soon as possible. The code of Yot was my key in this work, thanks again! Thanks to everyone :)

I am programming this system to sobstitute the lcd clock of my car, to have an enhanced clock and the possibility to switch visual to see the temperature of many electronics I have installed (via i2c) into.

Federico - Sideralis
Arduino &C: http://www.sideralis.org
Foto: http://blackman.amicofigo.com


Can you please post the working code


This is my code, it's working but perhaps not perfect :) Keep in mind that is my code there is also the part of code that is needed by my rtc.

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WProgram.h>
#include <DS1307.h>

int rtc[7];
byte data[56];
char orario[16]; //non cambiare 16 perchè per qualche motivo non va piu' niente dopo
char dataOdierna[16];
int anno;
int changeScreen = 0;
int k = 0;
int primaAccensione = 0;

#define expander 0x20  // Address with three address pins grounded (B0100000).
                    // Note that the R/W bit is not part of this address.
volatile byte count = 0;

uint8_t bell[8]  = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
uint8_t note[8]  = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
uint8_t duck[8]  = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
uint8_t retarrow[8] = {      0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
LiquidCrystal_I2C lcd(expander,8,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

void setup()
 attachInterrupt(0, expanderInterrupt, CHANGE);

 lcd.init();        // initialize the lcd
 lcd.noBacklight(); // accende e spegne l'uscita sul pin 12.
                    // lcd.backlight();
                    // sink current +5V------+LED-------330Rresistor------P8574pin
 lcd.createChar(0, bell);
 lcd.createChar(1, note);
 lcd.createChar(2, clock);
 lcd.createChar(3, heart);
 lcd.createChar(4, duck);
 lcd.createChar(5, check);
 lcd.createChar(6, cross);
 lcd.createChar(7, retarrow);
 lcd.print(" System");
 lcd.setCursor(0, 1);
 lcd.print(" Ready.");
 for(int i=0; i<9; i++)  {
 for(int i=0; i<56; i++)  {


void loop()

 /*for(int i=0; i<7; i++)
  Serial.print(" ");

 //rtc[0] --> secondi
 //Tramite questo IF faccio lampeggiare il : per i secondi
 if (rtc[0]%2) {
   sprintf(orario,"%02d:%02d",rtc[2],rtc[1]); //%02d converte 9 in 09 e 32 in 32
 else {
   sprintf(orario,"%02d %02d",rtc[2],rtc[1]);
 if (changeScreen==0) {
   lcd.print(2, BYTE);
 else if (changeScreen==1) {
   lcd.print(1, BYTE);
   lcd.print(4, BYTE);
   lcd.print("hcca XX");
 if (count == 1) {
   count = 0;
   attachInterrupt(0, expanderInterrupt, LOW);
   if (primaAccensione==0) {changeScreen=0; primaAccensione++;}
   else if (k==1) { changeScreen=!changeScreen; lcd.clear(); }
   else if (k>1) { k=0; }

// display all keycodes
void displayKeyCodes(void) {
 uint8_t i = 0;
 while (1) {
   lcd.print("Codes 0x"); lcd.print(i, HEX);
   lcd.print("-0x"); lcd.print(i+8, HEX);
   lcd.setCursor(0, 1);
   for (int j=0; j<8; j++) {
     lcd.print(i+j, BYTE);

// Funzioni per interrupt da arduino
void expanderInterrupt() {    
 count = 1;

void expanderWrite(byte _data ) {

byte expanderRead() {
 byte _data;
 Wire.requestFrom(expander, 1);
 if(Wire.available()) {
   _data = Wire.receive();
 return _data;

Federico - Sideralis
Arduino &C: http://www.sideralis.org
Foto: http://blackman.amicofigo.com

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131