Shifting Bits With Arduino

I’ve recently started back into electronics. I took an electronics course back in high school, but I’ve dealt mostly with software since then.
I’ve had a Freeduino-SB sitting around since last year, until I finally soldered on the through hole components a few weeks ago. The Freeduino is an Arduino clone made by Solarbotics.

So far, I’ve managed to power a dual digit 7 segment display using two 74HC595 shift registers. These can be chained in series to provide more outputs from the same three output pins from the Arduino.

Arduino Countdown

Here’s a video of the code in action. The initial state of the 7 segment display is randomly determined, as one of the pins on the shift registers is connected directly to power, instead of an additional output pin.

Here’s the rather basic code I’m currently using:

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;

long count = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  //Reset display
  byte bits = 0;
  writeDigit(bits, bits);
}

byte getByteForDigit(int digit){
  byte bit = 0;
  switch(digit){
      case 1:
        bit = B110;
        break;
      case 2:
        bit = B1011011;
        break;
      case 3:
        bit = B1001111;
        break;
      case 4:
        bit = B1100110;
        break;
      case 5:
        bit = B1101101;
        break;
      case 6:
        bit = B1111101;
        break;
      case 7:
        bit = B111;
        break;
      case 8:
        bit = B1111111;
        break;
      case 9:
        bit = B1100111;
        break;
      case 0:
        bit = B111111;
        break;
      default:      
        break;  
    }
    return bit;
}

void loop() {
    byte bits = 0;
    byte bits2 = 0;
    bits = getByteForDigit((100-count) % 10);
    bits2 = getByteForDigit((100-count) / 10);
    writeDigit(bits, bits2);
    count = (count + 1) % 100;
    delay(250);
}

void writeDigit(byte bits, byte bits2){

  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, bits);
  shiftOut(dataPin, clockPin, MSBFIRST, bits2);
  digitalWrite(latchPin, HIGH);
}

2 thoughts on “Shifting Bits With Arduino”

  1. I know a lot of homebrewers who use arduino’s to control temperature, these all electric guys get quite complicated, controlling their mash, boil and chillers through the board.

    1. It’s quite the powerful microcontroller. I’m really liking the platform. It’s easy to develop for. The hardware is relatively easy to work with. My grade 12 electronics class is gradually coming back to me. The more complicated logic circuits are taking a bit longer to understand, but it’s a fun challenge.
      I have several interesting ideas for longer term projects.

Leave a Reply