Monday, 2 June 2014

Arduino Uno Tutorials – 8 – Shift Register

I like this project from the Freetronics tutorials because it gives me a means of programming multiple LED from a single sketch.
I can split the LED up into groups for different things in my project, and simply control them as a single array.
The Freetronics project goes in to some detail about bits, bytes, binary numbers, etc. However, there are loads of good bits of pertinent information on binary numbers, conversion etc. on the interweb. The main things to understand to use the 74HC595 shift register are:

  • 1 input (bit) – 8 outputs (byte),
  • The 8 outputs are either On (1) or off (0),
  • The output is represented in binary format,
  • The UNO doesn’t really understand binary numbers > 255. Here’s a quick primer for a byte of binary numbers. Binary is a base 2 numbering system, compared with decimal, which  is base 10. Each digit in a binary number is twice as big as the next number … so, one digit is = 2, two digits = 4 (2*2), three digits = 8 (4*2), four digits = 16 (8*2) and so forth.
  • 1 = 0 or 1
  • 2 = 00, 01, 11, 10
  • 3 = 000, 001, 010, 011, 111, 110, 101, 100
  • etc.. I recommend the Wiki Answers pages on converting Decimal to Binary and Binary to Decimal. Mostly, binary to decimal, because each digit represents an LED and 0 and 1 represents High or Low states for the LED. You need to tell your Arduino the state of the LED via a decimal number. On the other hand, you COULD pass it a hexadecimal number, but then you’d be converting Binary to Decimal to Hexadecimal and, really … who want’s to? There’s also a great Wiki Answers page on converting Decimal to Hexadecimal too.
    Quick Alternative!

    Windows Calculator …

    image
    Launch WIndows Calculator, click on View and select “Programmer”
    Select Bin mode
    image
    Enter your 8 digits representing the LED states … for example, every second LED is High, so every second digit is a 1 (Windows calculator will strip the leading 0 for you … thanks Windows).
    Now here’s the tricky bit …. click the Dec (base 10) option and … presto … 85 is displayed. Click the Hex (base 16) option and … magically … 55 is displayed. You can pass either 85 or 0x55. If your number is less than 256 (0 to 255) then you could pass it in binary in the sketch by preceding the binary number with “b”, so b01010101 would work.. Apparently, you could also do it in octal (base 8), but I haven’t tried. Check out the Arduino reference Integer Constants page for more information on using these bases in your sketch.
    The Freetronics project has a fairly straight forward hardware set-up where you are connecting the data pin to 2, the latch pin to 3 and the clock pin to 4, Pin 16 is connected to 5V, OE pin is attached to Ground. Next, the output pins are connected to their respective LED and the LED are connected to ground. Refer to the Freetronics project for the breadboard layout. On the Arduino site, it is recommended that you connect a 0.1uF capacitor to the latch pin if you are getting some flickering in your LED output. The capacitor is there to smooth the pulses.
    I didn’t get any flickering when I used some decent LED. When I used the LED that came with the Freetronics experimenters kit, I needed the capacitor.
    Picture 56
    The above picture is the circuit with the 74HC595 on the right and the spooky green LED to the left of the shift register.
    This is the second time that I wired this circuit up and it worked without any real problems both times. I’d say that this is a pretty easy circuit to wire.
    The lighting is low in this image (and the video) so that you can see the LED. At normal white balance and lighting, there’s just a glow.
    I’ll try this again with a second shift register … when my eBay order arrives!
    Splitting the LED up for different uses (some in a running lights scenario, some just blinking) simply means allocating the control differently. Again, when I have the second shift register, I’ll write another article to show you what I mean.
    The Freetronics sketch for the project is as follows:
    int dataPin = 2;
    int latchPin = 3;
    int clockPin = 4;
    void setup()
    {   pinMode(dataPin, OUTPUT);   pinMode(latchPin, OUTPUT);   pinMode(clockPin, OUTPUT);
    }
    void loop()
    {   for(int currentValue = 0; currentValue < 256; currentValue ++)   {       digitalWrite(latchPin, LOW);       shiftOut(dataPin, clockPin, MSBFIRST, currentValue);       digitalWrite(latchPin, HIGH);       delay(100);   }
    }

    It differs from the Arduino website sketch in that the Arduino sketch connects the data to digital 14, clock pin to digital 12 and the latch to digital 8. Freetronics, Analog, Arduino … digital.

    Freetronics Project 8 – Shift Register using Arduino Uno and the 74HC595
  • No comments:

    Post a Comment

    Paypal Donations

    Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.