Beruflich Dokumente
Kultur Dokumente
Hi! I’m co-founder and Strategic Product Manager at OutSystems, with a passion for mobile and
web development, great products, and geeky stuff!
Mar 5, 2017 · 3 min read
Setting up a LED Matrix can quickly get you out of pins to do other
things with your Arduino. One way to overcome this is to use a
74HC595 shift register.
This chip transforms bits that are inserted in series trough the data pin
into 8 parallel bits. More on that later, but first, the circuit:
Pins QA-QH on the 595 are the outputs that we want, so they’re
connected to the matrix rows. Don’t worry about QH*.
• SRCLK (SeRial CLocK) the pin you set to high to store what’s in
SER;
• RCLK (Register CLocK) the pin you set to high once you’re done
setting all the pins.
So here’s how it works. Let’s imagine you want to set the QA-QH to
10010010. You start with the least significant bit (0) so you set SER to
LOW (D10 on the Arduino). Next, you set SCK (D11 on the Arduino)
to HIGH and then to LOW, to “save” the value.
Time to go to the second bit, which has value 1. So you set SER to
HIGH, and again you set SCK to HIGH and then to LOW.
You keep going, until you’ve sent the 8 bits. At this point, you set the
RCLK to HIGH to sent your changes to pins QA through QH.
This may seem complex, but fortunately Arduino has a function that
takes care of sending that bits out to the shift register:
This means that, to set data on the 595, all you have to do is:
digitalWrite(RCK_PIN, LOW);
// Write byte_to_write to SER, using SCK_PIN as the clock
and
// send the least significant byte first (LSBFIRST)
shiftOut(SER_PIN, SCK_PIN, LSBFIRST, byte_to_write);
digitalWrite(RCK_PIN, HIGH);
So here’s the full code for the LED matrix, using a 595:
1 byte character[8]={0x24, 0x24, 0x24, 0xa5, 0x81, 0x81, 0x7e
2
3 uint8_t colPins[8]={ 2, 3, 4, 5, 6, 7, 8, 9};
4
5 #define SER_PIN 10
6 #define SCK_PIN 11
7 #define RCK_PIN 12
8
9 void setup() {
10 // Turn everything to low
11 for(int i=0; i<8; i++) {
12 pinMode(colPins[i],OUTPUT);
13 }
14 pinMode(SER_PIN, OUTPUT);
15 pinMode(SCK_PIN, OUTPUT);
16 pinMode(RCK_PIN, OUTPUT);
17 Serial.begin(9600);
18 }
19
20 void loop() {
21 // iterate each row
22 int rowbits = 0x80;
23 for(int row=0; row<8; row++) {
24 for(int k=0; k<8; k++)
25 digitalWrite(colPins[k],HIGH); // Cleanup cols
write595(rowbits); // prepare to write the row
If you run this code you’ll get a nice smile! If you want to build a
different character, head over to the character builder and create your
own.
You can get the code and schematics for this project on github.