0
\$\begingroup\$

I am using a 74HC595 shift register IC connected to my Arduino board as described in this tutorial to drive 8 RGB LEDs. The code is given below and is similar to the one in the tutorial:

const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 13;
const int redValuePin = 9;
const int greenValuePin = 10;
const int blueValuePin = 11;

void setup() { 
  pinMode(latchPin, OUTPUT); 
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(redValuePin, OUTPUT);
  pinMode(greenValuePin, OUTPUT);
  pinMode(blueValuePin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Reset");
}

void loop() {
  analogWrite(redValuePin, 0);
  analogWrite(greenValuePin, 255);
  analogWrite(blueValuePin, 255);
  if (Serial.available() > 0) {
    int bitToSet = Serial.read() - 48;
    registerWrite(bitToSet, HIGH);
  }
}

void registerWrite(int whichPin, int whichState) {
  byte bitsToSend = 0x00;
  digitalWrite(latchPin, LOW);
  bitWrite(bitsToSend, whichPin, whichState);
  Serial.println(bitsToSend);
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
  digitalWrite(latchPin, HIGH);
}

However, I get HIGH output only on pin 7 (O7) when I use LSBFIRST and on pin 15 (O0) when I use MSBFIRST, irrespective of bitsToSend (which is updated properly as I have verified by printing its value). Is there any reason in the hardware/software that caused this, or do I just have a faulty IC?

Edit: The RGB lines are separate from the shift register circuit and are just all connected to the 8 LEDs, such that I can control their colors collectively.

\$\endgroup\$
4
  • \$\begingroup\$ I came to know that the breadboard diagram given in the tutorial is wrong and the schematic instead is the right circuit. However, even after connecting according to the schematic, I still get the same problem. \$\endgroup\$ Aug 21, 2023 at 15:01
  • \$\begingroup\$ So what's the assumption? You are pressing characters 0..7 on your keyboard? If the baud rate is incorrect, you will not receive the correct characters and so the bits will be off too. \$\endgroup\$
    – Justme
    Aug 21, 2023 at 16:36
  • \$\begingroup\$ Yes, the expected behaviour is to see a corresponding LED glow when I press characters 0-7. Could you elaborate about what you mentioned of the baud rate? \$\endgroup\$ Aug 23, 2023 at 15:20
  • \$\begingroup\$ Can you communicate betwee Arduino and PC properly otherwise, can you e.g. echo back all bytes received without problems? \$\endgroup\$
    – Justme
    Aug 23, 2023 at 16:48

1 Answer 1

0
\$\begingroup\$

This is resolved. The problem was due to my Serial monitor adding a NL (new line) character to my inputs, causing error in the value sent to the shift register IC. Once configured to not do this, the program works fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.