Sunday, 13 November 2011

Driving a Seven Segment!

I recently had an idea....I have lots of those sometimes!

I have decided to make a set of Chess timing clocks for a friend of mine.  We play fairly often but don't have any timers....He can sometimes take forever on his moves, particularly when he is under pressure ;)  We have been using an Android app but it isn't quite the same.  As Christmas is coming I thought it would be nice to make him a set of chess clocks.

To that end I bought ten common cathode seven segment displays off Ebay.  They came from dannell.4you and cost a very reasonable £5.52.  They were delivered pretty quickly and they all work!  




It has been a long time since I played with seven segment displays....Not since my college days and that was with 74 series logic!  Anyway I decided to drive them with my arduino, although any microprocessor will work as long as it has eight pins that can be set to outputs and they can source enough current to drive LEDS.  There are a fair few arduino tutorials on-line available but I thought I would add my own efforts to the mix.

Some things to understand about seven segment displays:
Seven segments are essentially a set of LEDS arranged in a simple way so that when driven correctly they display a character, either 0 to 9 or A to F.  They were more popular in years gone by as they were cheaper and more readily available than liquid crystal displays.  The other reason for using them is that they can easily be seen from a distance unlike an LCD.
Unfortunately my seven segments didn't arrive with any information at all....No problems I say, we can work this out!  Suffice to say it took me a little longer than I though it would but here is how I found out the internal connections.  I knew the device was common cathode.  Seven Segment LED displays come in two flavours, common cathode and common anode.  Cathode being the negative connection on a diode and anode being the positive.  I will go into diodes in another post if needed.  That's enough for now....expect more later!

So I have a common cathode seven segment LED display, I just didn't know which pins were which.  I turned the device over and counted the number of pins available.  Ten pins all present and correct!  As there  are seven segments and a dot I can guess that there are eight LEDS inside the display.  All I have to do now is find out which pins connect to what.  Two of the connections will be to the cathode and should be directly connected together.  Grabbing my multimeter, I switched it to its ohm meter (resistance) measurement setting on the lowest division.  I then using the red and black probe leads went through every pin until I found which ones were connected.  Helpfully my meter on lowest ohm measurement setting bleeps when the leads are connected together (have continuity).  It is so useful, some meters don't have this feature on ohms and or it only bleeps on the diode setting.  If this is the case with yours use low ohms measurement anyway as the diode tester will pick up the LEDS and not the common connections.  You will just have to keep an eye on the display until it registers that the wires are connected together (0.05R or similar).

I found that pins 3 and 10 are common (connected together).  All the other pins must be the diodes.  I then set the multimeter to diode tester and put the black lead of my multimeter on pin 3 or pin 10 and the red probe lead on all of the other pins in turn.  Each of the separate segments will light up if it is working.  The meter will also measure the voltage drop for each segment.  On the Seven segment display I had the forward voltage drop was 1.8 volts.  Useful to know for when we want to design a circuit.

    
So now that we know how all the pins are connected we can set up a simple driving circuit using the arduino.  I have my arduino set up with a couple of breadboards to make it a bit easier for me to do development and stuff.  I recommend everyone gets a breadboard for doing electronics hobbyist stuff.  You can quickly build up and check circuits and then take them apart once you are finished.  I also use the wingshield addon that is available from loads of good online vendors.  Google is your friend!  

Put the seven segment into the breadboard make sure it is oriented so that none of the pins are connected 
together.  There is a separating channel down the middle of the breadboard for this.  Next connect pins 3 and 10 on the Seven Segment Display to 0V.  Next connect the following pins on the arduino to the Seven Segment Display:

Arduino Pin 2 to Seven Segment Display pin 7 (A)
Arduino Pin 3 to Seven Segment Display pin 6 (B)
Arduino Pin 4 to Seven Segment Display pin 4 (C)
Arduino Pin 5 to Seven Segment Display pin 2 (D)
Arduino Pin 6 to Seven Segment Display pin 1 (E)
Arduino Pin 7 to Seven Segment Display pin 9 (F)
Arduino Pin 8 to Seven Segment Display pin 8 (G)
Arduino Pin 9 to Seven Segment Display pin 5 (dot)

It really helps to have breadboard wires for doing this!  Anyway, once you have made all of the connections load up the arduino ide and paste the code below into the sketch space.  What the code does is define a 2 dimensional byte array which has eighty cells in an eight by ten matrix.  We then set the cells in the array to store the values required to display the numbers from zero to nine.

We then tell the compiler to set pins 2 to 9 as outputs on the arduino and then we tell the program to count from 9 down to 0 in one second intervals waiting at zero for four seconds before repeating the process.  At each point in the count we set the output pins on the arduino with the values stored in each of the array cells.  This sets the output pins high (+5V) or low (0V) depending on what is required.  The Seven Segment Display will output a number as the correct internal LED segments are turned on.        





// Langster's Code for driving a seven segment display
// I bought them off ebay from dannell.4you - great ebay shop for low volume 
// electronic components!
// Connect Pin 3 and pin 10 on the seven segment display to 0V
// Connect all the other pins to the arduino digital ouputs as listed below:

// Arduino pin 2 goes to pin 7 on the seven segment
// Arduino pin 3 goes to pin 6 on the seven segment
// Arduino pin 4 goes to pin 4 on the seven segment
// Arduino pin 5 goes to pin 2 on the seven segment
// Arduino pin 6 goes to pin 1 on the seven segment
// Arduino pin 7 goes to pin 9 on the seven segment
// Arduino pin 8 goes to pin 8 on the seven segment
// Arduino pin 9 goes to pin 5 on the seven segment

// Define the LED digit patters, from 0 - 9 in an integer array
// Note that these patterns are for common cathode displays
// For common anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:
// Arduino pin: 2,3,4,5,6,7,8,9

byte seven_seg_digits[10][8] = {

  { 1,0,1,1,1,1,0,1 },  // = 0
  { 1,0,0,0,0,1,0,0 },  // = 1
  { 0,0,1,0,1,1,1,1 },  // = 2
  { 1,0,0,0,1,1,1,1 },  // = 3
  { 1,0,0,1,0,1,1,0 },  // = 4
  { 1,0,0,1,1,0,1,1 },  // = 5
  { 1,0,1,1,1,0,1,1 },  // = 6
  { 1,0,0,0,1,1,0,0 },  // = 7
  { 1,0,1,1,1,1,1,1 },  // = 8
  { 1,0,0,1,1,1,1,0 },  // = 9

/* Just for reference

  pin 2 = C
  pin 3 = DOT
  pin 4 = E
  pin 5 = F
  pin 6 = A
  pin 7 = B
  pin 8 = G
  pin 9 = D

*/

};

void setup() {              
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}


void sevenSegWrite(byte digit) {
  byte pin = 2;
  for (byte segCount = 0; segCount < 8; ++segCount) {
    digitalWrite(pin, seven_seg_digits[digit][segCount]);
    ++pin;
  }
}

void loop() {
  for (byte count = 10; count > 0; --count) {
   delay(1000);
   sevenSegWrite(count - 1);
  }
  delay(4000);
}