Introduction: Beginner Tutorial: Controlling LED Matrix With 2 595 Shift Registers and Potmeter
I am just starting with Arduino and the instructables from amandaghassaei helped me a lot. In her second tutorialabout inputs and outputs, she explains how to use shift registers and how to control a LED matrix by multiplexing. In the last step, she suggests to combine both into a sketch that controls an 8 x 8 LED matrix that can be controlled using only 6 pins of the arduino instead of the 16 necessary pins when you would not use any 595 shift registers. Furthermore, I wanted to add a potmeter to show the multiplexing principle more effectively.
After some struggle, I succeeded and wanted to share the code, since it might be useful for other beginners as well. Also, I would like to receive feedback if I could have reached my goal more efficiently. Since this tutorial elaborates on the tutorial of amandaghassaei, I suggest to do that tutorial first.
Materials
1x Arduino Uno
1x USB cable
1x Breadboard
Jumper wires
8x 220Ohm resistors
2x 595 shift register
1x red LED dot matrix
Step 1: Setting Up the Hardware
The first step is to set up the hardware. Connect the Arduino to your computer and connect the 5V and ground to your breadboard.
Then add the two shift registers to your breadboard and connect them as follows:
-GND (8): connect to ground
-Q7S (9): unconnected
-MR (10): connect to 5V
-SHCP (11): connect to pin 13 of the arduino for the shift register that controls the 5V side of the LED matrix and to pin 10 of the arduino for the shift register that controls the grounds of the LED matrix
-STCP (12): connect to pin 12 of the arduino for the shift register that controls the 5V side of the LED matrix and to pin 9 of the arduino for the shift register that controls the grounds of the LED matrix
-OE (13): connect to ground
-DS (14): connect to pin 11 of the arduino for the shift register that controls the 5V side of the LED matrix and to pin 8 of the arduino for the shift register that controls the grounds of the LED matrix
-VCC (16): connect to 5V
-Q0-Q7 ( 1-8 + 15): Q0-Q7of the shift register that controls the 5V side of the LED matrix should each be connected to a resistor (I chose 220 ohm resistors), and should then be connected through (in the right sequence) to the corresponding pins on the LED matrix (make sure to check the data sheet of your LED matrix).
Connect Q0-Q7 of the other shift registers to the grounds of the LED matrix (in the right sequence).
Next, add the potmeter by connecting the outer pins to the ground and 5V, and the inner pin to analog input A2 of the arduino.
Step 2: Uploading the Sketch to Arduino
Upload the following sketch to Arduino. It should show a heart on your LED matrix. The delay can be controlled using the potmeter. By removing the delay the heart becomes visible as a whole, while increasing the delay shows what is actually happening in the sketch. The image that is shown can be easily adapted, by changing the values in the ledStates array.
<p>int clockPin = 10;<br>int latchPin = 9; int dataPin = 8;</p><p>int clockPinx = 13; int latchPinx = 12; int dataPinx = 11;</p><p>int potPin = 2; int val = 0;</p><p>byte ledStates[8] = {B00000000, B00011000, B00111100, B01111110, B11111111, B11111111, B11111111, B01100110}; byte GroundLEDs [8] = {B01111111, B10111111, B11011111, B11101111, B11110111, B11111011, B11111101, B11111110};</p><p>void setup() { pinMode(latchPinx, OUTPUT); pinMode(clockPinx, OUTPUT); pinMode(dataPinx, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);</p><p> digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, B11111111); digitalWrite(latchPin, HIGH); Serial.begin(9600); }</p><p>void loop() { for(int i=0;i<8;i++){ SetStates(ledStates[i]); GroundCorrectLED (GroundLEDs[i]); val = analogRead(potPin); int del = map (val, 0, 1023, 0, 100); Serial.print(val); Serial.print(del);</p><p>delay(del);</p><p> digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, B11111111); digitalWrite(latchPin, HIGH); delay(del); }}</p><p>void GroundCorrectLED (byte states){ digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, states); digitalWrite(latchPin, HIGH); }</p><p>void SetStates (byte statesx){ digitalWrite(latchPinx, LOW); shiftOut(dataPinx, clockPinx, LSBFIRST, statesx); digitalWrite(latchPinx, HIGH); }</p>