SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By triath5147
#116428
Hi, I was wondering if anyone could help me out. I am using the PCF8575 from SparkFun to learn I2C. I have successfully gotten to the point Where I can send info to the chip to make LED's light up on the pins I desire. However reading from it has become quite frustrating.

I have the below code currently and it does not work at all. It is obvious that I do not understand how this chip communicates. In the DATA SHEET it says that you have to write all ones to the chip first to select inputs, but When I tried that it did not act like i thought it should. I than tried to access only pins 10-17 to read from and use pins 0 -7 as the outputs to light LED's. Again with no luck. Could you please help me by either correcting my code below, or be writing me a short read script so that I can dissect it, and understand what to do. I am currently looking to read a switch on one register, and than light an LED on the opposite register. Yout time is much appreciated.
Code: Select all
#include <Wire.h>

byte address = 0x20;   // address of PCF8575 with A0-A2 connected to GND "B01000000"
byte input;
byte c;
byte d;
                     
void setup()
{
   Wire.begin();       // join i2c bus 
}

//control the pins 

void loop(){
  
  byte a = B11000000; //controls pins 0 - 7
  byte b = B00110011; //controls pins 10-17
  
  // by changing the 1's and 0's above you can control which pins are on and off. 
  // Remember the chip can sink more than it can source So the 1's are off and the 0's are on.
    input = Wire.requestFrom(0x20,2);
    
    if(input == 2) {
      c = Wire.receive();
      d = Wire.receive();
    }
    if(d == B00010000) {
      Wire.beginTransmission(address);   // send the address and the write cmnd
      Wire.send(a); 
      Wire.endTransmission();   
    }      // pack the first byte
    else {
      Wire.beginTransmission(address);   // send the address and the write cmnd
      Wire.send(b);                      // pack the second byte
      Wire.endTransmission(); 
    }      // send the data
      

} 
By triath5147
#116439
OK I can now read from the PCF8575 and get a Binary output to print. I will be working on putting everything together to get an input on one side to trigger an output on the other as I get the time. But until then here is the code.
Code: Select all
#include <Wire.h>

byte address = 0x20;   // address of PCF8575

byte input;
byte c;
byte d;

void setup()
{
   Wire.begin();	 // join i2c bus
   Serial.begin(9600);
}

void loop(){

	  input = Wire.requestFrom(0x20,2);

	  c = Wire.receive();
	  d = Wire.receive();
    Serial.println(c, BIN);
	 delay(1000);
    Serial.println(d,BIN);
	 delay(1000);

    }
	 

By triath5147
#116441
OK I got to read the PCF8575 and turn on an LED sinking to Pin 13 of Arduino.
Code: Select all
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Read from a PCF8575 to control the inputs using the I2C bus. In this example I get an input from a switch 
//   connected to ground through a reistor. Which will return a "0" when the switch is pressed. By pressing the selected
//   switch it will then turn on an LED sinking to Arduino Pin 13.
//   1/01/2010 Sonny Schade triath5147@msn.com  Use and have fun. If you make any changes that improve
//                                               this code,Please post it to the community.
///////////////////////////////////////////////////////////////////////////////////////////////////////////




#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
                     
void setup()
{
   Wire.begin();       // join i2c bus 
   Serial.begin(9600); // used to Debug , Thanks PaulS
}



void loop(){
  
  
    input = Wire.requestFrom(0x20,2);
       
      c = Wire.receive();
      d = Wire.receive();
      Wire.endTransmission();
      
       if(d == B10111111) {
     digitalWrite(13, LOW); 
       }
       else {
         digitalWrite(13, HIGH);
       }
    Serial.println(c, BIN);  
    // delay(1000);
    Serial.println(d,BIN); 
    
    }      // send the data
      
OK so there we have it. Now if someone in this forum is more familiar with the Wire programming that can double check my work, to see if I am doing anything I shouldn't be, that might effect the ability to run other code in the loop.