Arduinos & Shift Registers

13 years ago | 17 September, 2011 | X minute read.

Arduino

originally published on wordpress.com

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:

  • 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). One is called MAX7219.
  • Versions to increase inputs as well!.

I bought a few 74HC595 shift registers on eBayand 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:

Breadboarded Circuit

This is the arduino sketch I used is below.

// 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

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!

74HC595, ARDUINO, BASIC, BEGINNER, DIGITAL LOGIC, ELECTRONICS, GPIO, LEARNING, LED, MAX7219, MICROCONTROLLER, SHIFT REGISTER