IO expander

From ElectroDragon
Jump to: navigation, search
  • PCF8574 / PCF8575 - I2C - common used for 1602
  • MCP23008 - I2C - 8-Bit I/O Expander with Serial Interface, - arduino library
  • MCP23017 - I2C - 16-Bit I/O Expander with Serial Interface,


PCF8574 simple io write code

  • Default IIC address 0x20
#include "PCF8574.h"
#include <Wire.h>
 
// adjust addresses if needed
PCF8574 PCF_38(0x20);  // add led to P0 (pin 4) (used as output)
 
void setup()
{
//your setup code here
}
 
void loop()
{
    //turn P0 (pin 4) of the I/O expander on and off once a second
    PCF_38.write(0, 1);
    delay(1000);
    PCF_38.write(0, 0);
    delay(1000);
}

PCF8575 Demo Code

Use Wire.write(B00000000); to write value to the pins.

#include <Wire.h>
 
byte address = 0x20;   // address of PCF8575 with A0-A2 connected to GND "B01000000"
byte input; // variable to receive the two bytes
byte c; // first of the two bytes to read
byte d; // second of the two bytes to read
int led = 13;
 
void setup()
{
   Wire.begin();       // join i2c bus 
   Serial.begin(9600); // used to Debug , Thanks PaulS
   pinMode(led, OUTPUT);
}
 
void loop(){
 
 
    input = Wire.requestFrom(0x20,2);
 
      c = Wire.read();
      d = Wire.read();
      Wire.endTransmission();
 
      if(bitRead(d,6) == 0) {
     digitalWrite(led, LOW); 
     delay(1000);
       }
       else {
         digitalWrite(13, HIGH);
       }
    Serial.println(c, BIN);  
    delay(100);
    Serial.println(d,BIN); 
 
    }      // send the data

Demo code