74HC595 Shift Register and PC817 optocoupler not working the way I expect them to

So I am working on this little project that will take in eight inputs that the Arduino will read, and send them to the shift register to be shifted out in the respective order. These shifted outputs will then go through 220 ohm resistors and finally go through the input of a PC817 optocoupler. The Arduino will supply the 5v and ground to the shift register, and the ground for the input of the PC817. On the output of the PC817, there are LED's that are connected to an external 5V power supply. When the PC817 receives the input from the 220 ohm resistors, the LED's light up at the ouput.

To test whether this part of the project works, I've removed all the other code and simply hard coded the numbers in to the shiftout function.
0000-0001 = 1 (One LED will be on)
0000-0011 = 3 ( Two LED's will be on)
0000-0111 = 7
.....
1111-1111 = 255 (All LED's will be on)

My code:

// Pins for SR
const int LATCH = 11;
const int DATA  = 12;
const int CLOCK = 10;
int  hard_coded_nums;

void setup()
{
  // Standard procedure for getting the SR working
  pinMode(LATCH,OUTPUT);
  pinMode(DATA,OUTPUT);
  pinMode(CLOCK,OUTPUT);
 
}

void loop()
{
  // Hold LATCH low so data within SR doesn't change
  digitalWrite(LATCH,LOW);
  
  // Pattern to display
  hard_coded_nums= 1; // 1,3,7,15,31,63,127,255
  
  // Shifts out the pattern from ARD. to SR
  shiftOut(DATA, CLOCK, MSBFIRST,  hard_coded_nums);
  
  // Pattern is sent out from the SR
  digitalWrite(LATCH,HIGH);
  delay(1);
}


Here is a picture of how the circuit looks like (tinker cad doesn't have optocouplers)

So, when I perform the physical circuit, I am running in to issues. For starters, the LED's are intermittent. Secondly, the circuit does not do what it's suppose to whenever I try to get 3-7 LED's to turn on. If I just turn on 1,2 and 8 LED(s), the circuit works. I've uploaded a youtube video of me trying out the code for 1, 2, and 3 LED's to show what my problems is.

**I've attempted this with and without including the delay and they both do the same thing.

Is there something going on with my code? Is there a flaw in my understanding of using the 74HC595 or PC817?

I appreciate any help!!

Pin 2 of the PC817 optocoupler goes to Arduino GND .

Place a 100nF ceramic capacitor across the shift register Vcc and GND.

Try adding a 0.1uF cap from VCC to Gnd at the shift register.
You have pin 10 High, and 13 Low?

You need an oscilloscope to see the pulses you’re generating. If you want to see them with your eyes, the delay() needs to be at least 100. The typical demos use 500 which is a 1/2 second delay.

Shifting out the same output over and over would only make the output appear to be on steady.

Thank you for the replies! I placed a 100nF capacitor across the VCC and GND of the SR [as suggested by @LarryD and @CrossRoads] and it seems to have to fixed the intermittent blinking LED's. I did have pin 10 HIGH and 13 LOW on the SR and I did have pin 2 of PC817 to Arduino GND as well. Here is a picture of my set up:
image

Unfortunately, I still have the same issue with some LED's being on when they shouldn't be. Here I have it set to have the top 4 LED's on, but you can see two blue LED's being somewhat on as well.

I just attempted to have all LED's on. It works but some are dim?

image Couldn't attach more than one photo per post

image
All LED's on, some are dimmer than others..

Try reducing the 220 to 120Ω.

Optocouplers - at least the PC817 - are most certainly not intended to drive LEDs. What series resistors are you using for the LEDs - certainly not less than 1k? :roll_eyes:

The uneven brightness of the LEDs relates to variation in CTR, but should not be so noticeable with the correct 1k resistors.

I can only assume the LEDs are proxy for some other device you later wish the optocouplers to control. Clearly LEDs themselves do not need isolation.

Try writing all 0's to the shift register in setup() so there is known good data when loop() starts.

Yes to the proxy part. The external power supply will be use to short another circuit, it' easier to have a better visualization of what was going on with the LED's. The resistors I used were certainly less than 1K.

I originally tried to do this with an eight channel relay (this one in particular) but when I tried having all LED's on, only six would turn on. I wasn't sure if the relay was drawing too much current from the 74HC595.

I did this and it had no effect on what was going on.

Not from the 74HC595 but what were you using to actually provide 5 V to the relay board? Certainly not the UNO as eight relays operated at once will draw 720 mA from the "JD-VCC" pin and "GND". :astonished:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.