Arduinos & Shift Registers

So 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.

At the beginning of this semester, 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.

So 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!

Now for the shift registers:

One issue I’ve had with microprocessors in my time 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:
– From 3 Arduino digital output pins, you can have upwards of 8 digital outputs (you can chain the registers together for more than 8 outputs).
– High refresh rate
– Direct LED driver registers are available (with built in resistors).
– Versions to increase inputs as well!

So I bought a few shift registers on eBay and had a play.

The Arduino tutorial I used can be found here.
There is an animation on shift registers here.
If you need a visual demonstration of the concept this link may help.

This is it:

These pictures may help further in for the hardware side.

This is the code I **edited from the tutorial code** for the youtube video.

// 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);
  }
}

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

Advertisements

One response to “Arduinos & Shift Registers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s