Using a Shift Annals to Control a Bunch of LEDs

Shift registers are very useful tools; using a few pins connected to a shift register, we tin increment the number of output data pins that are available to us.

In this experiment, we'll be using a shift register to command eight LEDs, but we'll only be using three pins from the ATmega.

Shift Register

74HC595N Shift Register IC

A shift register is an external integrated circuit (IC) that can be used to expand the number of output pins available to us. Essentially they let you turn serial input from a single pin (1 chip afterward the other) into multiple parallel output signals (all at once on carve up lines).

Block diagram

The shift register used in your kit is the popular 74HC595. Information technology has 8 output lines which allows you to dispense and employ bytes for output in your code.

Overview

So how does this piece of work? The IC is made up of ii registers, units of memory that can hold upward several binary values in society (8 for the IC in your kit). They are:

  • The shift annals, which holds eight values before they are written to the output pins. Values can be "shifted" through this register from ane position to the next, starting at position "A" to position "H".
  • The storage register, which takes values from the shift register and sends them to the data output lines, labelled QA to QH. For example, a logical 1 in position "C" of the storage register would create a High betoken on QC.

There are three pins on the IC that nosotros use to control it with the Omega. Two of these pins are clocks: special inputs that trigger the IC to do something when they receive a signal that changes from LOW to HIGH (likewise known as a pulse or a ascent edge).

Pin Proper name Purpose
SER Serial data pin This is the serial data input line. When we pulse the serial clock (SRCLK), the betoken on this line is stored in the 1st position ("A") of the shift register.
SRCLK Serial clock When pulsed, shifts each value in the shift register forwards by 1 position, so loads the value from the SER pin into position "A". Note that this does not change the signals on the output lines until you pulse the register clock (RCLK).
RCLK Register clock, or "latch pivot" When pulsed, updates the storage register with new values from the shift register, sending a new set of signals to the 8 output pins. This happens so quickly that they all seem to change simultaneously!

Bit Order

Keep in mind that the get-go value you ship the shift annals will exist shifted towards the terminal output pin as you send it more data.

Let'south say we want to send the following bits: 10101010. Intuitively, information technology seems easiest to send each bit in the number from left to correct equally if information technology were a string. In Python, this would look something similar:

However, sending it this way ways that after we've sent all eight, the 1st bit would actually be shifted all the way to the last output (QH), the 2d bit would be shifted to the 2nd-to-last output (QG), and and then on until everything is reversed! This mode of shifting values out is known as most-pregnant bit (MSB) outset. If we used this method in our shift register class, we would have to wire everything up backwards, and this could make information technology confusing to assemble or debug circuits.

Nosotros tin can get around this result past sending the rightmost, or least significant flake (LSB), get-go. We can modify the above code into something like this:

Pinout Diagram

The pinout diagram for the 74HC595 is shown below:

595-pinout

On the correct side of the chip, y'all tin can run into the three control pins described to a higher place, as well as the first output (QA). On the left side, you can see the other seven outputs (QB - QH).

Controlling a Shift Register

So how can this let us command multiple outputs with i data pivot? Well, allow's say nosotros take 8 LEDs hooked upwards to the data lines, and we want to plow on the 2d, quaternary, and the eighth LEDs like and so:

LED Data Line Desired Value
1 QA LOW
2 QB High
3 QC Depression
4 QD Loftier
v QE LOW
6 QF LOW
7 QG Low
8 QH HIGH

First, we'll clear out the register so all LEDs are off by writing eight 0'south to the shift register, and so pulsing the latch pin to write the outputs to the data lines. This is done past setting and holding SER LOW, and then pulsing SRCLK 8 times, so pulsing RCLK once.

Then, using the LSB method, nosotros will reverse the bytestring to get 10001010. For each of these values:

  1. Gear up SER to the specified value (HIGH or LOW).
  2. Pulse SRCLK from LOW to Loftier to shift the value of SER into the shift register.

We repeat the 2 steps in a higher place (for example, by using a loop) until all 8 values have been shifted in. Then pulse the RCLK pin to write these values to the storage register and information lines, which turns on the LEDs!

In this way, we can command upwardly to 8 different outputs with only 3 GPIOs. This is an incredibly powerful technique that you can use to piece of work with many components at once.

Here's the diagram from earlier to summarize what nosotros've just described.

Block diagram

Daisy-Chaining

Shift registers can also be continued in series to each other to extend the number of information lines that can exist controlled at once. We can do this by using the QH' pin, which is connected inside the shift register to the last output QH.

Merely connect the SER pin of one shift annals to the QH' pin on another, and connect their SRCLK and RCLK pins together. That manner, when you pulse SRCLK, the second chip will read from the final output of the 1st, and when you pulse RCLK, both fries will update their output lines. This is great because this does not require whatsoever additional GPIOs from the Omega!

You lot've now merely created a sixteen-bit shift annals, and you lot tin extend the chain further past calculation more chips in the same way as higher up. This is known as daisy-chaining.

Detailed Specifications

if you're curious nigh the clock cycle timings or other information virtually the IC, y'all can refer to the datasheet for the SN74HC595 shift register. The clock cycle timing diagram can be plant on page eight.

Building the Circuit

For this experiment, nosotros volition send a viii bits (a byte) serially from the ATmega to the shift register. When the latch pin of the shift register is set Depression, the shift register will use the stored 8 bits to set its 8 output pins accordingly. We will adhere one LED to each of the 8 output pins and make them light upward like Knight Passenger'due south KITT. By the ability of the shift register, we tin can do this using but three ATmega pins!

This is the diagram of the circuit we'll be building:

Circuit diagram of the shift register experiment

What You lot'll Demand

Ready the post-obit components from your kit:

  • Omega plugged into Arduino Dock
  • USB Micro-B cable for ability
  • Breadboard
  • Jumper wires
  • 8x 200Ω Resistor
  • 8x LED color of your selection!
  • 1x 74HC595 Shift register

Hooking upwards the Components

The IC should exist plugged in across the channel of your breadboard (the slot running down the middle separating the abcde columns from the fghij columns). If you lot don't exercise this you will curt out the pins across your IC. You may need to bend the pins just a bit in lodge to get it to fit.

Lets have a look at how the 16 pins of the 74HC595 shift register scrap are defined. We'll be referring to each pin by the numbers provided in the diagram above. When plugged in with the messages beingness right-side upwards, the bottow row of pins are pivot 1 to 8 going from left to right. The top row of pins are pin 9 to 16 going from correct to left.

Note: Your IC will have a semi-circle indentation that indicates "upwards". Brand sure that yous plug information technology in properly and so you know which pins are where.

Here's the steps to become at that place:

  1. Connecting your shift register to the breadboard
  • Showtime by plugging in your shift register beyond the channel so that the each pin has its ain row.
  • Connect the supply voltage pin (Vcc) and the master reset pivot (MR) on the IC to the Vcc rail on the breadboard
  • Connect the GND pin and output enable pin (OE) to the GND track on the breadboard

Shift register wired

The jumpers at the top volition be wired to the LEDs somewhen.

  1. Connecting your LEDs
    • Connect the anodes of the eight LED each to one of the eight output pins of the 74HC595 - pin fifteen and pin 1 to 7, respective to Q0 to Q7. Identify the LEDs left to correct in the following pin order: Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7.
  • Attach eight 100Ω current limiting resistors from cathodes of the LEDs to the GND runway on the breadboard.

  1. Connecting your Arduino Dock
  • Connect the GND pin on the Dock to the GND rail on the breadboard.
  • Connect Arduino Dock digital pivot 4 to DS on the shift register - this is where our input is sent.
  • Connect Arduino Dock digital pin five to STCP on the shift annals.
  • Connect Arduino Dock digital pin vi to SHCP on the shift register.
  • Connect the Vcc rail to a 5V pivot on the Arduino Dock

Circuit complete, ready to code!

Writing the Lawmaking

                              #define NUM_LEDS     viii                                // duration to pause                int                delayTime =                100;                 // the pin connected to the latch pin, RCLK (pin 12 of the shift register)                //    setting the latch LOW volition transport the 8 bits in storage to the output pins                int                latchPin =                5;                // the pin connected to the clock pivot, SRCLK (pin 11 of the shift register)                int                clockPin =                6;                // the pivot continued to the serial data pin, SER (pin 14 of the shift register)                int                dataPin =                4;                  // This lawmaking runs once when the program starts, and no more                void                setup() {                 // initialize all the pins connected to the shift annals equally outputs                  pinMode(latchPin, OUTPUT);   pinMode(dataPin, OUTPUT);     pinMode(clockPin, OUTPUT); }                 // function which sends the stored byte to the output pins past setting the latch pin Low                void                updateShiftRegister(byte storageByte) {                 // set the latch pivot LOW                  digitalWrite(latchPin, Low);                  // send the storage byte to the shift register with the LSB start                                //     since the latch is Low, set the viii output pins based on the stored 8 $.25 and in plow low-cal the correct LED                  shiftOut(dataPin, clockPin, LSBFIRST, storageByte);                  // prepare the latch pin Loftier again                  digitalWrite(latchPin, Loftier); }                 // The code in here will run continuously until we turn off the Arduino Dock                void                loop() {                 // the byte (8 bits) to be stored in the shift register                                //    initialize to 00000001, representing the first LED on                  byte storageByte =                0x01;                  // create the effect of having the light travel to the left                                for                (int                i =                0; i < NUM_LEDS-one; i++)   {                 // transport the 8 bits to the shift register and set latch LOW                    updateShiftRegister(storageByte);                  // bitwise shift to the left by 1 bit                                //    the MSB volition disappear and a 0 will be shifted in for the LSB                                //  ex. 10000001 to 00000010                    storageByte = storageByte <<                i;                  // wait before moving on to the next LED to heighten the animation                    delay(delayTime);      }                  // create the effect of having the light travel in the opposite management                                for                (int                i =                0; i < NUM_LEDS-1; i++)   {                 // send the eight bits to the shift annals and set latch Low                    updateShiftRegister(storageByte);                  // bitwise shift to the right by 1 flake                                //    the LSB volition disappear and a 0 will exist shifted in for the MSB                                //     i.due east. 10000000 to 01000000                    storageByte = storageByte >>                1;                  // wait before moving on to the adjacent LED to enhance the animation                    delay(delayTime);      } }            

What to Expect

The eight LEDs will light upwards like KITT from Knight Rider. The first LEDs will turn on, then the next will plow on and the previous 1 will plow off. This will repeat for all the LEDs in a loop from left to right and then from correct to left. Only i LED should be lit upward at a time.

Information technology should await a little like this:

See, simply like KITT:

Knight Industries Two Thousand

A Closer Expect at the Code

Nosotros are but using three Arduino Dock pins to control eight LEDs by taking advantage of the shift annals. Lets brainstorm by declaring the three pin variables (latchPin, clockPin and dataPin) and initializing the three pins as output in setup().

Each time we want to light up a different LED, nosotros update the shift register to send the shift register new signals for each LED.

Updating the Shift Register

We control the shift register using a single function updateShiftRegister. The first matter it does is to set the latch pivot low using a call to digitalWrite():

Then we use a function that'due south included in the default Arduino libraries, shiftOut(), to send the byte:

This function does the following deportment:

  1. Sets the SER pin to either High or Low co-ordinate to the bit of the byte you desire to send
  2. Sets the clock pivot Loftier, then Depression to load the SER bit into the shift register
  3. Repeats the in a higher place ii steps until all bits in the byte have been sent

The function takes an argument, bitOrder, which determines whether information technology sends the correct-most (to the lowest degree significant) bit, or the left-almost (most pregnant) bit first. Here we've decided to ship it least significant scrap first (LSBFIRST) and so that our wiring order can friction match the order of the shift register'south outputs.

Once the byte has been sent, we set the latch pin Loftier to trigger the clock that updates the shift register'southward outputs. This is done with another call to digitalWrite():

You'll notice we left in a slight delay earlier every update. This is because if nosotros let it run as fast the CPU can go,information technology will be too fast for united states to see. Instead, the lights will announced every bit if they were all on at the same time. The shift annals tin can accurately update at 100MHz - much faster than our optics tin can perceive! In lodge to really see the event, nosotros irksome information technology down by adding the filibuster.