Single Digit Display
Fun projects from beginner to advanced

 
Single Digit Display

Single Digit Display

Want to know how to control a seven-segment display? Here's a fun little project to help you learn how the 74HC595 Shift Register can control it.

This project explains how you can control a seven segment display using a 74HC595 shift register. The advantage of doing this is it uses less pins on your arduino board, which means more pins for inputs/outputs! Shift registers are used in many applications and it is great to know how to use them.
This project needs:
* Arduino Micro(or Arduino Uno) x 1
* Breadboard x 1
* Assorted jumper cables
* Seven Segment Display (Common Anode) x 1
* 74HC595 Shift register x 1
* Resistor 560 ohm x 8

Connecting it up
Interative window: move around and zoom in and out with your mouse

Electrical Diagram

Simple Distance Sensor Electrical Diagram


Programming
You can copy and paste this code into the Arduino IDE or write it yourself.

//input pins on 74HC595
#define DATA 7
#define CLK 8
#define LATCH 9
#define CLEAR 5 

/*
     a=1
     -----
    |     |
f=32|     | b=2
    |g=64 |
     -----
    |     |
e=16|     | c=4
    |     |
     -----   +h=128
       d=8
*/

//startup spin 
const byte spin[6]={254,253,251,247,239,223};

//74HC595 numbers output table (common anode display) = Digits: 0 to 9 (no decimal point)
const byte number[10]={
  192,  //0
  249,  //1
  164,  //2
  176,  //3
  153,  //4
  146,  //5
  130,  //6
  248,  //7
  128,  //8
  152,  //9
};

//74HC595 Alpha Characters output table = {A,B,C,D,E,F,G,DP}.
const byte alpha[26]{
  136,  //A
  131,  //b
  198,  //C
  161,  //d
  134,  //E
  142,  //F
  144,  //g
  137,  //H
  207,  //I
  225,  //J
  137,  //K
  199,  //L
  234,  //M
  171,  //n
  192,  //O
  140,  //P
  152,  //q
  175,  //r
  146,  //S
  135,  //t
  193,  //U
  227,  //v
  213,  //W
  137,  //X
  145,  //y
  164,  //Z
};

//======================================================  
void setup() {
  Serial.begin(9600);
  //initilize pins as outputs
  pinMode(DATA, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLEAR, OUTPUT);
  
  digitalWrite(DATA,LOW);
  digitalWrite(CLK,LOW);
  digitalWrite(LATCH,LOW);
  digitalWrite(CLEAR,HIGH);

  delay(15);
  startup(4);
}

void loop() {
  
  for(int i = 0; i<10; i++){
      digitalWrite(LATCH,LOW);
      shiftOut(DATA, CLK, LSBFIRST,number[i]);
      digitalWrite(LATCH,HIGH);
    Serial.print(i);
       delay(1000);              
  }
  for(int i = 0; i<26; i++){
      digitalWrite(LATCH,LOW);
      shiftOut(DATA, CLK, LSBFIRST,alpha[i]);
      digitalWrite(LATCH,HIGH);
    Serial.print(i);
      delay(1000);              
  }
}

//===================================================
void startup(int repeat){
 for(int a = 0; a<repeat; a++){
  for(int i = 0; i<6; i++){
      digitalWrite(LATCH,LOW);
      shiftOut(DATA, CLK, LSBFIRST,spin[i]);
      digitalWrite(LATCH,HIGH);
      delay(100);
  }          
 }
 for(int i = 0; i<repeat; i++){
      digitalWrite(LATCH,LOW);
      shiftOut(DATA, CLK, LSBFIRST,number[0]);
      digitalWrite(LATCH,HIGH);
      delay(150);
      digitalWrite(LATCH,LOW);
      shiftOut(DATA, CLK, LSBFIRST,255);
      digitalWrite(LATCH,HIGH);
      delay(150);
  }
}


Technical stuff
The 74HC595 shift register works like a switch block. You can tell it which of the 8 bits to be a 1 or 0 (on or off). The shift register is controlled by any 3 digital pins on the Arduino board.
The seven segment display has 8 LED's (7 LED's for the number, 1 LED for the decimal point). This project uses a "common anode" display, meaning each segment has to be grounded to be illuminated because they all have a common voltage supply. A "common cathode" display has a common ground and each segment needs to have a voltage supply.

Lets now look at the Seven Segment Display

Common Anode Display

The seven segment display has 8 LEDs (digit + decimal point). To illuminate each segment you need to power each segment individually. To do this, first you need to connect the common pin to Ground (Common Cathode), or the Voltage Supply (Common Anode). To complete the circuit you connect the power to one of the segment pins (common cathode display), this illuminates one segment on the display. For a common Anode display connect each segment pin to ground to illuminate.
When programming the shift register to display segments on a common anode display, you need to write a "0" for on an "1" for off.
For a common cathode display it is the opposite, a "1" for on and "0" for off.
Make sure you know which type you have.

Common Cathode Display

Lets now look at the 74HC595 Shift Register:
See "Simple Shift Register Circuit" for a more detailed explination of the 74HC595 Shift Register.

Lets put it all together:
So now we have 3 digital OUTPUTS on the Arduino board to control 8 "switches" on the shift register to control 8 LED's on the seven segment display.
To use the shift register we use the "shiftOut()" function this shifts 1 byte of information (8-bits) into a shift register. The "shiftOut()" function uses the Data pin (DS) and Clock pin (SHCP) of the 74HC595 to move the 8-bits into the register. To display that information we move it into the storage register by turning the "Latch" pin (STCP) from 0 to 1 (Low to High).

How binary works:
See binary tutorial

Project complete!
Now you can display a number on a seven segment display for any project you want!

PCB - Arduino Micro

References

* Shift Out Tutorial: https://www.arduino.cc/en/Tutorial/ShiftOut

 
     

© Copyright intRobotics. All Rights Reserved.