Arduino – LED Matrix display 8×8 dots (MAX7219)
Hardware
Deze 8×8 Dot matrix heeft 64 ledjes, aangestuurd door een MAX7219 IC, je hebt maar 3 I/O aansluitingen nodig vanaf een microcontroller.
Pinout
Pin: | Functie: |
01 | +5v |
02 | GND |
03 | DIN (DATA) |
04 | CS (Chip Select) |
05 | CLK (CLOCK) |
Wat heb je nodig?
Het display werkt met binaire data, er is daarvoor een arduino bibliotheek geschreven om het makkelijk te maken.
Bij het gebruik van de bibliotheek kun je het matrix display eenvoudig aansturen met: lc.setLed([displaynr], [rij], [kolom], [true/false]);
Voorbeeld: lc.setLed(1, 4, 4, true);
Waar:
[displaynummer] = het adres van de matrix unit, je kan er maximaal 8 doorkoppelen
[rij] = de rij van het ledje dat aangestuurd moet worden (let op: begin bij 0)
[rij] = de kolom van het ledje dat aangestuurd moet worden (let op: begin bij 0)
[true/false] = true = led aan, false = led uit
Het instellen van de pin configuratie voor de bibliotheek is als volgt:
LedControl(CS, CLK, DIN, NRDEV);
Voorbeeld: LedControl(12,11,10,1);
Aansluiten op de Arduino
Sluit het volgende aan volgens onderstaand schema:
Arduino Pin: | 8x8 DOT Matrix pin: |
---|---|
+5v | +5v |
GND | GND |
D10 | 03 (DIN / DATA) |
D11 | 05 (CLK / CLOCK) |
D12 | 04 (CS / Chip Select) |
Script LED demo loop
Dit script laat de ledjes 1 voor 1 branden en zet ze daarna uit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// Inladen van de ledcontrol bibliotheek. #include "LedControl.h" LedControl lc=LedControl(12, 11, 10, 1); // DIN, CLK, CS, NRDEV //Een variabele voor het wachten voordat we het display updaten. unsigned long delaytime = 50; void setup() { // Haal het aantal apparaten op dat we hebben "gecreëerd" met Ledcontrol. int devices=lc.getDeviceCount(); // Alle apparaten initialiseren (in een loop). for(int address=0;address<devices;address++) { // De MAX72XX IC is in slaapstand modus bij opstarten. lc.shutdown(address,false); // Zet de helderheid op een medium niveau. lc.setIntensity(address,8); // Maak de dot matrix leeg (clear display). lc.clearDisplay(address); } } void loop() { // Lees het aantal apparaten uit. int devices=lc.getDeviceCount(); // Laat de ledjes stuk voor stuk branden. for(int row=0;row<8;row++) { for(int col=0;col<8;col++) { for(int address=0;address<devices;address++) { lc.setLed(address,row,col,true); delay(delaytime); } } } // Zet de ledjes stuk voor stuk uit. for(int row=0;row<8;row++) { for(int col=0;col<8;col++) { for(int address=0;address<devices;address++) { lc.setLed(address,row,col,false); delay(delaytime); } } } } |
Meerdere DOT matrix displays aan elkaar gekoppeld
Het volgende voorbeeld laat zien hoe je meerdere 8×8 dot matrix displays aan elkaar koppelt en aanstuurt, het laat 2 figuren uit de space invaders zien.
Sluit de modules aan volgens onderstaand schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include "LedControl.h" LedControl lc=LedControl(12,11,10,2); // Pins: DIN,CLK,CS, # of Display connected unsigned long delayTime=200; // Delay between Frames // Put values in arrays byte invader1a[] = { B00011000, // First frame of invader #1 B00111100, B01111110, B11011011, B11111111, B00100100, B01011010, B10100101 }; byte invader1b[] = { B00011000, // Second frame of invader #1 B00111100, B01111110, B11011011, B11111111, B00100100, B01011010, B01000010 }; byte invader2a[] = { B00100100, // First frame of invader #2 B00100100, B01111110, B11011011, B11111111, B11111111, B10100101, B00100100 }; byte invader2b[] = { B00100100, // Second frame of invader #2 B10100101, B11111111, B11011011, B11111111, B01111110, B00100100, B01000010 }; void setup() { lc.shutdown(0,false); // Wake up displays lc.shutdown(1,false); lc.setIntensity(0,5); // Set intensity levels lc.setIntensity(1,5); lc.clearDisplay(0); // Clear Displays lc.clearDisplay(1); } // Take values in Arrays and Display them void sinvader1a() { for (int i = 0; i < 8; i++) { lc.setRow(0,i,invader1a[i]); } } void sinvader1b() { for (int i = 0; i < 8; i++) { lc.setRow(0,i,invader1b[i]); } } void sinvader2a() { for (int i = 0; i < 8; i++) { lc.setRow(1,i,invader2a[i]); } } void sinvader2b() { for (int i = 0; i < 8; i++) { lc.setRow(1,i,invader2b[i]); } } void loop() { // Put #1 frame on both Display sinvader1a(); delay(delayTime); sinvader2a(); delay(delayTime); // Put #2 frame on both Display sinvader1b(); delay(delayTime); sinvader2b(); delay(delayTime); } |
Meerdere DOT matrix displays aan elkaar gekoppeld met lichtkrant
Met het volgende script kan je een lichtkrant maken door meerdere modules aan elkaar te koppelen.
Wat heb je nodig?
1) Arduino MaxMatrix bibliotheek
Ps bij Arduino IDE < 1.6.x : PROGMEM const unsigned char CH[] vervangen in PROGMEM prog_uchar CH[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
#include <MaxMatrix.h> #include <avr/pgmspace.h> PROGMEM const unsigned char CH[] = { 3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space 1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // ! 3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // " 5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // # 4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $ 5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // % 5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // & 1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // ' 3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // ( 3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // ) 5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // * 5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // + 2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // , 4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // - 2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // . 4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // / 4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0 3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1 4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2 4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3 4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4 4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5 4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6 4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7 4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8 4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9 2, 8, B01010000, B00000000, B00000000, B00000000, B00000000, // : 2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ; 3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // < 3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // = 3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // > 4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ? 5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @ 4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A 4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B 4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C 4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D 4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E 4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F 4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G 4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H 3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I 4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J 4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K 4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L 5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M 5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N 4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O 4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P 4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q 4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R 4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S 5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T 4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U 5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V 5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W 5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X 5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y 4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z 2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [ 4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, // \ backslash 2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ] 3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat 4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _ 2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // ` 4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a 4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b 4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c 4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d 4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e 3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f 4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g 4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h 3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i 4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j 4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k 3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l 5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m 4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n 4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o 4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p 4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q 4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r 4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s 3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t 4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u 5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v 5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w 5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x 4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y 3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z 3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // { 1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // | 3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // } 4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~ }; int data = 12; // DIN pin of MAX7219 module int load = 10; // CS pin of MAX7219 module int clock = 11; // CLK pin of MAX7219 module int maxInUse = 1; //change this variable to set how many MAX7219's you'll use MaxMatrix m(data, load, clock, maxInUse); // define module byte buffer[10]; char string1[] = " A B C D E F G H I J K L M N O P Q R S T U V X W Y Z "; char string2[] = " a b c d e f g h i j k l m n o p q r s t u v x w y z "; char string3[] = " 1 2 3 4 5 6 7 8 9 0 - = "; char string4[] = " ! @ # $ % ¨ & * ( ) _ + "; char string5[] = " ' , . ; ~ ] ´ [ | < > : ^ } ` { / ? "; void setup(){ m.init(); // module initialize m.setIntensity(15); // dot matix intensity 0-15 Serial.begin(9600); // serial communication initialize } void loop(){ /* // this is the code if you want to entering a message via serial console while (Serial.available() > 0){ byte c = Serial.read(); Serial.println(c, DEC); printCharWithShift(c, 100); } delay(100); m.shiftLeft(false, true); */ // print the active sentences printStringWithShift(string1, 100); printStringWithShift(string2, 100); printStringWithShift(string3, 100); printStringWithShift(string4, 100); printStringWithShift(string5, 100); } void printCharWithShift(char c, int shift_speed) { if (c < 32) return; c -= 32; memcpy_P(buffer, CH + 7*c, 7); m.writeSprite(32, 0, buffer); m.setColumn(32 + buffer[0], 0); for (int i=0; i<buffer[0]+1; i++) { delay(shift_speed); m.shiftLeft(false, false); } } void printStringWithShift(char* s, int shift_speed){ while (*s != 0){ printCharWithShift(*s, shift_speed); s++; } } void printString(char* s) { int col = 0; while (*s != 0) { if (*s < 32) continue; char c = *s - 32; memcpy_P(buffer, CH + 7*c, 7); m.writeSprite(col, 0, buffer); m.setColumn(col + buffer[0], 0); col += buffer[0] + 1; s++; } } |
DOT matrix display lichtkrant
Met het volgende script kan je een lichtkrant maken dmv van de HCMAX7219 biblitoheek.
Wat heb je nodig?
1) Arduino HCMAX7219 bibliotheek
let goed op de pinout, deze staat vast in de bibliotheek:
1 2 3 4 5 6 |
MODULE.....UNO/NANO.....MEGA VCC........+5V..........+5V GND........GND..........GND DIN........11...........51 CS (LOAD)..10...........10 CLK........13...........52 |
Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <HCMAX7219.h> #include "SPI.h" /* Set the LOAD (CS) digital pin number*/ #define LOAD 10 /* Create an instance of the library */ HCMAX7219 HCMAX7219(LOAD); void setup() { } void loop() { byte Loopcounter; int Position; /* Clear the output buffer */ HCMAX7219.Clear(); /* SCROLL SOME TEXT 2 TIMES BEFORE MOVING ON */ for (Loopcounter = 0; Loopcounter <= 2; Loopcounter++) { for(Position=0; Position <= 64; Position++) { HCMAX7219.printMatrix("HELLO!! ", Position); HCMAX7219.Refresh(); delay(80); } } /* SCROLL SOME INVERTED TEXT 2 TIMES BEFORE MOVING ON */ HCMAX7219.Invert(INVERTON); for (Loopcounter = 0; Loopcounter <= 2; Loopcounter++) { for(Position=0; Position <= 64; Position++) { HCMAX7219.Clear(); HCMAX7219.printMatrix("HELLO!! ", Position); HCMAX7219.Refresh(); delay(80); } } HCMAX7219.Invert(INVERTOFF); /* SCROLL AND INTEGER NUMBER */ for(Position=0; Position <= 80; Position++) { HCMAX7219.Clear(); HCMAX7219.printMatrix(-12345678, Position); HCMAX7219.Refresh(); delay(80); } /* SCROLL AND INTEGER NUMBER WITH DECIMAL PLACE */ for(Position=0; Position <= 96; Position++) { HCMAX7219.Clear(); HCMAX7219.printMatrix(-12345678, 2, Position); HCMAX7219.Refresh(); delay(80); } } |
Bron:
brainy-bits.com
brainy-bits.com
forum.hobbycomponents.com