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!!