Need some help please with 16pin IO board

Hello i am hoping someone can help me please, i am trying to get a sketch to work for a pcf8575 16 pin i/o board to enable to me to use the full 16 pins to detect individual states of 16 buttons and trigger an LED change of state.

i have a pcf8575 board connected using the I2c bus to an Arduino uno and have the below sketch which appear to run correctly but is not changing the status of the led's the pin i am watching in this instance is pin 6 on the i/o board and i wonder if i have selected the correct bit for it. any help would be greatly appreciated mamy thanks Kevin



//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   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
int ledPin1 = 3;
int ledPin2 = 4;
                     
void setup()
{
   Wire.begin();       // join i2c bus 
   Serial.begin(9600); // used to Debug , Thanks PaulS
   pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}



void loop(){
  
  
    input = Wire.requestFrom(0x20,2);
       
      c = Wire.read();
      d = Wire.read();
      Wire.endTransmission();
      
       if(bitRead(d,6) == 0) {
        //Serial.println("Button Pressed");
     digitalWrite(3, LOW);
     digitalWrite(4, HIGH); 
       }
       else {
         digitalWrite(3, HIGH);
         digitalWrite(4, LOW);
       }
    Serial.println(c, BIN);  
    // delay(1000);
    Serial.println(d,BIN); 
    
    }      // send the data
      

Is that pin 6 numbered from 1 or pin 6 numbered from zero ?

Hi,
Can you please post a schematic of your project?

Have you looked at the pcf8575 library of arduino?

Tom.. :grinning: :+1: :coffee: :australia:

One of these?
download

The button is connected between "P06" and ground?

I'm not certain which sequence the bytes corresponding to pins "P00"-"P07" and pins "P10"-"P17" will arrive. Maybe the bit you are looking for is in d instead of c?

What do you see on Serial Monitor and what changes when you push the button?

Please click Tools->Auto Format in the IDE before you post your code. Thanks!

Hi Yes i have looked through it, to be honest its some what confusing, i have been looking for example sketches to read the pins but they are very few and far between, lots on how to set the pin outputs with an led for instance but i need to read the pins (im using it as an input board only to fire 16 individual servos using a PCA9685 ic2 board. which i have working - just need this board to provide the inputs to fire the servos)

originaly the sketch i found had the below instead

replaced > if(d == B10111111) {

in the code , with:

if(bitRead(d,6) == 0) {

and i'm wondering if that's the issue, it cant find the correct bit value for the pins.

yep that's the chappie. I've connected the sca, vcc, gnd and sda correctly to the Arduino,
when the sketch runs it flicks between various values as shown in the attached,
when i press the button it goes to all 0 until i let it go.

Well, that's bit 5 that is changing, not bit 6. Still can't tell if it is bit 5 in c or d.

Please don't post images of serial monitor. Copy the text from serial monitor and paste it into your reply, between code tags. You can disable auto-scrolling if it makes it easier.

Hence my question

The pin numbering starts from "P00", so I asked @kinsella12 if the button was attached to pin "P06". I didn't get an answer.

Why did you come to the conclusion that it is bit 5?

Seeing the license plate image, and basing it on the OP's information,
I counted:

PIN - Bit
P00 = bit 0;
P01 = bit 1;
P02 = bit 2;
P03 = bit 3;
P04 = bit 4;
P05 = bit 5;
P06 = bit 6;
P07 = bit 7;
.......

Where am I going wrong?

Tks.

Because of the serial monitor image posted by @kinsella12 . Some lines show "0" and others show "100000" (i.e. bit 5 is changing).

What I don't understand is why the other bits are all zero, unless @kinsella12 has them all connected to ground. As far as I know, all inputs of pcf8575 default to INPUT and are pulled weakly high internally.

1 Like

Apologies, im in New Zelaand so 12hrs different (was alseep!! )
the switch is connected one side to ground the other to P05 (which i assumed was bit 6, P00 being bit 1 and so on)
cheers

Hi sorry for delay, all other pins on the PCF8575 are currently not connected at all, do i need to set the pins as HIGH in the setup of the sketch or define them as inputs so the PCF sets them high itself?
cheers Kevin

No, P00 would be bit 0. P05 would be bit 5.

No, as I explained in post #11. I don't understand why the other bits all read 0. I expected they would read 1 by default.

whats the best way to share a diagram, photo of how ive got it wired up, just in case ive messed up with the wiring.
cheers

HI ive just completely re connected everything from scratch, now i have the below in the monitor window. but nothing is changing when i press the button connected to p05 i have the button connected to gnd and p05 so that when i press it the p05 pin goes low.

11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111

Hi,
Post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Please do not use Fritzy.

A picture of a hand drawn is usually the easiest.

Tom.. :grinning: :+1: :coffee: :australia:

its a bit crude im afraid but this is the circuit i am using. cheers

Hi, @kinsella12
Good schematic. :+1:

Do you need a 10K pullup resistor between P05 and Vcc to pull the input up when the button is open circuit?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Hi yep sorry forgot to add that, i have resistor from P05 to vcc in the circuit. initially i didnt as i thought all pins goes high on the PCF8575 when in input mode.
is there another way i can read the 16 pins as inputs from buttons and then do different actions depending on which button was pressed? (for info i am wanting to use the PCF8575 to read sixteen buttons that will then action an associated servo supplied by a pca9685 for use in model railway layout to change 16 different points)

cheers