Instructables

How to use a 74HC595 Shift Register with a AVR ATtiny13

Picture of How to use a 74HC595 Shift Register with a AVR ATtiny13
If you have been playing with microcontrollers and electronics then you have likely seen LED dot matrix displays and other projects that use shift registers like 7-segment displays and more. This instructable goes over a quick intro to the 74HC595 8-Bit Serail to Parallel Shift Register with Latch and how to interface it to an AVR ATtiny13 to flash some LEDs.

What you need:
1 - Breadboard
1 - Power Source - 7805 or some other 5v source
1 - ATtiny13 and a way to program it ( I'm using the AVRISP mkII with AVRStudio 4.17 w/ WinAVR)
1 - 74HC595 Shift Register
1 - 100uF cap
1 - 0.1uF cap
1 - 10K resistor
8 - LEDs
8 - 470ohm resistors
~15 - hookup wires


What is a Shift Register?

Serial to Parallel Shift Registers are ICs that allow "serial" data to be clocked in on 1 pin and stack up in a register which is output on 8 parallel pins (in the case of a 8-bit shift register). As data is clocked in, it shifts all the other bits over one position with the end one falling off. Common shift registers are 74HC164 and 74HC595.
There's a couple of good online tutorials about these with animations.

www.eelab.usyd.edu.au/digital_tutorial/part2/register01.html

The 595 has another feature which is cool. It's called a latch. It allows the outputs to show the current bits which have been latched in the Latch Register while new data is shifted into the Shift Register. Then toggle the latch pin and the new data is copied into the Latch Register.

Here's a link to the 74HC595 Datasheet:

www.onsemi.com/pub_link/Collateral/MC74HC595A-D.PDF


Check Out a Youtube Video of the Circuit in Action


 
Remove these adsRemove these ads by Signing Up

Step 1: Get Familiar with the 595

Picture of Get Familiar with the 595
The 74HC595 is a pretty cool little package. We are going to hook it up to shift in a pattern of leds

The pin out  goes like this:

Pin 1 - Output B - source for LED (+)
Pin 2 - Output C - source for LED (+)
Pin 3 - Output D - source for LED (+)
Pin 4 - Output E - source for LED (+)
Pin 5 - Output F - source for LED (+)
Pin 6 - Output G - source for LED (+)
Pin 7 - Output H - source for LED (+)
Pin 8 - GND
Pin 9 - Serial Output - Carries Value from Output H to Data Pin (pin 14) of another 74HC595 to create a chain of SR's.
Pin 10 - Shift Register RESET -  Active LOW clears data in Shift Register, Latch Register is no affected.
Pin 11 - Shift Clock - LOW to HI transition shifts in data (0 or 1) from the Data Pin (pin 14). This can be toggled very fast in applications where you want a fast refresh rate like displays. I think you can toggle it on the order of 1000Hz +
Pin 12 - Latch Clock - LOW to HI transition latches the SR data to the outputs - The latch in our case will be triggered after every 8th bit is shifted in.
Pin 13 - Output Enable - Active LOW - Allows data in the Latch Register to show on the display. Typically this will be kept low, so you could just hard-wire it, but I have the AVR setup to control it.
Pin 14 - Serial Data In (Data Pin) - Don't be scared by the word "serial". It just means digital 1 or 0 on a single pin. This is where the AVR will feed in the bits
Pin 15 - Output A - source for LED (+)  - It's a weird spot for it but it works
Pin 16 - VCC 2.0-7.0 VDC

Step 2: Hook it up on the breadboard

Picture of Hook it up on the breadboard
595_schematic.jpg
Setting up the Circuit is pretty simple. It will take you about 10 minutes.

Place the ATiny13 and the 74HC595 near each other.
Hook up VCC and GND on both.
Put the 10K resistor from the ATtiny13 RESET pin to VCC
Put the LEDs in a row with the 470ohm resistors on the LED cathodes to GND.
Run Hook up wires from the LED anodes to the 595 outputs in order A-H.

Here's the important part:
Connect the ATtiny13 to the 74HC595 like this:
PB0 to Data Pin (pin14)
PB1 to Output Enable (pin13)
PB2 to Latch Clock (pin12)
PB3 to Shift Clock (pin11)
PB4 to Reset (pin10)

Also connect the Data Output (pin9) to GND since we aren't using it.

Take alook at the provided schematic. It's not beautiful but it will get the point across better than words.

The power supply shown in the schematic is pretty standard. You probably already have it setup. Just put the 100uF and 0.1uF caps across VCC to GND to smooth out any ripples.

Once you have it boarded up, hook up your programmer and go the the next step.

Step 3: Review the Code and Program your AVR

The ATtiny13 is a small MCU (8 pin), but you can increase it's outputs using a Shift Register like we are going to do.

The program I wrote is relatively simple, but it have some nice features which may make the code look more complicated but read through it and you should be OK.

I'm not going to go over the code here because I made sure there are good comments for each line of code.

The code is written in C using AVRStudio4.17 with WinAVR 20090313 GCC C-compiler.
If you are using another compiler like IAR, Codevision, AVRdude, etc you can probably adapt the code or atleast review it and learn something.

The programming may not be the most efficient, but it works for this demonstration.

Download the ZIP file for: source code, schematic, and picture

Please post comment and questions and I will do my best to answer them.

Thanks for looking. Check out my other AVR Instructables.

contrechoc made it!7 months ago

Thank you for the very elaborate and accurate code wth all the comments...

(I used an attiny85...same code.)

First I made a breadboard test. Then I made the LED (smd) in a circle. My idea was to make an interactive etching. I added a bit of code for an LDR. If it sees light change, it reverses the way the light is turning around the circle.

I had to skip the ENABLE PIN, made this HIGH on the 74HC595 and had a free adc PIN on the Attiny85.

All works on a 3V lithium battery.

Normally I use the LED matrix code with interrupts, with this shift register you have less wires...

I'll make another circle for use in a sweater!

shift2.jpgshift1.jpgshift3.jpg
FreakErn8 months ago
Where can I download the code?
gussmith2 years ago
My Bad! I just plugged in another SN74HC595N and now I see led8 blink too.
Just a bad pin.
gussmith2 years ago
I use an ATTINY85 and a SN74HC595N with the exact wiring and example code.
When running
I put a delay in the loop of option 1 and only see the leds 1 to 7 blink but not led 8.

Can't figure out what I'm doing wrong.
opticalaser2 years ago
hi my friend first thank you a lot
but my compiler is Basic may be you can tech-me how i can write program
by basic

thanks

lefam3 years ago
what is the total amount of current that the 595 shift register can sink and source?
roznerd (author)  lefam3 years ago
Thanks for the question. The 595 is able to source or sink 20mA on each output, however the catch is that the total current for the chip is 70mA, so you have to consider that. Alot of guys get away with running dot matrix leds and stuff directly off the 595 because they are multiplexing and the average current works out to be be less than the max. I think the 595 has some internal over-current protection on the outputs too.

There's a good discussion about this at this link:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248563533

I hope this helps. Thanks
lefam roznerd3 years ago
thanks. it helped a lot...
arthur927103 years ago
Hi, this code is very helpful. I have a question, it uses a for loop to fill the register from an array, how can I send a binary or hex code. I am using this to build a 10 x 10 matrix.
Thanks!
roznerd (author)  arthur927103 years ago
I'm glad you found this tutorial helpful. The for loop and array was used to make it easy to show, but as you know, its not really practical for most useful applications.

I understand what you are getting at with the application for a dot matrix. You want to individually clock in the bits of an 8 bit byte. This could be in bin or hex, it makes no difference.

I have done this before for using a 595 to drive a 4 digit multiplexed 7 segment display. Here's the excerpt of code I used to take the byte (in hex in my case) associated with the digit I wanted to display and get it clocked into the 595 without using an array.

It uses whats called a bitmask. I describe it in the comments of the code below. I tried to be very thorough because I know how hard it is looking at someone elses code and trying to figure out what's going on. Hopefully this is helpful for you. Thanks for the feedback - roznerd


------------------------------------------------------------------------------------------------
for (k=0; k<8; k++)
// you have to loop 8 times to clock in all 8 bits in the byte to display, makes sense.
{

//Your byte will replace the "digit" byte. if the 7th bit of the byte "digit" is 1 then set Data pin high for 595 and toggle clock to shift it in. If the 7th bit of the byte "digit" is not 1 (i.e. 0) then set Data pin low and toggle the 595 clock pin.

if (digit & 0x80)

// this is the bit mask, by AND-ing the 8-bit byte you want to display with a "mask" that overlays all bits except the 7th bit (0x80 = 10000000) you get to differentiate whether the 7th bit is a 1 or 0 by the outcome of the AND. IF 1 & 1 = 1 (data pin high) ELSE 0 & 1 = 0 (data pin low).
Then you shift the bits in your byte to the left << 1 position and compare again.

{
Data_H; // defined in header to set data pin high
Shift_Clk_H; // defined in header to set shift pin high
Shift_Clk_L; // defined in header to set shift pin low

}
else
{
Data_L; // defined in header to set data pin low
Shift_Clk_H;
Shift_Clk_L;
}

digit = digit << 1; // shift the bits in your byte to the left 1 position
} // end for

// after all 8 bits in the byte "digit" are clocked in, toggle the latch to show on the display
Latch_Clk_H;
Latch_Clk_L;

Thanks! This will help me a lot!
SuperFlyGT4 years ago
Thanks for the quick tutorial. I was looking at using this part for a personal project and this helped to clear up some questions I had.

Out of curiosity, why ground Pin 9 (serial data out / QH')? If the output is of QH is high (and by extension Pin 9), you're shorting a high output to ground.

Another version of the datasheet (for HC595, not HC595A, http://focus.ti.com/lit/ds/symlink/sn74hc595.pdf) lists a clamping current of +/- 20 mA max which I suspect is saving the chip from blowing. It would be safer to either leave this output floating in this application or put a resistor to ground.
roznerd (author)  SuperFlyGT4 years ago
I'm glad the tutorial was helpful for you. In retrospect, I would have left the Pin 9 floating or pulled it down with a 10K resistor. I think connecting Pin 9 to GND for breadboarding is probably ok, since it's probably internally clamped to prevent over current, but in a design, I would handle it differently. Good catch, thanks
tissit4 years ago
avrdude is not a compiler.
ju1ce4 years ago
Thanks for this tutorial - I really like your clear way of presenting things.