Arduino shift register help.

The code I am using is

#include <SPI.h>
byte ssPin = 10;
byte testData = 0xAA;  // test data
void setup(){
pinMode (ssPin,  OUTPUT);
digitalWrite (ssPin, HIGH);
}
void loop(){
digitalWrite (ssPin, LOW); // RCLK
SPI.transfer(testData); // SCK to SRCLK, MOSI to SER In. OE Low, MRCLR HIGH
digitalWrite (ssPin, HIGH);
}

I have a 7-segment display and a 74HC595N npx shift register, both on a breadboard. Below is my hook up. I am confused on why it’s not displaying properly. I need to keep OE on shift register low so all outputs are enabled and SRCLK high or else all data will be cleared. Any help would be appreciated!

7-seg display to shift register (out) hookup:
7 segment display:
A to QA
B to QB
C to QC
D to QD
E to QE
F to QF
G to QG
D.P to QH
common cathodes connected with resistor to ground

Shift register to arduino:
Ser to RX
GND to shift register ground
Vcc to 5v

ilovearduinosomuch: I am confused on why it's not displaying properly.

You really need to define "not displaying properly".

ilovearduinosomuch: common cathodes connected with resistor to ground

One resistor only to the common cathode? That means the display brightness will vary according to the number of segments lit - and they are likely to be unevenly lit anyway. Brightest with "1", dimmest with "8".

TL;DR of sorts on the code for the present.

Hi, can you clarify these a bit more please?

ilovearduinosomuch: // SCK to SRCLK, MOSI to SER In.

Shift register to arduino: Ser to RX

So, exactly what Arduino pins are connected to which pins on the shift register?

Also I don't see an SPI.begin. SPI.setDataMode may also be needed.

Yes, need SPI.begin(); in setup. SPI.setDataMode will not be needed.

I think before I attempt to use a shift register, I should confirm my underlying knowledge of how shift registers work (at least how I think they work.). Ok, below is wiring.

QA-QH = Shift register outputs. (parallel out) VCC = 5V SER = TX (arduino sends the data to Shift register on this pin) SRCLK = Pulled low (when pulled high, shifts register) RCLK = Pulled low (Needs to be pulled high to output the new shift register values) SRCLR = Pulled high (when pulled low, clears entire register) OE = tied to GND (enables output when tied to gnd and is disabled when high.) (Also how would one tie it to ground besides plugging it in to ground rail on BB or arduino headers? I was wondering if you can do that on any regular pin, not sure if that is even possible as I am just a teenager)

I would first connect the IC's GND and VCC pins to their respective partner pins (5v and GND), QA -QH would be connected to anodes of LED's and their cathodes to GND. Then SER to tx, Then SRCLK,to a pin and assign that to be output and make it low. Then, assign RCLK to a pin and make it low, assign a pin to SRCLR and make it high, OE tied to GND.

I am not sure exactly how I would send data and if I do it in bytes or bits. I would think bits because in the code once a bit is sent you would have to pull SRCLK high to shift the bit over, then low again, send another bit and repeat the process over. I really want to achieve this without the simplicity of arduinos shift out function. Is my understanding of how a shift register works accurate? If anything seems off I would love to learn and correct myself! As always I appreciate it. A lot!

Hi, Can you please post a copy of your circuit, in CAD or picture of hand drawn circuit in jpg, png or pdf format.

Thanks. Tom....... :)

I tried using the byte "11111111" to turn on all the led's and it worked, all 0's and it worked, but then I tried doing on off on off on off on off pattern on the leds from left to right and I got the following, off on off off on off off off. I then realized, hey this isn't that 1 didn't mean to turn the led on, i forgot binary was a counting system, I then went to learn more about it on wikipedia and various sites and tried the value 1000 which is 8 and I got on on on off on off off off. I am very confused as to what I need to do to get the on off on off on off on off pattern to work.

the Shift Register outputs are as follows QA - LED 1, QB LED 2, QC - LED 3, ETC. from left to right. the pair QA - LED 1 being first on the left, and the pair QH - LED 8 on the end, left to right? Thanks so much!

/*
SHIFT REGISTER CONTROL

Created 

August 21 2014
By Caleb Putnam

How it works: THIS WILL BE ADDED LATER UPON CODE COMPLETION AND VERIFICATION.


*/

  int dataPin = 10;
  int RCLK = 12;
  int SRCLR = 11;
  int SRCLK = 13;
  byte data = 11111111;

void setup() {

  pinMode(dataPin, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(SRCLR, OUTPUT);
  pinMode(SRCLK, OUTPUT);
  
 }




void loop() {


  digitalWrite(RCLK, LOW);
  digitalWrite(SRCLR, HIGH);
  digitalWrite(SRCLK, LOW);
  
  shiftOut(dataPin, SRCLK, MSBFIRST, data);
  
  digitalWrite(RCLK, HIGH);  


 }

Hi, can you try 00001111 this will tell you which LED is MSB and LSB to make sure you are sending the byte the right way round.

Tom...... :)

Also a circuit diagram please.

Hi,

When you give a number in binary you must put a "B" in front, e.g. "B10101010".

If the sequence is appearing on the leds in the reverse of what you wanted, change "MSBFIRST" to "LSBFIRST" in your sketch.

TomGeorge:
Can you please post a copy of your circuit, in CAD or picture of hand drawn circuit in jpg, png or pdf format.

Seems like you are way overthinking this. Get this simple bit working as the starting point.

Connect Arduino D13 (SCK) to SRCLK.
Connect Arduino D11 (MOSI) to SerIn.
Connect Arduino D10 (SS) to RCLK.
Connect shift register SRCLR to +5.
Connect shift register OE (G) to Gnd.
Connect a 0.1uF cap from shift register Vcc to Gnd.
Connect Arduino Gnd to shift register Gnd.
Connect Arduino +5 to shift register Vcc.

Connect Q6 to resistor to G (anode). Common cathode to Gnd.
Q5 to resistor to F
Q4 to resistor to E
Q3 to resistor to D
Q2 to resistor to C
Q1 to resistor to B
Q0 to resistor to A

#include <SPI.h>
byte ssPin = 10;
byte fontArray[] = { // bit = 1 = segment on. bit = 0 = segment off
0b00111111, // 0   bit 7-6-5-4-3-2-1-0 = DP-g-f-e-d-c-b-a
0b00000110, // 1      A
0b01011011, // 2   F     B
0b01001111, // 3      G
0b01100110, // 4   E     C
0b01101101, // 5      D
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9 
};

byte x; // use as counter

void setup(){
pinMode (ssPin, OUTPUT);
digitalWrite (ssPin,  HIGH); // D10
Serial.begin(9600); // takes care of D0, D1
SPI.begin(); // takes care of D13,D12, D11
}

void loop(){
Serial.println(x);  // show expected result on serial monitor
digitalWrite (ssPin, LOW);
SPI.transfer(fontArray[x]); // look up the font & send it out
digitalWrite (ssPin, HIGH); // shift register output updates on this rising edge
delay(2000); // hold it so you can look back & forth to confirm outputs

x=x+1; // update x for next run thru loop
if (x==10){ // reset if reach end of font array
x=0;
}

} // end loop

Oh wow. As soon as I added the capital B before the byte it worked. How could I have missed that. I now can control the patterns on regular 5mm leds. I thought If I used the data type byte it would automatically be recognized as binary since a byte is composed of 8 bits and a bit is one number, 1 or 0, on or off?? I appreciate all the replies! I am transferring the circuit to fritzing from Eaglecad so I can just get a photo of it.

transferring the circuit to fritzing from Eaglecad

Why? File:Export:Image, all you need to do.

Ahh. +1 crossroads. +1....

Data formats - a lot of that is to make the code easier for us humans.
0b01010101 or B01010101 and 0x55 (hex) make it obvious to the eye that alernating bits are 0,1, while 85 (decimal) is not.

The IDE makes the data we create easy to follow.
If you’re counting 0 to 59 for a clock for example, decimal is nice,vs 0 to 0x3b or 0b00111011 .
Creating fonts, its easier to picture the segments and write it out in binary.

Hi, I am trying to count up from displaying 1 to the number 2. The code is below, it skips the first number and goes to the number 2? I used a delay between the two bytes. The first delay before even sending a byte is to make it easier for me to troubleshoot, and tell which part of the code it is at. Thanks :).

/*
SHIFT REGISTER CONTROL

Created 

August 21 2014
By Caleb Putnam

How it works: THIS WILL BE ADDED LATER UPON CODE COMPLETION AND VERIFICATION.


*/

  int dataPin = 10;
  int RCLK = 12;
  int SRCLR = 11;
  int SRCLK = 13;
  byte data = B00101000;
  byte data2 = B11001101;

void setup() {

  pinMode(dataPin, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(SRCLR, OUTPUT);
  pinMode(SRCLK, OUTPUT);
  
 }




void loop() {


  digitalWrite(RCLK, LOW);
  digitalWrite(SRCLR, HIGH);
  digitalWrite(SRCLK, LOW);
  
  delay(1000);
  
  shiftOut(dataPin, SRCLK, LSBFIRST, data);
    
  delay(1000);
  
  shiftOut(dataPin, SRCLK, LSBFIRST, data2);
   
    
  
  digitalWrite(RCLK, HIGH);  


 }

You need this after each shiftOut to move the data from the input register to the output register:

digitalWrite(RCLK, LOW); digitalWrite(RCLK, HIGH); // output changes on this rising edge.