PCF8574 Expander with 4 Inputs / 4 Outputs

PCF8574 Expander with 4 Inputs / 4 Outputs © GPL3+

How to use a PCF8574 as a port expander (4I/4O) polling of the buttons without interrupt.

  • 73 views
  • 1 comment
  • 2 respects

Components and supplies

About this project

Making an I/O port expander using I2C for future use together with an ATTINY85.

Finding the optimal polling timing for the inputs (push buttons).

Code

Untitled fileArduino
// DEGRYSE RUBEN 2019/11
//I2C device found at address 0x3E  ! PCF 8574A

#define DEBOUNCE 150  // button debouncer on PCF8574, checked with scope 120 is minimum for maximum reliable response
byte buttons[] = {0, 1, 2, 3}; // first 4 inputs on the PCF8574 / PIN 4 --> OUTPUT LEDS
#define NUMBUTTONS sizeof(buttons) // return size of array (see above)
unsigned long previousMillis = 0;

#include "Arduino.h"
#include <Wire.h>
#include <PCF8574.h>
#define expadr 0x3E		//define address of PCF8574 expander

PCF8574 expander;		//name of object expander

//The setup function is called once at startup of the sketch
void setup()
{
	//Serial.begin(115200);

	// Start the I2C interface
	Wire.begin();
	expander.begin(expadr);

	for (int i=0; buttons[i]<4;i++){
		expander.pinMode(i,INPUT);
	}

	for (int i=4; buttons[i]<8;i++){
		expander.pinMode(i,OUTPUT);
	}
	expander.write(0xFF);


}

// The loop function is called in an endless loop
void loop()
{

	checkIO();

}

// Sub is called debounce and check inputs
void checkIO ()
{

	byte i;

	unsigned long currentMillis = millis();

	if (currentMillis - previousMillis < DEBOUNCE){
		return;
	}

	for(i=0;i<4;i++){
		if(expander.digitalRead(i)==LOW){
			expander.toggle(i+4);
		}
	}

	previousMillis = currentMillis;



}

Schematics

img_20191102_151143_oTh8ryfqyl.jpg
Img 20191102 151143 oth8ryfqyl

Comments

Similar projects you might like

Arduino Inputs Outputs on PC

Project tutorial by moty

  • 6,597 views
  • 3 comments
  • 9 respects

Maximize Inputs

Project in progress by amosp33

  • 1,168 views
  • 0 comments
  • 6 respects

NANO I2C IO Expander

Project tutorial by Joel Soranzo

  • 3,729 views
  • 2 comments
  • 8 respects

Master Slave I2C Connection

by PIYUSH_K_SINGH

  • 10,165 views
  • 0 comments
  • 10 respects
Add projectSign up / Login