Leonardo SPI/ICSP and 74HC165D board not working

Hello everyone.
This is my first project and topic here. Unfortunately I have faced an issue connecting ali-xpress board with 3x74HC165D to Leonardo.


This is my board picture.
I've connected:

  1. Out: 5v to Leonardo ICSP 5v
  2. Out: GND to Leonardo ICSP GND
  3. Out: MISO to Leonardo ICSP MISO
  4. Out: SCK to Leonardo ICSP SCK
  5. Out: CS to Leonardo RX LED (Which should be SS)

I had multiple revisions of code. For example:

#include <SPI.h>

const int numChips = 3;

void setup() {
  SPI.begin();
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < numChips; i++) {
    byte data = SPI.transfer(0x00); 
    Serial.print("Chip ");
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.println(data, BIN); 
  }

  delay(500); 
}

And my output always 0. If I put 5v directly to MISO I get my 11111111
Does anybody know how I solve this issue?
Will appreciate any help.

Why OUT? you are putting IN the signals.

I thought that out of expansion board into Arduino Leonardo :thinking:
Isn't it right? There is arrows as well from IN to OUT.
I planned daisy chain 2nd board into IN

Maybe try this
Edit: If you connect these serial, only one CS needed and 3 reads instead.
The outputs are not tri-state, so serial is your only option without a 74LS125.

#include <SPI.h>

const int CS = 4;

void setup() {
  SPI.begin();
  Serial.begin(9600);
  pinMode(CS,OUTPUT);
  digitalWrite(CS,HIGH);
}

void loop() {
  digitalWrite(CS,LOW);
  SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
  byte data1 = SPI.transfer(0x00); 
  byte data2 = SPI.transfer(0x00); 
  byte data3 = SPI.transfer(0x00); 
  SPI.endTransaction();
  digitalWrite(CS,HIGH);

  Serial.print("Chip 1 ");
  Serial.println(data1, BIN); 
  Serial.print("Chip 2 ");
  Serial.println(data2, BIN); 
  Serial.print("Chip 3 ");
  Serial.println(data3, BIN); 
}

Did not work.
I even tried connecting pins instead of ICSP directly to digital and avoid using SPI.h
And outputs sometimes (in any case, ICSP or digital pins connection) switching. Like all 0 or all 1. I tested my soldering, there is no short circuits between wires.
And weirdly there is a voltage on signal wires. Around 4v, which should not be the case :thinking: . Maybe expansion board does something with 74HC165D chips... I clearly see there 4 pin resistor 103 for each 4 of the signal connections

Example of code

#define dataPin 7   /* MISO */
#define clockPin 8  /* SCK */
#define latchPin 4  /* CS */

const int numBits = 8 * 3;   /* Set to 8 * number of shift registers */

long readDelay = 200;
long previousInputTime = 0;
void setup() {
  Serial.begin(9600);
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
}


void loop() {
  readInput();
}

void readInput() {
  long currentMillis = millis();
  if (currentMillis - previousInputTime < readDelay) {
    return;
  }
  previousInputTime = currentMillis;
  digitalWrite(latchPin, HIGH);
  digitalWrite(latchPin, LOW);

  // Step 2: Shift
  Serial.print("Bits: ");
  for (int i = 0; i < numBits; i++) {
    int bit = digitalRead(dataPin);
    if (bit == HIGH) {
      Serial.print("1");
    } else {
      Serial.print("0");
    }
    digitalWrite(clockPin, HIGH); // Shift out the next bit
    digitalWrite(clockPin, LOW);
  }

  Serial.println();
}

I believe I've found a scheme

I think I set the SPI transfer rate too high for your device.

#include <SPI.h>

#define CS 4

void setup() {
  SPI.begin();
  Serial.begin(9600);
  pinMode(CS,OUTPUT);
  digitalWrite(CS,HIGH);
}

void loop() {

  digitalWrite(CS,LOW);
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  byte data1 = SPI.transfer(0x00); 
  byte data2 = SPI.transfer(0x00); 
  byte data3 = SPI.transfer(0x00); 
  SPI.endTransaction();
  digitalWrite(CS,HIGH);

  Serial.print("Chip 1 ");
  Serial.println(data1, BIN); 
  Serial.print("Chip 2 ");
  Serial.println(data2, BIN); 
  Serial.print("Chip 3 ");
  Serial.println(data3, BIN); 

  delay(500);
}

@SurferTim Thank you for your time.
Unfortunately lowering rate also does not work.
I am not sure now if I connected everything correctly...

The issue that also exist. Randomly reading switches between all bits 0 to all bits 1
Like:

Chip 1 11111111
Chip 2 11111111
Chip 3 11111111
Chip 1 0
Chip 2 0
Chip 3 0
Chip 1 11111111
Chip 2 11111111
Chip 3 11111111
Chip 1 11111111
Chip 2 11111111
Chip 3 11111111
Chip 1 0
Chip 2 0
Chip 3 0

You aren't leaving the inputs floating, are you?
You may need to control the data latch also. I think that is the SL pin on your schematic.

Edit: You should have the output of your device connected to the MISO pin and the clock to the SCK pin on the ICSP pins on the Leonardo.

@SurferTim I connected SL to Pin 4, cause it is located under CS mark on my board.
So it seems like I do not have latch here?
My pins inputs are just wire which I press by hand to ground or 5v to see any change.
It should give some change to output anyway, right?
Plus on the scheme it seems like I have pullup resistors connected...
On my board I see on each connector pins N1, N2, N3, N4, GRND. So it seemed for me like I need to just connect input to the ground to see a result...


This is my current connection.
RED -> 5v
Black -> GND
Green -> MISO
Blue/Purple -> SCK

P.s. My board was bought here: https://www.aliexpress.com/item/1005006434921578.html?spm=a2g0o.order_list.order_list_main.10.37e31802nZlDCl

Edit, if you look into the images on the shop site, they connect IN to the board and daisychain everything into the OUT connector based on orientation of resistors :thinking:
But I connect OUT to my Leonardo.
Going with first advice and connecting IN was dumb and heated my board...

OUT goes to the Leonardo. IN should be from the next 74HC165 set if you desire more than one of those boards.

@SurferTim That is how I connect it, so should not be a problem here...

Don't know what to tell you. According to the datasheet and the schematic, it should work.

@SurferTim I understand. I even resoldered everything just to be on the safe side...
I ordered standalone 74HC165N pieces, will try to wire them directly without any aliexpress boards, maybe then it will work :thinking:
Thank you for your time.

I did forget one thing. The SPI mode. It may be a different mode. If SPI_MODE2 doesn't work, try SPI_MODE1 and SPI_MODE3.

  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE2));

I tried this. No effect as well... I have no idea what may be wrong or could be my wiring quality be an issue

I think this is the wrong way round. latchPin needs a LOW pulse to load the data and must be HIGH while shifting.

As per the datasheet:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.