mdraber
Published © LGPL

Practical Guide to PCF8574: Adding Extra Pins for Arduino

Add extra 8 pins to Arduino !

IntermediateProtip2 hours1,044
Practical Guide to PCF8574: Adding Extra Pins for Arduino

Things used in this project

Story

Read more

Schematics

I2C connection of IO expander

Code

Controlling 7segment display using PCF 8574 expander and the pushbutton without interrupts

Arduino
Code without using interrupts
#include "Arduino.h"
#include "PCF8574.h"

PCF8574 pcf8574(0x21);

int digits [10][7]{
  {1,1,1,1,1,1,0}, // digit 0
  {0,1,1,0,0,0,0}, // digit 1
  {1,1,0,1,1,0,1}, // digit 2
  {1,1,1,1,0,0,1}, // digit 3
  {0,1,1,0,0,1,1}, // digit 4
  {1,0,1,1,0,1,1}, // digit 5
  {1,0,1,1,1,1,1}, // digit 6
  {1,1,1,0,0,0,0}, // digit 7
  {1,1,1,1,1,1,1}, // digit 8
  {1,1,1,1,0,1,1}  // digit 9
};

int i=0;
#define SEG_A  P5
#define SEG_B  P4
#define SEG_C  P1
#define SEG_D  P2
#define SEG_E  P3
#define SEG_F  P6
#define SEG_G  P7
#define Button  P0

void display_digit(int d){
  if (digits[d][0]==1) pcf8574.digitalWrite(SEG_A,LOW); else pcf8574.digitalWrite(SEG_A,HIGH); //A
  if (digits[d][1]==1) pcf8574.digitalWrite(SEG_B,LOW); else pcf8574.digitalWrite(SEG_B,HIGH); //B
  if (digits[d][2]==1) pcf8574.digitalWrite(SEG_C,LOW); else pcf8574.digitalWrite(SEG_C,HIGH); //C
  if (digits[d][3]==1) pcf8574.digitalWrite(SEG_D,LOW); else pcf8574.digitalWrite(SEG_D,HIGH); //D
  if (digits[d][4]==1) pcf8574.digitalWrite(SEG_E,LOW); else pcf8574.digitalWrite(SEG_E,HIGH); //E
  if (digits[d][5]==1) pcf8574.digitalWrite(SEG_F,LOW); else pcf8574.digitalWrite(SEG_F,HIGH); //F
  if (digits[d][6]==1) pcf8574.digitalWrite(SEG_G,LOW); else pcf8574.digitalWrite(SEG_G,HIGH); //G
} 


void setup() {
  Serial.begin(9600);
  pcf8574.pinMode(SEG_A, OUTPUT);
  pcf8574.pinMode(SEG_B, OUTPUT);
  pcf8574.pinMode(SEG_C, OUTPUT);
  pcf8574.pinMode(SEG_D, OUTPUT);
  pcf8574.pinMode(SEG_E, OUTPUT);
  pcf8574.pinMode(SEG_F, OUTPUT);
  pcf8574.pinMode(SEG_G, OUTPUT);
  pcf8574.pinMode(Button, INPUT_PULLUP);
  if(pcf8574.begin())
     Serial.println("OK");
     else Serial.println("Failed");
}
void loop() {
   int button_pressed=pcf8574.digitalRead(P0);
   Serial.println(button_pressed);
   if (button_pressed==LOW){
    i=i+1;
    if(i>9) i=0;
   } 
   delay(1000); 
   display_digit(i);
}

Blinking 2 LEDs

Arduino
One led is controlled from Arduino pin and the other one is controlled from Expander pin
#include "Arduino.h"
#include "PCF8574.h"
PCF8574 pcf8574(0x21);

#define LED_PIN_ARDUINO  3
#define LED_PIN_PCF8574  P6


void setup() {
  Serial.begin(9600);
  if(pcf8574.begin())
     Serial.println("OK");
     else Serial.println("Failed");
  pcf8574.pinMode(P2, INPUT_PULLUP);
  pcf8574.pinMode(LED_PIN_PCF8574, OUTPUT);
}
void loop() {
  digitalWrite(LED_PIN_ARDUINO, HIGH);
  pcf8574.digitalWrite(LED_PIN_PCF8574, LOW);  
  delay(1000);
  digitalWrite(LED_PIN_ARDUINO, LOW);
  pcf8574.digitalWrite(LED_PIN_PCF8574, HIGH);
  delay(1000);
}

Controlling 7 segmet display

Arduino
Controlling the display using 7 expander pins. The code is displaying digits from 0 to 9 in 1s intervals
#include "Arduino.h"
#include "PCF8574.h"

PCF8574 pcf8574(0x21);

int digits [10][7]{
  {1,1,1,1,1,1,0}, // digit 0
  {0,1,1,0,0,0,0}, // digit 1
  {1,1,0,1,1,0,1}, // digit 2
  {1,1,1,1,0,0,1}, // digit 3
  {0,1,1,0,0,1,1}, // digit 4
  {1,0,1,1,0,1,1}, // digit 5
  {1,0,1,1,1,1,1}, // digit 6
  {1,1,1,0,0,0,0}, // digit 7
  {1,1,1,1,1,1,1}, // digit 8
  {1,1,1,1,0,1,1}  // digit 9
};


#define SEG_A  P5
#define SEG_B  P4
#define SEG_C  P1
#define SEG_D  P2
#define SEG_E  P3
#define SEG_F  P6
#define SEG_G  P7

void display_digit(int d){
  if (digits[d][0]==1) pcf8574.digitalWrite(SEG_A,LOW); else pcf8574.digitalWrite(SEG_A,HIGH); //A
  if (digits[d][1]==1) pcf8574.digitalWrite(SEG_B,LOW); else pcf8574.digitalWrite(SEG_B,HIGH); //B
  if (digits[d][2]==1) pcf8574.digitalWrite(SEG_C,LOW); else pcf8574.digitalWrite(SEG_C,HIGH); //C
  if (digits[d][3]==1) pcf8574.digitalWrite(SEG_D,LOW); else pcf8574.digitalWrite(SEG_D,HIGH); //D
  if (digits[d][4]==1) pcf8574.digitalWrite(SEG_E,LOW); else pcf8574.digitalWrite(SEG_E,HIGH); //E
  if (digits[d][5]==1) pcf8574.digitalWrite(SEG_F,LOW); else pcf8574.digitalWrite(SEG_F,HIGH); //F
  if (digits[d][6]==1) pcf8574.digitalWrite(SEG_G,LOW); else pcf8574.digitalWrite(SEG_G,HIGH); //G
} 


void setup() {
  Serial.begin(9600);
  if(pcf8574.begin())
     Serial.println("OK");
     else Serial.println("Failed");

  pcf8574.pinMode(SEG_A, OUTPUT);
  pcf8574.pinMode(SEG_B, OUTPUT);
  pcf8574.pinMode(SEG_C, OUTPUT);
  pcf8574.pinMode(SEG_D, OUTPUT);
  pcf8574.pinMode(SEG_E, OUTPUT);
  pcf8574.pinMode(SEG_F, OUTPUT);
  pcf8574.pinMode(SEG_G, OUTPUT);
}
void loop() {
   for (int i=0;i<10;i++) {
   display_digit(i);
   delay(1000);
  }

}

Controlling 7segment display using PCF 8574 expander and the pushbutton witt interrupts

Arduino
Code with interrupts
#include "Arduino.h"
#include "PCF8574.h"

// For arduino uno only pin 1 and 2 are interrupted
#define ARDUINO_UNO_INTERRUPTED_PIN 2

// Function interrupt
void keyPressedOnPCF8574();

PCF8574 pcf8574(0x21, ARDUINO_UNO_INTERRUPTED_PIN, keyPressedOnPCF8574);

int digits [10][7]{
  {1,1,1,1,1,1,0}, // digit 0
  {0,1,1,0,0,0,0}, // digit 1
  {1,1,0,1,1,0,1}, // digit 2
  {1,1,1,1,0,0,1}, // digit 3
  {0,1,1,0,0,1,1}, // digit 4
  {1,0,1,1,0,1,1}, // digit 5
  {1,0,1,1,1,1,1}, // digit 6
  {1,1,1,0,0,0,0}, // digit 7
  {1,1,1,1,1,1,1}, // digit 8
  {1,1,1,1,0,1,1}  // digit 9
};

int i=0;
unsigned long timeElapsed;
#define SEG_A  P5
#define SEG_B  P4
#define SEG_C  P1
#define SEG_D  P2
#define SEG_E  P3
#define SEG_F  P6
#define SEG_G  P7
#define Button  P0

void display_digit(int d){
  if (digits[d][0]==1) pcf8574.digitalWrite(SEG_A,LOW); else pcf8574.digitalWrite(SEG_A,HIGH); //A
  if (digits[d][1]==1) pcf8574.digitalWrite(SEG_B,LOW); else pcf8574.digitalWrite(SEG_B,HIGH); //B
  if (digits[d][2]==1) pcf8574.digitalWrite(SEG_C,LOW); else pcf8574.digitalWrite(SEG_C,HIGH); //C
  if (digits[d][3]==1) pcf8574.digitalWrite(SEG_D,LOW); else pcf8574.digitalWrite(SEG_D,HIGH); //D
  if (digits[d][4]==1) pcf8574.digitalWrite(SEG_E,LOW); else pcf8574.digitalWrite(SEG_E,HIGH); //E
  if (digits[d][5]==1) pcf8574.digitalWrite(SEG_F,LOW); else pcf8574.digitalWrite(SEG_F,HIGH); //F
  if (digits[d][6]==1) pcf8574.digitalWrite(SEG_G,LOW); else pcf8574.digitalWrite(SEG_G,HIGH); //G
} 


void setup() {
  Serial.begin(9600);
  timeElapsed=millis();
  pcf8574.pinMode(SEG_A, OUTPUT);
  pcf8574.pinMode(SEG_B, OUTPUT);
  pcf8574.pinMode(SEG_C, OUTPUT);
  pcf8574.pinMode(SEG_D, OUTPUT);
  pcf8574.pinMode(SEG_E, OUTPUT);
  pcf8574.pinMode(SEG_F, OUTPUT);
  pcf8574.pinMode(SEG_G, OUTPUT);
  pcf8574.pinMode(Button, INPUT_PULLUP);
  if(pcf8574.begin())
     Serial.println("OK");
     else Serial.println("Failed");
}
void loop() {
  display_digit(i);
}

void keyPressedOnPCF8574(){
   if (millis()-timeElapsed>300){
     i=i+1;if(i>9) i=0;
     timeElapsed=millis();
   }
}

Credits

mdraber

mdraber

49 projects • 67 followers

Comments