PCA9702 reading inputs

Hello, I am working with PCA9701/PCA9702, these are 16 bit and 8 bit GPI shift registers for adding extra ports, these work with SPI, I come to this forum to get advice and help as I am not sure if I am understanding how does this work, the data sheet says that it takes CS low, then serial clock and SDIN, to read the input ports, I am using a basic routine using SPI library for reading the PCA9702 however, I do not know which data to send on SDIN, if I send 0xFF I get the same at SDOUT, I hope you can give me some pointers, here is my code

#include <SPI.h>

byte receivedVal;
void setup() {

//Serial.begin(); //Serial Monitor is eanbled
SPI.setDataMode( SPI_MODE1 ) ;
SPI.begin();

Serial.begin(9600);
Serial.setTimeout(50);

}

void loop() {

//Set up the two commands to be sent
byte myData[] = { 0xFF } ; //8-bit command/data

digitalWrite(SS, LOW); //CS/ of chip is made LOW for data communication

delayMicroseconds(100); //for stabilization (optional)
//----------------------
SPI.transfer(myData, sizeof(myData)); //data for SDIN so shift register can shift
receivedVal = SPI.transfer(0x00); //read data on SDOUT

delayMicroseconds(100); //for stabilization (optional)
digitalWrite(SS, HIGH); //data is latched at output at rising edge of CS/

delayMicroseconds(100); //for stabilization
//----------------------
Serial.println(receivedVal); //display data read

}

however, I only see 256 on the output
this is the Arduino to PCA9702 connection

I am applying 12 Vdc to the input IN0, using 5Vdc supply on PCA9702 as datasheet says it can take up to 5.5Vdc

can you tell me if this code is correct ?

Post your code using code tags <CODE>
Just click on the icon and paste your code where it says "paste your code here"

#include <SPI.h>

byte receivedVal;
void setup() {

//Serial.begin(); //Serial Monitor is eanbled
SPI.setDataMode( SPI_MODE1 ) ;
SPI.begin();

Serial.begin(9600);
Serial.setTimeout(50);

}

void loop() {

//Set up the two commands to be sent
byte myData[] = { 0xFF } ; //8-bit command/data

digitalWrite(SS, LOW); //CS/ of chip is made LOW for data communication

delayMicroseconds(100); //for stabilization (optional)
//----------------------
SPI.transfer(myData, sizeof(myData)); //data for SDIN so shift register can shift
receivedVal = SPI.transfer(0x00); //read data on SDOUT

delayMicroseconds(100); //for stabilization (optional)
digitalWrite(SS, HIGH); //data is latched at output at rising edge of CS/

delayMicroseconds(100); //for stabilization
//----------------------
Serial.println(receivedVal); //display data read

}

if I want to read the input ports, will the above code be good enough ?, I am very new to Arduino

Instead of
SPI.setDataMode( SPI_MODE1 ) ;
use the following but it should be called AFTER SPI.begin()
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1));

This line does nothing, remove it
SPI.transfer(myData, sizeof(myData)); //data for SDIN so shift register can shift

What do you have connected to the inputs of the PCA inputs?

Put a delay(100) at the end of loop

thanks for the reply, I am connecting 5vdc to the PCA9702 inputs on IN0-IN3 and GND to IN4-IN7

You should see 0x0F

Try this code

#include <SPI.h>

byte receivedVal;

void setup() {
SPI.begin();
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1));
Serial.begin(9600);
}

void loop() {
digitalWrite(SS, LOW); // CS/ of chip is made LOW for data communication
delayMicroseconds(100); //for stabilization (optional)

receivedVal = SPI.transfer(0x00); //read data on SDOUT

delayMicroseconds(100); //for stabilization (optional)
digitalWrite(SS, HIGH); 
Serial.println(receivedVal); //display data read
delay(100);
}

I have tried the code, always get 10000000 (0x80) on the output, applied GND to all inputs

on the code, receivedVal = SPI.transfer(0x00); is meant for reading, however, don't we need to send info to the SDIN register so it can read it and shift it out to the SDOUT ?

Is working now, I can monitor the input status, thank you very much for your help, the issue was that the board has input resistors connected to a 24 vdc source for pull-upp, I was usng only 5Vdc which made undetermined input state.
Now I can go to the next level as there are daisy chained multiple PCA9701s, I will post later and hope you can keep helping me, thanks

Your post #1 says that you are using 8-bit PCA9702, now you are saying about 16-bit PCA9701. Which one is correct?

Hello GolamMostafa, thanks for your reply, I am analyzing a board that is used to monitor and control inputs and outputs, the board has one PCA9702 and 6 PCA9701, all chips are daisy chained starting from 9702, before I posted this topic I was struggling trying to read the inputs of the whole 117 inputs from the board, so I started by trying to read the first 8 inputs from the very first GPI (9702) which is an 8 bit register, Jim-P was able to help me by modifying the code which had additional non functional lines, as I soldered a wire to the 9702 SDOUT and connected it to Arduino SDIN (pin 12) I am able to work with the 9702, however, the main goal is to read all the inputs, so hopefully I can keep on the same topic or do you recommend to open a new one that states how to read 9701/9702 daisy chain ?

so here is the next step, the board has a PCA9702 and is daisy chained to a PCA9701, and this to another 9701 and so forth up to 7 PCA9701, so there is one PCA9702 and daisy chain 7 chips PCA9701, in this picture I only put 9702 daisy chained to 9701 for simplicity

-Arduino SCK connects to all chips SCK
Arduino CS(active low) connects to all chips CS(active low)
-Arduino SDOUT (MOSI) connects to 9702 SDIN
-Arduino SDIN (MISO) is input and SDOUT from last 9701 on the chain connects to it

the data sheet states: Multiple PCA9701 or PCA9702 devices can be serially connected for monitoring a large number of switches by connecting the SDOUT of one device to the SDIN of the next device

the question now is how can I read these 24 bits (3 bytes), starting for the first two chips, as the goal would be reading the whole chain, I assume that if I am able to read these topology I can be able to read the additional ones

the problem that I have is that I do not know how to send SPI data as the first would take only one byte and the second 2 bytes, I have read in other threads that is posible to send 2 bytes partitioned , whatever that means, any idea on how to attain this ?

The PCA9701/9702 does not require any iniialization, which means that there is no need to send any command from the Host (Arduino UNO) to the GPI. If this statement is correct, then why should the MOSI line of UNO be connected with the SDIN line of the GPI?

Can you show a diagram how you are going to casecade two PCA9701 chips and connect them with UNO using SPI Port?

Write codes to read 16-bit input data from a PCA9701 first and then connect two PCA9701 in a casecade manner and read 16-bit from individual chip. (There is a little bit difference between casecade and daisy chain concepts.)

here is the diagram o how to connect two PCA9701 daisy chained

I have removed Arduino SDOUT as per your observation

Then you need to do 15 transfers


#include <SPI.h>

byte receivedVal[15];

void setup() {
  SPI.begin();
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1));
  Serial.begin(9600);
}

void loop() {
  digitalWrite(SS, LOW); // CS/ of chip is made LOW for data communication
  delayMicroseconds(100); //for stabilization (optional)

  for (int i = 0; i < 15; i++)
  {
    receivedVal[i] = SPI.transfer(0x00); //read data on SDOUT
  }

  delayMicroseconds(100); //for stabilization (optional)
  digitalWrite(SS, HIGH);
  for (int i = 0; i < 15; i++)
  {
    Serial.print(receivedVal[i], HEX); //display data read
    Serial.print("  ");
  }
    Serial.println();

  delay(200);
}

You should not leave it floating.
Connect to ground

Why are all the CS/-pin are tied together? Please, read carefully the function of the CS/-pin (Fig-1). (I don't have the chips to test for you.)
pca9701CS-pin
Figure-1:

I ran the code above, however, I only get FF FF FF FF FF FF FF FF FF FF FF FF FF FF
does the receivedVal is an 8 bit or 16 bits ?, as the PCA9702 is 8 bit, I think this would be good if all chips were 8 bit register, but in this case only te first is 8 bit and the 7 additional are 16 bit register, please let me know if I am right, I think we should send 8 clock pulses for the first and 16 clock pulses for the PCA9701 , what do you think ?

Regarding the CS being all connected togheter that means is daisy chain, as the SPI theory indicates, I do not have a dedicated CS pin for each of the chips, this is an alternate way to do it as the SPI sends all chips CS low then in order to access that specific chip the clock pulses must be sufficient enough to reach that register