# 4 digit 7 segment display questin

Ok I hope the answer is not right in front of me but I have been searching for a while.

I am trying to make a 4-digit 7-segment display a counter. After trying several versions of code, I find the ones using PORTD to be the most efficient and accurate when displaying the numbers.

I have successfully gotten 2 digits to count sequentially but I can't get the middle segment (g) to turn on at the appropriate time.

I am using code from Simple Labs Induino R3. What am I missing? Thanks for any help. Meredith

Which 4-digit 7 segment display ?

Which code?

Below is the code and it works except for segment g in the middle.

This code uses the middle 2 digits (10,11) so I am trying to figure out how to use all 4.

I am using the following 7 segment display from SparkFun.

Thanks for responding…

Meredith

int seven_seg_val={ B00000011,B11100111,B10010011,B11000011,B01100111,B01001011,B00001011,B11100011,B00000011,B01000011};

void setup()
{
// Set all the pins from 0 to 7 as output.
// You can use binary data directly by preceeding with B
DDRD = B11111111; // This is the same as saying DDRD = 255;
// set the control pins for the common lines of both the 7 segment displays as OUTPUT
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
// write a LOW signal to the control pins for the common lines to keep them both disabled
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
}

void loop()
{
// A for loop to iterate through values 0 to 9 that have to be displayed on tens position of the 7 segment display
for(int i=0; i<10;i++)
{
// A for loop to iterate through values 0 to 9 that have to be displayed on ones position of the 7 segment display
for(int j=0; j<10;j++)
{
// A for loop to add stability to the display - the display looks more stable if the same value is displayed for more number of times at a higher speed.
for(int k=0; k< 100; k++)
{
// Turn ON Display 1 using the control Pin
digitalWrite(10,HIGH);
// Turn OFF Display 2 using the control Pin
digitalWrite(11,LOW);
// Write the set of values for the digit ‘2’ to the PORT register
PORTD = seven_seg_val*;*

• // A Short Delay*
• delay(1);*
• // Reset the PORT Register to 0*
• PORTD = 0;*
• // Turn OFF Display 1 using the control Pin*
• digitalWrite(10,LOW);*
• // Turn ON Display 2 using the control Pin*
• digitalWrite(11,HIGH);*
• // Write the set of values for the digit ‘3’ to the PORT register*
• PORTD = seven_seg_val[j];*
• // A Short Delay*
• delay(1);*
• // Reset the PORT Register to 0*
• PORTD = 0;*
• }*
• }*
• }*
}

If segment G fails to light, there is either a wiring error or an error in the array seven_seg_val. Change the first entry in seven_seg_val temporarily to B00000000 to see if an 8 (and decimal point) light up when it would normally display a zero. If it does, the wiring is OK and the solution is to correct the array.

Edit: On a second look, the array seven_seg_val[] is certainly incorrect because it has the same value for 0 and 8. It looks like it should be corrected to:

``````int seven_seg_val[]={
B00000011,B11100111,B10010001,B11000001,B01100011,B01001001,B00001001,B11100011,B00000001,B01000001};
``````

assuming that the array represents the digits 0 through to 9 and the bit values represent segments F,E,A,B,C,D,G,dp respectively.

6v6gt thank you for your reply… it really helped me to think through the problem further … I still don’t have the code working… and I believe it is a mismatch between the way I have it wired and the code I am trying to use.

I have attached the mapping I am using from the arduino Uno and the Sparkfun 4 digit 7 segment display and I can’t get several of the segments to light up although when I change the binary values for the PORTD assignment different segments light up. This is where I got the wiring and the code: Arduino 4 Digit 7 Segment Display Tutorial

Thank you again for your input.

I did’t find exactly the same code that you are using on the site you referred to (Arduino 4 Digit 7 Segment Display Tutorial) so I assume that your starting code is a hybrid out of bits that have been put together from different sources.

Anyway, the problem is clear. Look at the following which is a mapping between ports and arduino pin numbers

You’ll see that PORTD maps to arduino pins D0 through to D7. Segment G of your display is connected to pin D8 which is outside this range.

If you want to use your existing program structure, you’ll have to do something like the following:

in setup(), add the following line at the end:

``````pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
``````

Make changes to your inner loop structure

``````      for(int k=0; k< 100; k++)
{
// Turn ON Display 1 using the control Pin
digitalWrite(10,HIGH);
// Turn OFF Display 2 using the control Pin
digitalWrite(11,LOW);
// Write the set of values for the digit '2' to the PORT register
// PORTD = seven_seg_val;  ?????
PORTD = seven_seg_val[i];
if ( i != 0 && i != 1 && i != 7 ) {
digitalWrite( 8 , LOW ) ;  // sement G ON
}
else {
digitalWrite( 8 , HIGH ) ;  // sement G OFF
}
// A Short Delay
delay(1);
// Reset the PORT Register to 0
PORTD = 0;

// Turn OFF Display 1 using the control Pin
digitalWrite(10,LOW);
// Turn ON Display 2 using the control Pin
digitalWrite(11,HIGH);
// Write the set of values for the digit '3' to the PORT register
PORTD = seven_seg_val[j];
if ( j != 0 && j != 1 && j != 7 ) {
digitalWrite( 8 , LOW ) ;  // sement G ON
}
else {
digitalWrite( 8 , HIGH ) ;  // sement G OFF
}
// A Short Delay
delay(1);
// Reset the PORT Register to 0
PORTD = 0;
}
``````

What are you doing here. Turning all the segments on ?

``````        // Reset the PORT Register to 0
PORTD = 0;
``````

Shouldn’t you be switching them off ? PORTD = 255 ;

Another alternative might be to change your wiring to put segment G on arduino pin 1 and adjust the array seven_seg_val accordingly.

Hi, Have you tried the example code from the sparkfun site? http://cdn.sparkfun.com/datasheets/Components/LED/_7Seg_Example.pde

Tom... :)

Hi Tom,

I haven't tried that code but I find the code that assigns the value to the PORTD instruction works faster than the digitalWrite() subroutine so I am pursuing that method a bit more but if I can't get it to work soon, I will be implementing the program from Sparkfun... Thanks for the suggestion.

Meredith

I have made several direct drive 7 segment circuits and display routines for them. I have never encountered any delay problem using digitalWrite vs. port manipulation. If you are, it's probably in the algorithm or the implementation.