Reversed output on 74hc595

I am a beginner at electronics. I was trying to light up 8 leds using a 74hc595. I did all the cabling and programming but faced a very unexpected result. When I upload a high bit to the register, it outputs a low bit, and vice versa. So when I want to light up all 8 leds, they all get dark. If I try to to get them all dark, then they altogether light up. Here is my code for lighting them up one by one (This actually makes them all dark one by one):

int pData = 2;
int pShift = 3;
int pLatch = 4;

void setup() 
{
  pinMode(pData, OUTPUT);
  pinMode(pShift, OUTPUT);
  pinMode(pLatch, OUTPUT);
}

void loop() 
{
  
  digitalWrite(pLatch, LOW);

  digitalWrite(pShift, LOW);
  digitalWrite(pData, HIGH);
  digitalWrite(pShift, HIGH);
  
  digitalWrite(pLatch, HIGH);

  delay(500);
}

I can of course move on with this reverse logic, uploading high when I need low, etc, but seriously, what is going on here? Any guidance is appreciated.

digitalWrite(pShift, LOW);
digitalWrite(pData, HIGH);
digitalWrite(pShift, HIGH);

You have just loaded '1' into the shift register.

Sounds like a wiring logic problem. Can you post your schematic?

I don't have a schematic ready but I can show you some pictures:

dhenry:

digitalWrite(pShift, LOW);
digitalWrite(pData, HIGH);
digitalWrite(pShift, HIGH);

You have just loaded '1' into the shift register.

And that is supposed to light up the first led, right? In my case it does the opposite.

Depending how the leds are wired, those pictures can be total correct.

If you don't want to reverse your data being sent, just reverse how the leds are wired: if they are wired to ground now; wire them to Vcc; if they are wired to Vcc, wire them to ground.

that is supposed to light up the first led, right?

Yes / no, depending on how your leds are wired.

dhenry:
Depending how the leds are wired, those pictures can be total correct.

If you don't want to reverse your data being sent, just reverse how the leds are wired: if they are wired to ground now; wire them to Vcc; if they are wired to Vcc, wire them to ground.

Damn, I totally see your point. thanks a million. I had to be much more careful.

bgenc:
I don't have a schematic ready but I can show you some pictures:

Okay, it appears you have your logic backwards. You have all your LED anodes wired to +V through a current limiting resistor. Then you are driving the individual cathodes from the output of your shift register. So, if you want to turn on an LED you must write a LOW into the shift register output so that current will flow from +V toward a low voltage(effectively ground). When you write a HIGH into the shift register output, you effectively have +V at both ends of your circuit, and no current flows. Does that make sense?

PapaG:

bgenc:
I don't have a schematic ready but I can show you some pictures:

Okay, it appears you have your logic backwards. You have all your LED anodes wired to +V through a current limiting resistor. Then you are driving the individual cathodes from the output of your shift register. So, if you want to turn on an LED you must write a LOW into the shift register output so that current will flow from +V toward a low voltage(effectively ground). When you write a HIGH into the shift register output, you effectively have +V at both ends of your circuit, and no current flows. Does that make sense?

It absolutely does. Thank you very much. Problem solved.

bgenc:

PapaG:

bgenc:
I don't have a schematic ready but I can show you some pictures:

Okay, it appears you have your logic backwards. You have all your LED anodes wired to +V through a current limiting resistor. Then you are driving the individual cathodes from the output of your shift register. So, if you want to turn on an LED you must write a LOW into the shift register output so that current will flow from +V toward a low voltage(effectively ground). When you write a HIGH into the shift register output, you effectively have +V at both ends of your circuit, and no current flows. Does that make sense?

It absolutely does. Thank you very much. Problem solved.

You're welcome. :slight_smile: