Skip to content

Commit 61e1197

Browse files
preparing arduino driver to stand alone
0 parents  commit 61e1197

40 files changed

+5536
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Ardunio Driver for RGB LED Matrix
2+
3+
To use this driver in the Arduino IDE, add the folder `RGB_LED_Matrix_Lib` as a library as described in [this document](https://www.arduino.cc/en/Guide/Libraries).
4+
5+
This driver uses SPI to transfer bits to the shift registers.
6+
7+
## Arduino ATmega Boards
8+
9+
The default wiring for connecting the RGB LED Matrix to an Arduino using the ATmega328 micro-controller (e.g., Uno, Nano, etc) is:
10+
11+
| LED Matrix Connection | Arduino Uno/Nano Pin | Mega 2560 Pin | Notes |
12+
|:-:|:-:|:-:|---|
13+
| **+5V** | 5V | 5V | |
14+
| **GND** | GND | GND | |
15+
| **SER** | 11 | 51 | SPI MOSI Pin |
16+
| **CLK** | 13 | 52 | SPI SCK Pin |
17+
| **LATCH** | 10 | 10 | Pin 10 is default, but this can be changed when creating the `RGBLEDMatrix` object. |
18+
| **SER**' | _unused_ | _unused_ | Used to chain multiple boards together. Would connect to the SER of the next board.|
19+
20+
Note that the SPI MISO pin is unused.
21+
22+
## 3.3v Logic Level
23+
To use the RGB LED Matrices designed in this project with microcontroller boards that use a 3.3V logic level, you must convert the 3.3V logic signals to 5V levels to work with the shift registers. You can easily use a 74HCT125 buffer/line driver chip to do this transformation. For example, you can wire a Teensy 3.6, which is a 3.3v device, to a 74HCT125 chip in the manner shown in the diagram below to get all power and signal lines required to drive the RGB LED Matrix while the Teensy is connected to USB power:
24+
25+
![Teensy 3.6 Circuit to Drive RGB LED Matrix](docs/teensy36_5V_logic_circuit.png)
26+
27+
An alternative to using this 74HCT125 circuit would be to replace the 74HC595 shift registers on the RGB LED Matrix with the 74HCT595 variety. However, this might be more expensive.
28+
29+
## Teensy 3.x Boards
30+
Using the Teensy 3.x as the driving micro-controller for the RGB LED Matrix is a good choice because it's higher clock speed will allow your code to do more work without interrupting the PWM activities that are also happening at the driver level.
31+
32+
To use this Teensy 3.x driver in the Arduino IDE, add the folder `RGB_LED_Matrix_Lib` as a library as described in [this document](https://www.arduino.cc/en/Guide/Libraries). Also, ensure that the Arduino IDE has been updated to support Teensy development ([see here for more information](https://www.pjrc.com/teensy/td_download.html)).
33+
34+
## ESP8266 Boards
35+
ESP8266 boards are generally 3.3v logic level boards.
36+
37+
| LED Matrix Connection | Wemos D1 Mini | NodeMCU | Notes |
38+
|:-:|:-:|:--:|---|
39+
| **+5V** | 5V | Vin | |
40+
| **GND** | GND | GND | |
41+
| **SER** | D7 | D7 | SPI MOSI Pin |
42+
| **CLK** | D5 | D5 | SPI SCK Pin |
43+
| **LATCH** | D8 | D8 | SS pin |
44+
45+
## Color Modes
46+
This driver can support either 6-bit or 24-bit color. By default, this library uses 6-bit color. You can enable 24 bit color in this library by setting the preprocessor macro `TWENTY_FOUR_BIT_COLOR` to a value of 1 (note, not in your `ino` file, but at compile time for all files). You can do this either by editing the `RGBImage.h` file or setting a compiler flag. However, note that 24 bit color requires more RAM than an Arduino Uno or Nano has. Due its memory requirements, 24 bit color should work on most 32 bit boards and the Arduino Mega 2560. 24 bit color has been tested to work on the following boards:
47+
48+
* Teensy 3.6
49+
* Arduino Mega 2560
50+
* Wemos D1 mini Lite
51+
* NodeMCU
52+
53+
## Bit Layouts
54+
This driver can support two different bit layouts. The default bit layout assumes each RGB LED is a single unit and each column is wired up with the RGB bits consecutively. That is, for a 4x4 matrix, the bit layout would look like this:
55+
56+
![Default Bit Layout for RGB LED Matrix](docs/rgb-led-matrix-bit-layout-default.png)
57+
58+
The second supported bit layout groups all colors together in column order, then sends each color group in Red-Green-Blue order. For a 4x4 matrix, the bit layout would look like this:
59+
60+
![Default Bit Layout for RGB LED Matrix](docs/rgb-led-matrix-bit-layout-color-groups.png)
61+
62+
When constructing the the `RGBLEDMatrix` object, the fourth argument is optional and it take a `RGBLEDBitLayout` enum value indicating which bit layout you are using. This argument defaults to `INDIVIDUAL_LEDS`, which is the first layout described above. The other potential value is `RGB_GROUPS`.
22.9 KB
Loading
20.5 KB
Loading

docs/teensy36_5V_logic_circuit.png

150 KB
Loading
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <RGBLEDMatrix.h>
2+
#include <RGBImage.h>
3+
#include <Glyph.h>
4+
#include <RGBAnimation.h>
5+
#include <RGBAnimationSequence.h>
6+
#include <TimerAction.h>
7+
8+
//
9+
// This program will cause a diagonal rainbow gradient to cycle through on the LED matrix
10+
// Will work in either 6-bit or 24-bit color.
11+
//
12+
#define LOOP_COUNTER_MAX 8000
13+
14+
const int ROW_COLOR_LIST_SIZE = 19;
15+
ColorType rowColors[ROW_COLOR_LIST_SIZE];
16+
17+
ColorType currentColor = RED_COLOR;
18+
19+
ColorType redIncrement = 1 << RED_BIT_SHIFT;
20+
ColorType greenIncrement = 1 << GREEN_BIT_SHIFT;
21+
ColorType blueIncrement = 1 << BLUE_BIT_SHIFT;
22+
23+
const int SEQUENCE_LENGTH = 7;
24+
25+
// This is the list of main colors we want to cycle through. Agradient will be calculated between them.
26+
ColorType colorSequence[SEQUENCE_LENGTH] = {
27+
RED_COLOR,
28+
RED_COLOR+GREEN_COLOR,
29+
GREEN_COLOR,
30+
GREEN_COLOR+BLUE_COLOR,
31+
BLUE_COLOR,
32+
RED_COLOR+BLUE_COLOR,
33+
RED_COLOR+GREEN_COLOR+BLUE_COLOR,
34+
};
35+
36+
// This is the list of step values needed to transition to the next color.
37+
unsigned int colorIncrements[SEQUENCE_LENGTH] = {
38+
greenIncrement,
39+
redIncrement,
40+
blueIncrement,
41+
greenIncrement,
42+
redIncrement,
43+
greenIncrement,
44+
greenIncrement+blueIncrement
45+
};
46+
47+
// since ColorTYpe is an unsigned value, we need to keep track whether we are
48+
// adding or subtracting the increment.
49+
ColorType incrementType[SEQUENCE_LENGTH] = {
50+
true,
51+
false,
52+
true,
53+
false,
54+
true,
55+
true,
56+
false
57+
};
58+
59+
int sequenceIdx = 0;
60+
int loopCounter = 0;
61+
62+
int getCurrentIdx() {
63+
return sequenceIdx;
64+
}
65+
66+
int getNextIdx() {
67+
if (sequenceIdx == (SEQUENCE_LENGTH-1)) {
68+
return 0;
69+
}
70+
else {
71+
return sequenceIdx+1;
72+
}
73+
}
74+
75+
ColorType getNextColor() {
76+
if ( currentColor == colorSequence[getNextIdx()] ) {
77+
sequenceIdx++;
78+
if (sequenceIdx == SEQUENCE_LENGTH) {
79+
sequenceIdx = 0;
80+
}
81+
}
82+
83+
if (incrementType[getCurrentIdx()]) {
84+
currentColor += colorIncrements[getCurrentIdx()];
85+
}
86+
else {
87+
currentColor -= colorIncrements[getCurrentIdx()];
88+
}
89+
90+
return currentColor;
91+
}
92+
93+
RGBLEDMatrix* leds;
94+
95+
void setup() {
96+
// put your setup code here, to run once:
97+
98+
for (int i = 0; i < ROW_COLOR_LIST_SIZE; i++) {
99+
rowColors[i] = getNextColor();
100+
}
101+
102+
leds = new RGBLEDMatrix(10,10);
103+
leds->setup();
104+
leds->image().paintColor(BLACK_COLOR);
105+
leds->startScanning();
106+
}
107+
108+
void loop() {
109+
// put your main code here, to run repeatedly:
110+
if (loopCounter == LOOP_COUNTER_MAX) {
111+
for (int i = 0; i < ROW_COLOR_LIST_SIZE - 1; ++i) {
112+
rowColors[i] = rowColors[i+1];
113+
}
114+
115+
rowColors[ROW_COLOR_LIST_SIZE-1] = getNextColor();
116+
117+
leds->startDrawing();
118+
for (int i = 0; i < ROW_COLOR_LIST_SIZE; i++ ) {
119+
// determine if we are in the upper or lower diagonal
120+
if (i/leds->rows() == 0) {
121+
leds->image().drawLine(i,0,0,i,rowColors[i]);
122+
}
123+
else {
124+
leds->image().drawLine(leds->rows()-1,i%leds->rows() + 1,i%leds->rows() + 1,leds->columns()-1,rowColors[i]);
125+
}
126+
}
127+
leds->stopDrawing();
128+
129+
loopCounter = 0;
130+
}
131+
else {
132+
loopCounter++;
133+
}
134+
135+
leds->loop();
136+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <RGBLEDMatrix.h>
2+
#include <RGBImage.h>
3+
#include <Glyph.h>
4+
#include <RGBAnimation.h>
5+
#include <RGBAnimationSequence.h>
6+
#include <TimerAction.h>
7+
8+
//
9+
// This program will cause a diagonal rainbow gradient to cycle through on the LED matrix
10+
// Will work in either 6-bit or 24-bit color.
11+
//
12+
13+
RGBLEDMatrix leds(8,8);
14+
15+
const int ROW_COLOR_LIST_SIZE = 15;
16+
ColorType rowColors[ROW_COLOR_LIST_SIZE];
17+
18+
ColorType currentColor = RED_COLOR;
19+
20+
ColorType redIncrement = 1 << RED_BIT_SHIFT;
21+
ColorType greenIncrement = 1 << GREEN_BIT_SHIFT;
22+
ColorType blueIncrement = 1 << BLUE_BIT_SHIFT;
23+
ColorType greenAndBlueIncrement = greenIncrement|blueIncrement;
24+
const int SEQUENCE_LENGTH = 7;
25+
26+
// This is the list of main colors we want to cycle through. Agradient will be calculated between them.
27+
ColorType colorSequence[SEQUENCE_LENGTH] = {
28+
RED_COLOR,
29+
RED_COLOR+GREEN_COLOR,
30+
GREEN_COLOR,
31+
GREEN_COLOR+BLUE_COLOR,
32+
BLUE_COLOR,
33+
RED_COLOR+BLUE_COLOR,
34+
RED_COLOR+GREEN_COLOR+BLUE_COLOR,
35+
};
36+
37+
// This is the list of step values needed to transition to the next color.
38+
ColorType colorIncrements[SEQUENCE_LENGTH] = {
39+
greenIncrement,
40+
redIncrement,
41+
blueIncrement,
42+
greenIncrement,
43+
redIncrement,
44+
greenIncrement,
45+
greenAndBlueIncrement
46+
};
47+
48+
// since ColorTYpe is an unsigned value, we need to keep track whether we are
49+
// adding or subtracting the increment.
50+
boolean incrementType[SEQUENCE_LENGTH] = {
51+
true,
52+
false,
53+
true,
54+
false,
55+
true,
56+
true,
57+
false
58+
};
59+
60+
int sequenceIdx = 0;
61+
int loopCounter = 0;
62+
63+
int getCurrentIdx() {
64+
return sequenceIdx;
65+
}
66+
67+
int getNextIdx() {
68+
if (sequenceIdx == (SEQUENCE_LENGTH-1)) {
69+
return 0;
70+
}
71+
else {
72+
return sequenceIdx+1;
73+
}
74+
}
75+
76+
ColorType getNextColor() {
77+
if ( currentColor == colorSequence[getNextIdx()] ) {
78+
sequenceIdx++;
79+
if (sequenceIdx == SEQUENCE_LENGTH) {
80+
sequenceIdx = 0;
81+
}
82+
}
83+
84+
if (incrementType[getCurrentIdx()]) {
85+
currentColor += colorIncrements[getCurrentIdx()];
86+
}
87+
else {
88+
currentColor -= colorIncrements[getCurrentIdx()];
89+
}
90+
91+
return currentColor;
92+
}
93+
94+
void setup() {
95+
leds.setup();
96+
97+
for (int i = 0; i < ROW_COLOR_LIST_SIZE; i++) {
98+
rowColors[i] = getNextColor();
99+
}
100+
101+
leds.image().paintColor(BLACK_COLOR);
102+
leds.startScanning();
103+
}
104+
105+
void loop() {
106+
// put your main code here, to run repeatedly:
107+
if (loopCounter == 10000) {
108+
for (int i = 0; i < ROW_COLOR_LIST_SIZE - 1; ++i) {
109+
rowColors[i] = rowColors[i+1];
110+
}
111+
112+
rowColors[ROW_COLOR_LIST_SIZE-1] = getNextColor();
113+
114+
leds.startDrawing();
115+
for (int i = 0; i < ROW_COLOR_LIST_SIZE; i++ ) {
116+
// determine if we are in the upper or lower diagonal
117+
if (i/leds.rows() == 0) {
118+
leds.image().drawLine(i,0,0,i,rowColors[i]);
119+
}
120+
else {
121+
leds.image().drawLine(leds.rows()-1,i%leds.rows() + 1,i%leds.rows() + 1,leds.columns()-1,rowColors[i]);
122+
}
123+
}
124+
leds.stopDrawing();
125+
126+
loopCounter = 0;
127+
}
128+
else {
129+
loopCounter++;
130+
}
131+
132+
leds.loop();
133+
}

0 commit comments

Comments
 (0)