LED Matrix
I created a LED matrix to teach myself about multiplexing. An arduino controls the LEDs.

Basically, one pin controls each row of LEDs and one pin controls each column. When the pin connected to the anode of an LED is set to be a high output pin (in this case one of the "row" pins) and a pin connected the cathode of an LED is set to be an input pin (in this case the "column" pins), the LED at the intersection of the row and column pins turns on. It turned out great, but seemed useless. To increase the complexity and make it more useful, I modified the project to accept commands over serial. The arduino plays a pattern on the 4x4 grid when "start" is sent. A user can send "stop" to turn the LEDs off. Here is the arduino code:
uint8_t column[4] = { 3, 4, 5, 6 };
uint8_t row[4] = { 10, 9, 8, 7 };
unsigned long animationSpeedMillis = 100;
const uint8_t frames = 16;...
uint8_t column[4] = { 3, 4, 5, 6 };
uint8_t row[4] = { 10, 9, 8, 7 };
unsigned long animationSpeedMillis = 100;
const uint8_t frames = 16;
uint8_t pattern[frames][4][4] = {
{{1,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}},
{{1,0,0,0},
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}},
{{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,0}},
{{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1}},
{{0,0,0,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}},
{{0,0,0,1},
{0,0,1,0},
{0,0,0,0},
{0,0,0,0}},
{{0,0,0,1},
{0,0,1,0},
{0,1,0,0},
{0,0,0,0}},
{{0,0,0,1},
{0,0,1,0},
{0,1,0,0},
{1,0,0,0}},
{{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}},
{{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}},
{{0,0,0,0},
{0,0,0,0},
{1,1,1,1},
{0,0,0,0}},
{{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{1,1,1,1}},
{{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,0,0,0}},
{{0,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,1,0,0}},
{{0,0,1,0},
{0,0,1,0},
{0,0,1,0},
{0,0,1,0}},
{{0,0,0,1},
{0,0,0,1},
{0,0,0,1},
{0,0,0,1}},
};
// the setup function runs once when you press reset or power the board
void setup() {
for (uint8_t x=0; x<4; x++) {
pinMode(column[x], OUTPUT);
digitalWrite(column[x], HIGH);
pinMode(row[x], OUTPUT);
digitalWrite(row[x], LOW);
}
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Startup Complete");
}
// the loop function runs over and over again forever
void loop() {
for (int frame=0; frame millis()) {
if (shouldRun()){
drawPattern(frame, 1);
}
}
}
}
bool runLoop = false;
bool shouldRun(){
if (Serial.available()){
String command = Serial.readString();
if (command.equals("start")) {
Serial.println("Starting");
runLoop = true;
} else if (command.equals("stop")) {
Serial.println("Stopping");
runLoop = false;
} else {
Serial.println("Unknown Command:");
Serial.println(command);
}
}
return runLoop;
}
void drawPattern(int frame, uint8_t delayMillis){
for (uint8_t r=0; r<4; r++) {
for (uint8_t c=0; c<4; c++) {
if (pattern[frame][r][c] == 1) {
digitalWrite(row[r], HIGH);
digitalWrite(column[c], LOW);
delay(delayMillis);
digitalWrite(row[r], LOW);
digitalWrite(column[c], HIGH);
}
}
}
}
Show Full Code...
Here is the Python (I was using 2.7) code which will send a serial command to the Arduino. The command sent will be whatever is passed as the first argument to the program. For example, you could run it with "python3 scriptName.py start" or "python scriptName.py stop".
import serial
import sys
s = serial.Serial(
port='/dev/cu.usbserial-ADAOGMM2t',
baudrate=57600
)
print "sent"
s.write(sys.argv[1])
s.readline()
s.close()
Here is the diagram for a 3x3 matrix from
http://www.multiwingspan.co.uk/arduino.php?page=matrix:
Pins 5,6,7 and 8,9,10 get connected to the corresponding digital pins on an arduino. Although my matrix is 4x4, the principle is the same - only with two more pins and 7 more LEDs.