I have been using the following link for reference... http://robsraspberrypi.blogspot.co.za/2016/01/raspberry-pi-adding-more-inputs-using.html

// setup SPI for communication
int fd = Spi.wiringPiSPISetup(1, 1000000);
if (fd <= -1) {
      throw new IOException("SPI port setup failed.");
byte packet[] = new byte[1];
// send data packet
int i Spi.wiringPiSPIDataRW(0, packet, 1);
  • Have you contacted the author?
    – joan
    Commented Jul 25, 2016 at 11:29
  • 1
    What is not working? What is the question you need answered ? Commented Jul 25, 2016 at 11:56
  • The code just returns a 1 when calling the java code above and i dont know why. Does anyone know if this is the correct way of doing it
    – Brian
    Commented Jul 25, 2016 at 12:44
  • Can you post a schematic as how you have wired it including all power and ground connections. The frizzy picture in your link is incomplete.
    – Gil
    Commented Nov 30, 2022 at 4:20
  • Consider purchasing one of the inexpensive 24MHz logic analyzers and then you can watch the clocks etc to be sure you are following the data sheet. Some of the software for these will show you the SPI data and convert to Hex, Ascii, Decimal, etc. You then need to compare this to the data sheet. The important thing is the loks and the Latch timing. The latch must remain stable while the bits are shifted out.
    – Gil
    Commented Nov 30, 2022 at 17:39

1 Answer 1


Okay, so it looks like there a number of issues with your code. I'm not sure it would even run. This line...

int fd = Spi.wiringPiSPISetup(1, 1000000);

...is setting up channel 1 for 1Mhz (which should be fine), but later you are trying to read/write on channel 0 (which has not yet been setup...)

int i Spi.wiringPiSPIDataRW(0, packet, 1);

I also think you need a = in there. int i = Spi.wiringPiSPIDataRW(0, packet, 1); This line...

byte packet[] = new byte[1];

...looks like it declares an array of 1 byte (?? I'm no java expert ??) but it is not initialized with any value ? That means you are sending whatever value happens to be in that byte at the time to the SPI bus and reading back it's output...

Finally - I don't think that you get the result in i (if that's what you were expecting ?), the docs for wiringPiSPI say:

  • int wiringPiSPIDataRW (int channel, unsigned char *data, int len);

This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

So the data you are trying to read should end up in packet.

I would spend some more time looking at wiringPiSPI and some of the questions/comments here.

  • One clarification: In Java this line byte packet[] = new byte[1]; will create an array of length 1 and initialize the contents to 0. That's different than some other languages, where you would get a garbage value in the array at initialization. Rest of this seems right.
    – Brick
    Commented Nov 5, 2017 at 13:28
  • Actually, you need to use a 2-byte buffer, the first is written (you don't care what it is, this isn't real SPI), the second byte of the buffer will be filled with the 75hc165 parallel input values. You also need to use spidev0.1 (channel 1) CE pin to trigger the SH/LD pin on the shift-register and then read the values on spidev0.0 (channel 0) MISO. Use the CLCK pin from channel 0 for the shift-register clock. This works amazingly well. Essentially just loop, read from channel 1 (it will trigger the CE pin), then read channel 0 MISO for the values (wonky, but this isn't true SPI) Commented Apr 22 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.