7 segment display with 74hc595 shift register

I am new to shift registers so please forgive me if I have committed any blunders. For starters I wanted to display numbers 0-9 in a single segment (common anode) display using the 7 segment display. I made the connections and uploaded the code. Here it is

int latchPin = 2; //pin 12 on the 595
int dataPin = 3; //pin 14 on the 595
int clockPin = 4; //pin 11 on the 595

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}

void loop() {
  
  //0
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 64);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //1
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 121);
  digitalWrite(latchPin, HIGH);
  delay(1000);
  
  //2
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 36);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //3
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 48);
  digitalWrite(latchPin, HIGH);
  delay(1000);
  
  //4
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 25);
  digitalWrite(latchPin, HIGH);
  delay(1000);  
  
  //5
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 18);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //6
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 2);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //7
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 120);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //8
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);
  digitalWrite(latchPin, HIGH);
  delay(1000);

  //9
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 16);
  digitalWrite(latchPin, HIGH);
  delay(1000);
}

The problem here is that the digits 2, 5, 6 are not displaying properly. When displaying the digit 2 the segment B doesn't light up, but it does for other numbers. For 5, 6 the segment C doesn't light up, but it lights up for every other number. I checked the code several times. What could be the problem.

PS: just to be sure the 7 segment display is working properly I connected it in another circuit and everything was fine.

Translate your decimal constants into binary, then it's easier to find/fix the missing bits.

Looking at what is displayed and what is not displayed, it Looks to me like Segment B & C were wired switched.

When displaying "2", Segment B doesn't light up and Segment C isn't supposed to. When displaying "5" and "6", Segment "C" doesn't light up and Segment B isn't supposed to. When displaying for example "0", "1","3","4", "7", or "8", both light up and you wouldn't notice that they are in fact switched.

hey @jaba your advice seemed sensible and tried it out but it isn't true. after reading your reply I was kinda hoping I had made a mistake and switched the B and C wires in the shift register. I.e I switched pins 1 and 2, but to no avail. The mistake still persists.

@ DrDiettrich Just to test it out I tried to display the alphabet 'o' (I sent the number 71 in the ShiftOut() function) using the segments G,C,D,E. Since mine is a common anode display. I set the segments A,B,F high. When i uploaded it. the segment C doesnt light up. Only the segments G,E,D light up. But all the correct segments light up when displaying zero including B and C.

Chances are there is some sort of discrepancy with the code but I am not able to figure out what it is. Can you suggest any other methods?

Guys I think I am onto something here. I was tinkering around and just tried to light up every segment individually Segment A- 126(as expected only A lights up) B-125(both B and C light up(showing '1'), so i removed the wire connected to the B segment and B went off, similarly with C) lets call this situation Z C- 123 Same thing happens. (lets call this situation Y) D- 119 only D lights up.

What i went ahead and did next was I displayed '1' by lighting up B and C(lets call this situation X) what I observed was the brightness of the segments in situation X was much more than in situation Y and Z.

I hope I am being clear to you. thanks in advance for your patience. What do you make of this conundrum? please let me know

Do you have another 74HC595 to try?

yes i tried with it too. same result.

It'd be a lot easier to diagnose if all those DEC numbers were in BIN. Make sure you have figured out the order of the segments. It might be DP, A, B....G instead of A, B....G, DP.

With the '595 removed apply power and with a jumper CAREFULLY ground the locations where the chip outputs are and see if each individual segment lights up. Start where pin 15 would be and then 1 - 6, 7 if using DP. By the way, you are using current limiting resistors, aren't you?

If your rewrite

 shiftOut(dataPin, clockPin, MSBFIRST, 36);

into

 shiftOut(dataPin, clockPin, MSBFIRST, 0b0100100);

you have better control of the segments, because every digit corresponds to a segment. Then you can find out whether the bit pattern is wrong, or the bug resides in the hardware.

@DrDiettrich I entered the binary code for 2 and the same thing happened. no improvement for 5 and 6 either.

INTP: It'd be a lot easier to diagnose if all those DEC numbers were in BIN. Make sure you have figured out the order of the segments. It might be DP, A, B....G instead of A, B....G, DP.

I did. I have wired in the correct sequence. i checked and double checked and triple checked.

The same code will produce the same results, of course. But now you can find out which segments or which of the bits don't show as expected.

Give a list of the codes and the segments.

DrDiettrich: The same code will produce the same results, of course. But now you can find out which segments or which of the bits don't show as expected.

Give a list of the codes and the segments.

I sent codes to individually light up the segments and found that the segments B and C are not being lightened up individually. They always light up together and their brightness is less compared to what is actually supposed to be. I am beginning to think that there is some problem with the 74hc595 shift register. either that or I dont know how to over come it.

here is what i sent

  // segment a
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0b1111110);
  digitalWrite(latchPin, HIGH);
  Serial.println("2 is printed");
  delay(500);
  //segment b
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0b1111101);
  digitalWrite(latchPin, HIGH);
  Serial.println("2 is printed");
  delay(500);
  //segment c
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0b1111011);
  digitalWrite(latchPin, HIGH);
  Serial.println("2 is printed");
  delay(500);
  //segment d
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0b1110111);
  digitalWrite(latchPin, HIGH);
  Serial.println("2 is printed");
  delay(500);

Don’t use code to test the display. Manually test it with a multimeter in diode testing mode. Touch the anode and cathode for the segment. See if they have the same behavior. If they do, the display is to blame. Best at least rule that out.

I am so sorry, but turns out I hadnt actually tried out the extra shift register that I had with me.

I replaced the faulty one with the spare and the problem got solved to an extent. segment C has now started to work properly but the segment B isn't lighting up. I set all the bits to low to make every segment glow but Segment B didnt. So i checked the potential of Q1 with respect to 5v (since mine is a CA 7 segment display). and it was showing some very low value. Since this pin needs to be low for the respective segment to glow I have now understood that this is also a faulty IC since the pin 1 isnt going low even when I set it to be low. :confused:

As for the previous IC, i had a slight suspicion that there must be some internal connection between Q1(connected to B) Q2(connected to C) that made them light up together dimly, so I took out my multimeter and checked for continuity and it beeped! >:( >:( there was some god forsaken fault in the internal construction of this IC which connected Q1 and Q2. I tested all the other pins and they were fine(as expected).

So now it has been established that both the shift registers i have are faulty and I am gonna get it replaced with new working ones.

Thanks a ton people for your time(since 7 segment displays are too trivial a topic to be having a problem with) you people still took time from your day to address my issue so promptly. You guys are the best, learning arduino this way is even more fun! :)

once again Thank You! I ll keep you posted :)

Have you tried what I suggested in post 9? That should help determine if its a wiring or shift register problem. Can you post a picture of your setup that clearly shows the connections?

Yes! finally all your help paid off! I went to the shop and got the faulty chips replaced with new ones. It works like clock work! Thanks lot!