Arduinos & Shift Registers

- originally on wordpress.com

Arduino

I sometimes work on small D-I-Y electronic projects and have used PICAXE chips in the past when I’ve needed some microprocessor power. While PICAXE chips are very powerful and have a particularly nice BASIC based programming language; they are expensive, they are not open-source and, whilst they have a loyal following, do not have the large number of followers all over the world that a certain other microprocessor does.

As part of my studies, I started a unit on microprocessors and whilst I knew what an Arduino was, I’d never seen one. We were set a programming task on some atmega16 (writing a snake game which displayed in windows terminal over serial), and at the first tutorial my tutor strongly recommended buying an Arduino rather than shell out on the ~$140 STK500 we were using in class. I did this.

After playing with the Arduino for a while now I have to say I’m hooked. EVERYTHING is open-source including schematics for the boards, the bootloader for the chip, and all of the the software. There is also a great website with tutorials and a huge community forum. Get on board!

Shift Registers

One issue I’ve had with microprocessors is lack of digital output pins (for led displays and the like). Friends at uni used to tell me “use a shift register” (you know who you are) and I said “yes I know” but I never understood the concept and never bothered to try.

Until recently.

Some of the things I like about shift registers:

I bought a few 74HC595 shift registers on eBay and had a play.
The "ShiftOut" Arduino tutorial I learned from can be found here.

This is the circuit I built up to try it out:

Breadboard
Breadboarded Circuit

This is the arduino sketch I used is below.

shift_reg_sketch.ino
// Set Pins
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

// create variables
int ispeed;
int output;
boolean up;

void setup() {
  // initialise pin states
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // initialise variable states
  up = true;
  output = 1;
  ispeed = 40;

  // scanner style led scrolling
  for (int j = 0; j < 253; j++){
    // count from 0 to 255 and display the number
    // on the LEDs
    // take the latchPin low so
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
      shiftOut(dataPin, clockPin, MSBFIRST, output);
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
      if (up){output = output << 1;}
      else {output = output >> 1;}
      if ((output == 0b10000000) || (output == 0b00000001)){up = !up;}
      delay(ispeed);
  }

  // refresh rate example
  for (int j = 0; j < 200; j++){
    // count from 0 to 255 and display the number
    // on the LEDs
    output = 1;
    for (int i = 0; i < 8; i++) {
      // take the latchPin low so
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
      shiftOut(dataPin, clockPin, MSBFIRST, output);
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
      output = output << 1;
      delay(ispeed);

    }
    if(ispeed > 1){ispeed = ispeed-1;}
  }

  // binary counter
  ispeed = 250;
  for (output = 0; output < 256; output++){
    // take the latchPin low so
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
      shiftOut(dataPin, clockPin, MSBFIRST, output);
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
      delay(ispeed);
  }
}

This is how it works:

I made this to try to illustrate what is going on:

Shift Register Animation
Shift Register Animation

Anyway, my friend at uni was right. Shift registers ARE the way to go if you need a large number of digital outputs. Go and have a look!

Keywords: Arduino, Shift Register, LED, Electronics, Beginner, Basic, GPIO, Microcontroller, Digital Logic, 74HC595, MAX7219, DIY, Learning

LIKE IT?: BUY ME A COFFEE!LIKE IT?BUY ME A COFFEE!