1

Just picked up a couple of 74HC595 shift registers to use for my Arduino Uno, and I'm having trouble getting it working 100% of the time. My code is to get a value from the Serial monitor and shift it into the register. The issue is that bits 4 and 5 are always on no matter what I put into the monitor, but when I clock a 0 into the register during setup(), it works as expected, i.e. all LEDs go off

Here is the code:

int dataPin = 2;
int latchPin = 3;
int clockPin = 4;


void setup() {
    Serial.begin(9600);
    pinMode(dataPin, OUTPUT);      
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
    digitalWrite(latchPin, HIGH);
}

void loop() {
    if(Serial.available()) {
        digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin, MSBFIRST, Serial.read());
        digitalWrite(latchPin, HIGH);
    }
}

Is it the code that is wrong? Do i have a busted shift register? Or something else? Thanks

  • 4
    You may have incorrect wiring. Maybe you should show it? – Majenko Nov 15 '17 at 14:03
  • I would be interested if you changed your code to always set bits 4 & 5 to 0. That is, perform the serial read first, bitwise AND the result with 0xFFE7 and then shift out the value. – sa_leinad Nov 15 '17 at 14:53
4

Serial.read() is going to give you ASCII codes. So when you send 1 from the serial monitor the Arduino receives 49. Note that the ASCII code for a digit is the same as the digit plus 48, so that sets bits 4 and 5 high.

You can use Serial.read() - '0' to get the actual number from the ascii code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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