Go Down

Topic: Using the Phillips PCF8574A I2C as an INPUT? ( = e.g. a button.) (Read 1 time) previous topic - next topic

levorto

Jul 18, 2011, 03:21 pm Last Edit: Jul 18, 2011, 03:29 pm by levorto Reason: 1
Hello everyone !

I recieved an I2C chip a few days ago: The Phillips PCF8574A 8bit I/O expander.
http://www.datasheetcatalog.com/datasheets_pdf/P/C/F/8/PCF8574A.shtml

Half an hour later, I had hooked it up and was able to use it as OUTPUT, using the following (Sinking Current) technique :

+5V -----------> (+) Anode of LED --- (-) Cathode of LED ---[¤ R=33O Ohm ¤] ------------> P4 on PCF8574A.

I have since then tried to use one of the I2C-ports as an INPUT. I have now tried for a few days without luck.
Im not sure I understand how to create a sinking current button. Also, when reading through posts on this board, people seem to mention very different facts on how to setup resistors. One person says "Put 10K on SCL and SDA and put 2.2K on INT" and another says "2.2K on SCL and SDA and nothing else".

Does anyone know how to do this? Help is VERY much appreciated.

Quote
Grumpy_Mike: So in short if an input pin changes state this line (INT) goes low. You can then put this into one of the normal arduino pins and monitor that to see if an input has changed. This is a lot quicker than reading the I2C device itself.

- How would I set that up? Do I put the INT directly to a pin on the Arduino? Do I add pull-up resistors?

Posts with info:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256915868
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1281290936

johnwasser

The datasheet says the Interrupt output is "open drain" which is the MOS equivalent of the TTL "open collector".  That means the the pin will be floating or grounded and needs a pull-up resistor.  You can use the internal pull-up of the Arduino pin.

I think the datasheet is saying that you have to write a 1 to a pin before you can use it as an input.  After you set it to 1 your button can pull it to ground to force it to 0 so you can sense the button press.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

levorto

#2
Jul 19, 2011, 11:15 pm Last Edit: Jul 19, 2011, 11:40 pm by levorto Reason: 1
[Edit]

Now I managed to get the board to register a button. My problem now lies in the coding.
Can someone tell me how to change the Serial.println so it only prints when the button changes?

Code: [Select]
#include <Wire.h>
#define expander B0111000  //expander address

volatile byte count = 0;

void setup() {
Wire.begin();
Serial.begin(9600);
expanderWrite(B00100000);
attachInterrupt(5, expanderInterrupt, CHANGE);
}

void loop() {
  if(count = 1) {
    Serial.println(expanderRead(), BIN);
    count = 0;
    attachInterrupt(5, expanderInterrupt, CHANGE);
  }
}

void expanderInterrupt() {   
  detachInterrupt(5);
  count = 1;
}

void expanderWrite(byte _data ) {
  Wire.beginTransmission(expander);
  Wire.send(_data);
  Wire.endTransmission();
}

byte expanderRead() {
  byte _data;
  Wire.requestFrom(expander, 1);
  if(Wire.available()) {
    _data = Wire.receive();
  }
  return _data;
}

johnwasser

Code: [Select]

void loop() {
  if(count = 1) {   ////////////  Should be (count == 1).  The second '='makes all the difference.
    Serial.println(expanderRead(), BIN);
    count = 0;
    attachInterrupt(5, expanderInterrupt, CHANGE);
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Go Up