-2
\$\begingroup\$

I want to send data to my 74HC595 via SPI (in Arduino). But I could not manage to blink my LEDs. My code is in below.

#include <SPI.h>

int clock_pin = 13; // SCLK
int data_pin = 11;  // MOSI
int latch_pin = 10; // SS

uint8_t led ;
void setup() {
  pinMode(latch_pin, OUTPUT); // enable pin 10 as latch pin
  
  // SPI Setup
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8); 
  SPI.setBitOrder(LSBFIRST);  
  digitalWrite(latch_pin, HIGH);
}

void loop() {
  led = 0b10101111;
  put_Register(led);
  delay(500);  
  led = 0b10101101;
  put_Register(led);
  delay(5000);                  
}

void put_Register(byte led) {
  digitalWrite(latch_pin, LOW);
  SPI.transfer(led);       
  digitalWrite(latch_pin, HIGH);
}
\$\endgroup\$
5
  • \$\begingroup\$ It doesn't matter much but you wrote HC595 in the question and HC596 in the topic. \$\endgroup\$
    – pipe
    Commented Jul 24 at 18:53
  • 2
    \$\begingroup\$ Well, how did you wire the Arduino to 74HC595? Supply? Ground? Correctly connected LEDs? With series resistors? Also, is this about the electronics or using Arduino libraries? With cases like this the Arduino libraries are unknown black boxes. It may also be the MCU does not run the code properly. \$\endgroup\$
    – Justme
    Commented Jul 24 at 19:11
  • 2
    \$\begingroup\$ You should always describe what you expect to see and what you actually see in as much detail as possible. A schematic is usually useful as well. Just saying it doesn't work is not adequate and you will probably not receive good answers. \$\endgroup\$ Commented Jul 24 at 21:10
  • \$\begingroup\$ Which Arduino board are you using? This SPI pinout might be correct for an Uno, but not for other boards. \$\endgroup\$
    – user85471
    Commented Jul 24 at 22:19
  • \$\begingroup\$ Also note that arduino.stackexchange.com exists \$\endgroup\$
    – user85471
    Commented Jul 24 at 22:21

1 Answer 1

2
\$\begingroup\$

Your latch signal timing is wrong. Is is low during a serial transfer and needs a short positive pulse to transfer the shift register content to the output register.

Do not forget to set the output enable pin /OE to low and master reset /MR to high.

void setup() {
  //.... 
  pinMode(latch_pin, OUTPUT);
  digitalWrite(latch_pin, LOW);  // initial low
}


void put_Register(byte led) {
  SPI.transfer(led);       
  digitalWrite(latch_pin, HIGH);  // short high pulse on STCP
  digitalWrite(latch_pin, LOW);
}
\$\endgroup\$
2
  • \$\begingroup\$ The latch signal timing is not wrong. The 74HC595 latch signal is sensitive to rising edge. It really does not care if you send data to it with latch signal high or low, or whether you give it a high pulse or low pulse. \$\endgroup\$
    – Justme
    Commented Jul 24 at 20:49
  • \$\begingroup\$ @Justme Yes, my bad. It is edge triggered. \$\endgroup\$
    – Jens
    Commented Jul 24 at 21:27

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