You are here

Error message

Deprecated function: Function create_function() is deprecated in GeSHi->_optimize_regexp_list_tokens_to_string() (line 4698 of /var/www/diy-scib/sites/all/libraries/geshi/geshi.php).

PCA9534 IO expander

Sensor Type: 
other
Connection: 
I2C
Features: 
8 bit I/O expansion, interrupt on state change, configurable bus address

The Texas Instruments PCA9534 IO expander IC allows you to configure 8 digital pins as inputs or outputs. Each pin is individually configurable, and can trigger an external interrupt pin on any state change. The chip communicates via I2C, making it quite easy to interface.

The chip also has 3 additional pins used for setting the bus address. Simply ground all three pins to utilize the base address, or tie one or more pins high via a resistor to set a different address. This means you could in theory chain up to 8 devices on a single I2C bus (64 IO pins).

Reading individual bits simply requires the application of a bit mask. To remind you, here is a conversion table for decimal, hex and binary. Here is an example, and I will put together a collection of examples and upload them to github soon.

 

/* PCA9534 read demo
    This sketch sets all pins as input and reads
    the data, then prints to the serial monitor.
    Written by Emery Premeaux
    Copyright: Public Domain, but attributing would be welcome
    <a href="http://www.DIY-SciB.org
">http://www.DIY-SciB.org
</a>    */
 
 
#include <Wire.h>
#define IOE_ID 0x20   // address when all address pins are low
uint8_t data;
 
                     // IOE as in Input/Output Expander
void setup()
{
  // Set the PCA9534 pins to inputs
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(IOE_ID);
  Wire.write(0x03);            // control register
  Wire.write(0xff);            // all pins input
  Wire.endTransmission();
}
 
void loop()
{
  // put your main code here, to run repeatedly:
  Wire.beginTransmission(IOE_ID);
  Wire.write(0x00);                // go to the input port register
  Wire.endTransmission();
  Wire.requestFrom(IOE_ID, 1);     // start read mode
  if(Wire.available()){
      data = Wire.read();  // fetch one byte
      Wire.endTransmission();
      Serial.print(data, HEX);
      Serial.print(", ");
      Serial.print(data, BIN);
      Serial.print(", ");
      masking();
  } else {
    Serial.println("No connection?");
  }
  delay(500);
}
 
void masking(){
  if ((data & 0x08 ? false : true) == true){  // Bit 4
    Serial.print("N");
  }
  if ((data & 0x20 ? false : true) == true){  // Bit 6
    Serial.print("NE");
  }
  if ((data & 0x04 ? false : true) == true){  // Bit 3
    Serial.print("NW");
  }
  if ((data & 0x40 ? false : true) == true){  // Bit 7
    Serial.print("E");
  }
  if ((data & 0x80 ? false : true) == true){  // Bit 8
    Serial.print("SE");
  }
  if ((data & 0x10 ? false : true) == true){  // Bit 5
    Serial.print("S");
  }
  if ((data & 0x02 ? false : true) == true){  // Bit 2
    Serial.print("W");
  }
  if ((data & 0x01 ? false : true) == true){  // Bit 1
    Serial.print("SW");
  }
  Serial.println();
}

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer