Road to Create | Art of creation

Archive for November 2010



2 Digit Display (Weekend project)

For a project I needed a dual 7-segment display to show numbers varying from 00 to 99. About a year ago I tried to do the same, but I didn’t really know how to do it yet. Last weekend I summoned all my knowledge to try it again.

A 7-segment display is a display made out of 7 segments (Duh..) Those 7 segments are in my case simple red leds. There are 2 types of segment displays on the market, common anode and common cathode. Which means that all VCC or Ground pins are connected with eachother. For this project I choose to go for the common ground pin.
First I had to connect the display to the arduino. I used digital pin 2-11 for this, I skipped 0 and 1 because those are used for serial data. Between all the data pins and the display I added 1kOhm resistors. (At first I thought it wouldn’t be necessary, but when my poor arduino started to fry I added them anyway.)

The display is connected to the pins written next to the segments.
The next thing that had to be done, is making a list of all characters I wanted to use, and check which pins should be high for that matter. (on the right)
I’m always having trouble programming things I do not completely understand. Therefore I had to choose a data structure to save the character data in a way I could visualize it. That’s why I have chosen to go for a two-dimensional array.

In case you’re not familiar with arrays, here’s a short explanation:

Left top shows a simple variable, containing a value of the size integer. This variable can easily be requested by asking for “a”.
The left right shows a simple array. I accidentally forgot to write the name of the array, so it would actually be something like: int a[]. In this array you can put values of the size integer.
int a[] = {4,6,2}
a[1] Will return the value 6, since 6 is the second number in the array. (Arrays start counting at 0)

The structure I’ve chosen is shown below the previous two, namely a two-dimensional array.
If a variable can be a box containing a variable, and an array a set of boxes containing a variable a two-dimensional array are multiple sets of boxes stacked on each other.

int nums[][8] = { 
  {1,1,1,0,0,1,1,1} , 	//Digit ZERO
  { 0,0,1,0,0,0,0,1} 	//Digit ONE

This piece of code initializes an array called nums, containing two arrays with a predefined length of 8 integers.

So, now we’ve got the character data saved for usage. The next problem we have to face is the switching between the two displays. Switching between the two screens happens in the following sequence:
- Disable display 2
- Enable display 1
- Show data for display 1
- Disable dispay 1
- Enable display 2
- Show data for display 2

Enabling ground on the two displays is done by a transistor, connecting the display to ground when the signal on the base of the transistor is high.

The rest of the information for creating a two character display can be substracted from the code. If anything is still unclear you can always leave a comment or mail me for more information.

int nums[][8] = { 
    1,1,1,0,0,1,1,1                                  }
  ,     //Digit ZERO
    0,0,1,0,0,0,0,1                                  }
  ,     //Digit ONE
    1,1,0,0,1,0,1,1                                  }
  ,     //Digit TWO
    0,1,1,0,1,0,1,1                                  }
  ,     //Digit THREE
    0,0,1,0,1,1,0,1                                  }
  ,    //Digit FOUR
    0,1,1,0,1,1,1,0                                  }
  ,    //Digit FIVE
    1,1,1,0,1,1,1,0                                  }
  ,    //Digit SIX
    0,0,1,0,0,0,1,1                                  }
  ,    //Digit SEVEN
    1,1,1,0,1,1,1,1                                  }
  ,    //Digit EIGHT
    0,1,1,0,1,1,1,1                                  }
  ,    //Digit NINE
    2,3,4,5,6,7,8,9                                  }
  ,    //PIN INITS

int fdig, sdig;
int val;
int j;

void setup()  { 

  for(int i = 0; i<8; i++){
    pinMode(nums[10][i], OUTPUT); //Initialize all pins for writing
  pinMode(10, OUTPUT); //First Digit 
  pinMode(11, OUTPUT); //Second Digit

void loop()  {
  val = analogRead(0);    //Read the input pin
  j = map(val, 200, 1023, 0, 99); //Potmeter
  digitalWrite(11,LOW);    //Disable second Digit  
  digitalWrite(10,HIGH);   //Enable first Digit

  if (j>=10){                  //If Number > 10 use first digit
    fdig = j/10;               //Calculate the first digit
    for(int i = 0; i<8; i++){  //Run through the arrays for showing the numbers
      digitalWrite(nums[10][i], nums[fdig][i]);    //nums[arraynr][ledstatus(0/1)]

  else{                        //If Number <10 
    fdig = 0;                  //make first digit 0
    for(int i = 0; i<8; i++){
      digitalWrite(nums[10][i], nums[fdig][i]); 

  delay(10);               //Giving my sweet controller some time to rest

  digitalWrite(10,LOW);    //Disable first Digit 
  digitalWrite(11,HIGH);   //Enable second Digit

  sdig = j%10;             //Modulo 10, remains are for second digit

  for(int i = 0; i<8; i++){//Run through the arrays for showing the numbers
    digitalWrite(nums[10][i], nums[sdig][i]); //nums[arraynr][ledstatus(0/1)]

  delay(10);                //Giving my sweet controller some time to rest

No tags

Theme Design by