0

I'm quite new to Arduino world and I'm trying to understand how shift registers work. I built this construction : enter image description here

In fact, it is an Arduino Duemilanove with AtMega on it, but afaik it doesn't matter.

The tutorial I try to follow use SPI library so I try to do it too. I kind of understood that data input, latch and clock had to be pins 7, 4 and 11.

Basically, all I try to do is sending a byte to light the LEDs. As for me now, the sequence should light all LED, then no one, then one after another. I know there is one more input ont the right side of the SR but I didn't plan this model so I ran out of LEDs...

This is the code I use :

#include <SPI.h>
#include <StandardCplusplus.h>
#include <vector>
#include <iterator>
#include <size_type>

using namespace std;


//pin du shift register
#define DATA_PIN 13
#define LATCH_PIN 4
#define CLOCK_PIN 11
//à utiliser plus tard
#define INPUT_PIN 2


void setup() {  
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  //pinMode(INPUT_PIN, INPUT);

  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  SPI.begin();

  //SPI.transfer(getBit(3));
  //digitalWrite(LATCH_PIN, HIGH);
  //digitalWrite(LATCH_PIN, LOW);

  Serial.begin(9600);
  Serial.println("start !");
}


void loop() {
  shift(B11111111);
  shift(B00000000);
  shift(B10000000);
  shift(B01000000);
  shift(B00100000);
  shift(B00010000);
  shift(B00001000);
  shift(B00000100);
  shift(B00000010);
  shift(B00000001);
}


void shift(int n){
  SPI.transfer(n);
  digitalWrite(LATCH_PIN, HIGH);
  digitalWrite(LATCH_PIN, LOW);
  Serial.print("shift : ");
  Serial.println(n);
  delay(1000);
}

When I upload this code, LEDs sequence has no sense, but serial output was totally OK.

What I see (without the output 0 of course) is : 1111111, 0111111, 1011111, 1110111, 1111101 and then 1111111 without further changes, whereas serial output is 255, 0, 128, 64, 32, 16, 8, 4, 2, 1, 255, 0, 128 and so on.

I assume I didn't understand something important, and it prevent me from finding answers in google.

Thanks for helping, it's really disturbing for me.

EDITS :

changed the title, it is a 74HC595.

Strangest behavior too (not gonna create another topic for that now), I dont have to connect both the gnd and VCC from my arduino for this to light LEDs. It changes a little the brighness of LEDs, but surprisingly VCC< GND< both

This is the tutorial I use to understand shift registers : https://youtu.be/6fVbJbNPrEU?t=195, and the one that uses SPI is nXl4fb_LbcI

I am now aware that I should get one resistor for each LED but I don't have that much resistors and as far as I can see this don't harm my LEDs nor underpower them.

6
  • Which part are you using? - question says 74HC165, Fritzing drawing shows 74HC595. You show a resistor between the right and left "ground" busses - the IC's ground pin must be connected directly to ground. You also need current-limiting resistors in series with each LED. Commented Aug 4, 2015 at 0:30
  • On a 74HC595, pin 12 (wired to Due pin 13) is STCP, storage register clock input, while pin 14 (wired to Due pin 4) is DS , serial data input. This conflicts with your #define DATA_PIN 13, #define LATCH_PIN 4 statements. Maybe your other code compensates; but anyway fix it on your board and in your code and in your fritzing diagram then edit the question to match. I suggest you wire Ard pin 12 to '595 pin 12 and Ard pin 13 to '595 pin 13 to reduce cognitive dissonance. Also, wire pin 8 of the '595 directly to a ground bus, not to the LED bus that has a resistor between it and ground. Commented Aug 4, 2015 at 1:12
  • @PeterBennett just a typo, thanks. I didn't know about the resistor, and this conflict with what I understand about current, I will investigate. Commented Aug 4, 2015 at 8:09
  • @jwpat7 the tutorial video I watched uses different names as yours so it confuses me a little. It also says that these pins are mandatory to use SPI, but maybe I misunderstood. I corrected the Fritzing drawing, it was just an error in drawing. Commented Aug 4, 2015 at 8:23
  • I dont have to connect both the gnd and VCC from my arduino for this to light LEDs. - that is called "parasitic power" and it isn't good for the chip. Effectively the chip is being powered by the data inputs, which it is not designed to do. my LEDs don't die - not yet, no.
    – Nick Gammon
    Commented Aug 4, 2015 at 10:30

1 Answer 1

1

You have a number of problems here. The first is the wiring.

595 wiring diagram

My wiring diagram above is for the Uno / Duemilanove. Since that is what you have, you should follow that.

The pins are fixed in hardware, except for the slave select pin. Thus D13 on the Duemilanove should go to pin 11 on the 595. D11 on the Duemilanove should go to pin 14 on the 595. And your slave select (which may as well be pin 10 on the Duemilanove) should go to pin 12 on the 595.


Your code looks more complex than it needs to be. This code will exercise the 8 LEDs:

#include <SPI.h>

const byte LATCH = 10;

void setup ()
{
  SPI.begin ();
}  // end of setup

byte c;
void loop ()
{
  c++;
  digitalWrite (LATCH, LOW);
  SPI.transfer (c);
  digitalWrite (LATCH, HIGH);
  delay (20);
}  // end of loop

As other responders have said, you should have a current-limiting resistor for each LED. You can't share it like that.

Also the 595 ground (pin 8) should be wired to the Duemilanove ground, not via a resistor.


Reference

Using a 74HC595 output shift register as a port-expander

7
  • I corrected my diagram, is it OK now ? I'll let resistor like that because my LEDs don't die and I dont have enough resistors yet. "The pins are fixed in hardware", I assumed that but I cannot find it on the web. Also the tutorial I use (youtu.be/nXl4fb_LbcI?t=777) don't really agree with you. Commented Aug 4, 2015 at 8:44
  • Yes, well that is certainly sad. I trust you don't believe everything you see on YouTube. :) He is bringing the chip select low and high at the wrong time. See SPI - Serial Peripheral Interface - for Arduino. Have a look at my post about Hacking a scrolling LED strip sign - as I sit here that sign is still showing me the temperature and humidity, so I think my recommendations are correct.
    – Nick Gammon
    Commented Aug 4, 2015 at 10:26
  • When you're still a young newbie, you trust anyone who's brave enough to make a youtube tutorial you know :-) Also there is quite a lot of different names for pins and concepts. I cannot figure out what are MOSI, SS and SCK compared to data_in, latch, clock clear... Your LED strip sign is so cool ! I hope someday I'll have the level to do stuffes like this ! Commented Aug 4, 2015 at 11:23
  • In a couple of cases, it's ok to use a single resistor for all the LEDs (supposing the LEDs all have comparable Vf): 1, if only one LED is ever on at once; 2, if you don't care that LEDs get dimmer as more of them are turned on. For a simple lashup like this it's reasonable to use a single dropping resistor. Commented Aug 4, 2015 at 14:58
  • Hopefully my diagram clears up the pin names. MOSI (Master Out, Slave In) = Data_In (on the slave), SCK (Serial Clock) = Clock, SS (Slave Select) = Latch (in this case).
    – Nick Gammon
    Commented Aug 4, 2015 at 20:33

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.