Please help me understand this 4x7 Segment example (it doesn't work)

This following example is from the “The Most Complete Starter Kit Uno R3” from Elegoo.

I’ve arrived at the lesson about the 4x 7 segment display, controlled via a 74HC595, and in the tutorial it gives almost no information as to how it works or what it is supposed to do - I had to piece this together myself (it’s supposed to count 1-9 and A-F).

But setting up the wiring and running the provided code gives me strange artefacts on the segments. Initially I thought my wiring (see attachments) was wrong or shaky, but I checked all connections and added the option to manually select a number in the code - and that works no problems.

I did notice something strange using my manual code though: it would only work every 3s!

  • The first input displays ok.
  • The second input makes the first input disappear (clears the display).
  • The third input only works if it happens after 3s or more - otherwise the display remains empty.

This led me to try the following on the original code: set delay to 5000 instead of 500 and instead of fragments I do get the correct numbers - but only every other since every other turns the display off.

Now the manual for this component didn’t mention anything about resets or display times and it wasn’t my impression from handling it that it required 3s before it could update again.

Does anyone have any ideas as to what is happening here? Why the 3s delay and why wouldn’t the original code work? Is it even a code issue or a hardware limitation?

int latchpin=9;  //74HC595  pin 9 STCP
int clockpin=10; //74HC595  pin 10 SHCP
int datapin=8;   //74HC595  pin 8 DS

char table[]= {0x3f, /* 0011 1111 = 0*/  0x06, /* 0000 0110 = 1*/  0x5b, // 0101 1011 = 2
               0x4f, /* 0100 1111 = 3*/  0x66, /* 0110 0110 = 4*/  0x6d, // 0110 1101 = 5
               0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,  0x00}; // clear

void setup() {
  pinMode(latchpin,OUTPUT);  
  pinMode(clockpin,OUTPUT);  
  pinMode(datapin,OUTPUT);
  Serial.begin(9600); 
  while (!Serial); 
  Serial.println("Enter char to display 0-9: ");
}

void Display(unsigned char num){
  digitalWrite(latchpin,LOW);
  shiftOut(datapin,clockpin,MSBFIRST,table[num]);
  digitalWrite(latchpin,HIGH);
}

void loop() {
    static bool dooriginal = true;
    
    if (dooriginal){
        for (char i=1; i<18; i++){
            Display(i);
            delay(500);
        }
    }
    
    else {
      if (Serial.available())  {
        char ch = Serial.read();
        char todisplay = ch-'0';
        if (ch >= '0' && ch <= '9') Display(todisplay);
      }
    }   
}

How many elements does "table" have?

17 if I'm counting right?

  for (char i=1; i<18; You may be counting right, but your code isn’t.

Can you please explain? As I said: this is the example code that came with it, not my own code - and it does (semi) work if I up the delay).

(Also sorry for the slow replies - the forum will only let me post every 5 minutes for some reason).

A seventeen element array has elements with the indices zero to sixteen, inclusive.

There is no element with the index seventeen.

Ok, but for testing I usually changed it to 5 anyway and that didn't work either.

(changed it to 17, just to test - still same issues)

EDIT: I just had it running with 5 in the background and I noticed another weird thing: it's not always the same fragments. It might display like this: 1111 (nothing) 3333 4444 1111 (only segment A x 4) (nothing) 4444 1111 2222 (but with segment G missing on all) (only segment A x 4) 3333 4444 (nothing) (nothing) 1111 (nothing) 3333 (but with segment C+D missing on all) 4444 (nothing) 2222

So no real rhythm to the faulty display

I notice your schematic has pin 10 MR of the 74HC595 not connected, in the Elegoo manual that is connected to +5v. Its possible your shift register is randomly resetting the output.

edit: Apparently that schematic is from the Elegoo manual, I was looking at their previous lesson with the single digit LED, where pin 10 is shown correctly connected to +5v.

david_2018:
I notice your schematic has pin 10 MR of the 74HC595 not connected, in the Elegoo manual that is connected to +5v. Its possible your shift register is randomly resetting the output.

You, dear Sir, have an absolutely keen eye - that was indeed the issue!

I had noticed differences in the schematic vs the wiring once before in the elegoo manual, but in this example they both showed showed no connection on the MR and since I haven’t fully understood the component yet, I didn’t think anything of it.

Thanks for the spot!