Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
Discover an easy way to add more GPIO pins to your Arduino with the PCF8574 in this tutorial. We'll explore how it can be a handy alternative to other methods like shift registers, Max7219, and multiplexing. The video shows you how to control a single LED and a 7-segment display using the PCF8574, making it suitable for various projects. Plus, learn how to use expander pins for input and make the most of the interrupt pin. This tutorial is perfect for both beginners and those with some experience. Watch, learn, and level up your Arduino projects with these practical tips. Hope you find it helpful!
Link to the tutorial:
Controlling 7segment display using PCF 8574 expander and the pushbutton without interrupts
ArduinoCode 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
ArduinoOne 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
ArduinoControlling 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
ArduinoCode 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();
}
}
Comments