Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Simple Arduino - 74hc595 shift register construction [tmli5]

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.

Answer*

Draft saved
Draft discarded
Cancel
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. 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). Commented Aug 4, 2015 at 20:33

AltStyle によって変換されたページ (->オリジナル) /