Arduino +Sparkfuns 4-digit 7-segment 16pin display

I've used the STLED316S LED driver in several projects with good results. It's inexpensive and can drive up to 6 common anode digits plus 8 additional discrete LEDs with brightness control. It can also read from up to 16 tactile switches. I'm currently developing an alarm clock shield using this chip - I hope to post details soon. I've written a library for the chip I plan to share. It's only available in an SMT package but you could always put one on a breakout board if that's a problem.

Can anyone please help me get hari's code working with my 4 digit 7 segment display?

It is almost the same as sparkfun's except the pins are in a different configuration.

Here is the pin configuration:
1-8 on bottom (left to right)
9-16 on top (right to left)

Thanks!

bump

Pinout is not the only difference. Your display is common Cathode and my SparkFun display is common Anode. So basically wherever the code sets pin to HIGH it needs to set it LOW and vice versa. I only had to make 4 changes to the code and I've highlighted them yellow. That should work. Good luck!

/*
  WAS: Testing SparkFun's 4-digit 7-Segment Display (Blue)
  NOW: Altered for GmDude66 for H.LTC.4727
 */

int segmentA = 0; // Segments A thru P == pins 0 thru 7
int digit0 = 10; // Digits 3 thru 0 == pins 10 thru 13 (0 is LEFT most digit)

/*
[glow]Change#1 Added wiring guide for your H.LTC.4727[/glow]

W I R I N G   G U I D E

=== Common Cathode digits ===
Arduino  Display  Digit
  10        1       0 Leftmost
  11        2       1
  12        6       2
  13        8       3 Rightmost
  
=== Anode Segments ===
Arduino  Display   Segment
   0       14         A
   1       16         B
   2       13         C
   3       3          D
   4       5          E
   5       11         F
   6       15         G
*/

/*
10 digits:
 Each defines which segments should be on/off for that digit: A,B,C,D,E,F,G,P
 */
byte numbers[10] =
{
  B11000000, // 0
  B11111001, // 1
  B10100100, // 2
  B10110000, // 3
  B10011001, // 4
  B10010010, // 5
  B10000010, // 6
  B11111000, // 7
  B10000000, // 8
  B10010000  // 9
};

void setup()
{
  for (byte segment = 0; segment < 8; segment++)
    pinMode(segmentA+segment,OUTPUT);

  for (byte digit = 0; digit < 4; digit++)
    pinMode(digit0+digit,OUTPUT);
}

int perDigitTime = 100;
int digitPosition = 1;  // Start anywhere but zero so we won't crash when we try to turn off previous digit.
unsigned long previousMillis = 0;

int value = 0;
int powerOfTen[] = {
  1,10,100,1000};

void RefreshDisplay()
{
[glow]  //Change#2 (Your display is common Cathode, so to turn OFF a digit, we need to set it to +5)[/glow]
  digitalWrite(digit0 + digitPosition, [glow]HIGH[/glow]);  // Turn off previous digit

  digitPosition++;
  if (digitPosition > 3) digitPosition = 0;

  int digitValue = value % powerOfTen[ digitPosition+1 ] / powerOfTen[digitPosition];
  int number =  numbers[ digitValue ];
  
  if ((number > 0) || (value < powerOfTen[ digitPosition+1 ]))
  {
  for (byte seg = 0; seg < 8; seg++)
    digitalWrite(segmentA+seg, [glow]1 - [/glow]bitRead(number, seg) ); [glow]// Change#3 added "1-" to invert the bit for your common Cathode display[/glow]

  digitalWrite(digit0 + digitPosition, [glow]LOW[/glow]);  [glow]//Change#4 LOW will turn this digit on.[/glow]
  delay(4);
  }
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > perDigitTime)
  {
    previousMillis = currentMillis;
    value++;
  }

  RefreshDisplay();
}

Thank you!

The display works perfectly now!

One more question: how would I go about getting the colon to stay lit all the time?

EDIT: Just combined your code along with ninjamastr's code to get this display working with my 74HC595

Thank you!
The display works perfectly now!

Awesome! Glad to be able to help.

One more question: how would I go about getting the colon to stay lit all the time?

I don't know. Sorry... :-[

drink 1oz of hot sauce, followed by 4 fingers of whiskey

I promise your colon will be lit up full time for quite a while :smiley:

zink0xide,

As others have mentioned, you can use practically any 8-bit serial-to-parallel 'sinking' driver IC or "constant current" sinking driver IC connected to the segments and five PNP or P-channel MOSFET 'sourcing' column drivers. The 74HC595 is a little "light duty" for this application but seems to hold up well. A better choice might be the TPIC6C595.

Here's an example of my MacMux? design for that Sparkfun display. I apologize for not yet having enough experience to back up the design with an Arduino software driver example but I could provide a C example for PIC if someone wants to take a stab at coding my method for Arduino.

The MacMux? design uses the PWM module with a period equal to the digit scan interval (1 to 2 msecs) to multiplex the column driver lines. When the PWM signal goes high (display "off") at the beginning of each digit scan interval the column driver lines are re-tasked for use as and lines to load the 74HC595 shift register. The column driver lines are then reset to the correct column select pattern (only one line low) before the PWM line goes back low (display "on"). Complete fade-to-black brightness control is a matter of setting the PWM duty cycle to some value between approximately 5% and 100% in the main program. PWM duty cycle is inversely proportional to display brightness with a 5% duty cycle = full brightness and a 100% duty cycle = black.

Regards, Mike

I was just wondering how to program the Arduino to make the 4 digit 7-segment display show a number that was arrived at in a different part of the program. I would be using the red display, so would it be different than Hari's post? I think I may have to use an Arduino Mega since I'm already taking up 8 digital pins with other things.

just a quick comment.
4 resistors are enough, and the 40ma is not a real issue (only 4 segments can be using the same current source, and with 10ma you can still get good brightness) if using the code from "Archives | Project Lab"
(mentioned by jeremytsl).
in this code the relevant segments are turned on individually, and stays on for a short delay, and then turned off. this also answers the question embedded in the code for why there is a need for a delay.
"delay(1); // Don't understand why I Have to set a delay
digitalWrite(groundPins[g], HIGH);"
also, if using the red 7-segment display, which is rated 2.1V and 20ma a 330 or 220 Ohm resistors are better.
if using a code that lights all the segments in a number at the same time - then the comments are accurate and segments should each have a resistor and 40ma Arduino pin output should be taken under consideration.
thanks everyone for your comments!

Hi all, my apology if this belong in a separate thread.

I just received this (YSD-439AK2B-35) from sparkfun and started playing with it. What I realize is that if I set this to input on HIGH, it uses lower voltage (1.74v). And I tested it out with 1k resistors, which is the smaller I have ATM, it will lid up but very dim (w/ 1k R).

I'm too chicken too remove the resistor, since its brand new... So what I want to know is, can I cut the cost down and save a few resistors this way?

The specs say Reverse voltage: VR=5V, 10µA and this is the code I used to test it.

void setup() {
  pinMode(13, INPUT);
  digitalWrite(13, HIGH);
  pinMode(2, OUTPUT);
}

void loop() {
  digitalWrite(2, LOW);  
  delay(1000);
  digitalWrite(2, HIGH);  
  delay(1000);
}

Thanks.

nm.... forgot about internal resistors on pin13...

Hi Hari,

i used your modified code for Common Cathode display, but the digit is inverted, i have the millisecond number on the left and the other number to the right.

Maybe the problem is your note //Digits 3 thru 0 == pins 10 thru 13 (0 is LEFT most digit)

i Think that the right is //Digit 0 thru 3 == pins 10 thru 13 but i don't know how change this in the sketch.

thanks for your attention


Posted by: hari Posted on: 26.03.2010 at 04:54:35
Pinout is not the only difference. Your display is common Cathode and my SparkFun display is common Anode. So basically wherever the code sets pin to HIGH it needs to set it LOW and vice versa. I only had to make 4 changes to the code and I've highlighted them yellow. That should work. Good luck!

Testato,
If I understand you correctly, it is probably simplest to adjust the wiring rather than the code. Take a look at Arduino pins 10,11,12,13. Those corresponds to the digits.
Say right now you have it wired like this:

Arduino Display
10 P
11 Q
12 R
13 S

You have complete control as to which digit value correspond to which display digit. What if you re-wire it like this?

Arduino Display
13 P
12 Q
11 R
10 S

Good luck!

yes i know, it work, but i would understand wich is the way for correct the code, because is the code incorrect not the connection.

it is only for understand.

Ah...
If you take a look at RefreshDisplay(), you will find two lines similar to this:
digitalWrite(digit0 + digitPosition, LOW); // Turn off previous digit

Each digit is turned on one at a time, so the above line turn off the previously on digit, adjust the segments for the next digit, and then turn on that next digit.

You should be able to adjust the calculation of the first parameter instead of re-wiring the circuit. Maybe something like this:

  1. Change digit0 from 10 to 13
  2. digitalWrite(digit0 - digitPosition, LOW); // Turn off previous digit
  3. digitalWrite(digit0 - digitPosition, HIGH);

in this way the millis are correctly on the right, but the other 3 left display is off

But in the original Anode common display all are correct ? it is strange, because you are only modified the code for the Common cathode part.

Yes, in the original circuit and sketch, digits are displayed in proper sequence.
There was an error in original dataspecs, so maybe you're running into something similar with your display? I don't know.

Maybe I understand the problem, the programmer in general (I'm not a programmer) when write code for display name the most right digit DIGIT0, or DIGIT1.

IN the datasheet normally the name of digit is from rihgt to left.

that's all :slight_smile:

thanks