Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Maddy
Published © GPL3+

Using the DFRobot RGB LED Matrix

Everything you need to know to set up and use the DFRobot RGB LED Matrix in your next project.

BeginnerProtip45 minutes5,970

Things used in this project

Hardware components

DFRobot 64x64 RGB LED Matrix Panel
×1
Elegoo ATmega2560
×1
Male/Male Jumper Wires
×16

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Diagram of Wiring

Code

Matrix Demo

Arduino
Simple moving rainbow text demo
/* Code by Maddy McGee
 * Updated 7/2/18
 * https://www.hackster.io/Maddy
 * 
 * Results: White text that says "LED MATRIX" stays on the screen for 2000ms.
 * Then the text changes color through the rainbow and moves from the bottom of the screen to the top of the screen.
 */
#include <DFRobot_RGBMatrix.h> // Hardware-specific library

//variables for all DFRobot RGB Matrix programs
#define OE    9
#define LAT   10
#define CLK   11
#define A     A0
#define B     A1
#define C     A2
#define D     A3
#define E     A4
#define WIDTH 64
#define HEIGHT  64

DFRobot_RGBMatrix matrix(A, B, C, D, E, CLK, LAT, OE, false, WIDTH, HEIGHT);

//variables for this specific program
boolean isDown;//determines if the text is moving down
int y;//y position of the text
int c;//counter for changing text color
uint16_t text;//the text color 
const uint16_t BACK = matrix.Color333(0, 0, 0);//the background color is black

void setup() {
  matrix.begin();
  text = matrix.Color333(7, 7, 7); //set text color to white
  isDown = true;//text is moving down at start
  y = 28;//text starts in the middle
  
  // fill with the background color
  matrix.fillScreen(BACK);
  
  // format text and display before starting motion
  matrix.setTextSize(1);// size 1 is 8 'pixels' or LEDs tall
  matrix.setTextColor(text);
  matrix.setCursor(2, y);
  matrix.setTextColor(text);
  matrix.println("LED MATRIX");

  delay(2000);
}

void loop() {
  //clear previous text
  matrix.setCursor(2, y);
  matrix.setTextColor(BACK);
  matrix.println("LED MATRIX");

 //move next text
  if (isDown){
    if(y<56){
      y++;
    }else{
      isDown = false;
    }
  }else{
    if(y>0){
      y--;
    }else{
      isDown = true;
    }
  }
  
  //change color for next text
  if(c<43){
    c++;
  } else{
    c = 0;
  }
  changeTextColor(c);
  
  //print new text
  matrix.setCursor(2, y);
  matrix.setTextColor(text);
  matrix.println("LED MATRIX");

  delay(150);
}

// Changes variable text to a color based on input integer from 0 to 43
// If the input is to great color is white, if the input is too low color is black
// As the input increments the colors change red, yellow, green, cyan, blue, magenta, back to almost red
void changeTextColor(int pos) {
  if(pos<0){
    //negative input, black
    text = matrix.Color333(0, 0, 0);
  } else if(pos < 7) {
    //red to yellow
    text = matrix.Color333(7, pos, 0);
  } else if(pos < 15) {
    //yellow to green
    text = matrix.Color333(14-pos, 7, 0);
  } else if(pos < 22) {
    //green to cyan
    text = matrix.Color333(0, 7, pos-14);
  } else if(pos < 30){
    //cyan to blue
    text = matrix.Color333(0, 29-pos, 7);
  } else if(pos < 37){
    //blue to magenta
    text = matrix.Color333(pos-29, 0, 7);
  } else if(pos < 44){
    //magenta to red
    text = matrix.Color333(7, 0, 44-pos);
  } else {
    //input out of bounds, white
    text = matrix.Color333(7, 7, 7);
  }
}

Matrix Methods

Arduino
A runthrough and demonstrations of the methods for the DFRobot RGB Matrix library
/* Code by Maddy McGee
 * Updated 7/2/18
 * https://www.hackster.io/Maddy
 * 
 * A runthrough and demonstation of matrix methods for the DFRobot RGB Matrix library
 */
#include <DFRobot_RGBMatrix.h> // Hardware-specific library

/* Below is the pin definition
 * These pins can be swapped as long as the code is changed to reflect such change.
 * The pins must also support the same capabilities (i.e. DIO vs PWM)
 * Other pins are set in the library and need to follow the wiring schematic provided.
 */
#define OE    9
#define LAT   10
#define CLK   11
#define A     A0
#define B     A1
#define C     A2
#define D     A3
#define E     A4
#define WIDTH 64 // # of LEDs vertically
#define HEIGHT  64 // # of LEDs horizontally

DFRobot_RGBMatrix matrix(A, B, C, D, E, CLK, LAT, OE, false, WIDTH, HEIGHT);

//Colors on the screen are created in RGB with each color an integer ranging from 0 to 7
int r = 7;
int g = 7;
int b = 7;

//The coordinate system for the matrix are integers with (0,0) being the upper leftmost corner
// 1 = one LED, so (5, 10) is five LEDs to the right and 10 LEDs down
int x = 5;
int y = 10;
int x2 = 27;
int y2 = 32;

int wide = 40;
int high = 17;
int radius = 4;

void setup() {
  matrix.begin();//This line must come before modifying the matrix screen

  //This is the format for creating a color
  //A color can be int or uint16_t
  int white = matrix.Color333(r, g, b);
  uint16_t black = matrix.Color333(0, 0, 0);
  int darkRed = matrix.Color333(1, 0, 0);
  uint16_t brightRed = matrix.Color333(7, 0, 0);
  int green = matrix.Color333(0, 7, 0);
  int blue = matrix.Color333(0, 0, 7);
  int magenta = matrix.Color333(7, 0, 7);
  int cyan = matrix.Color333(0, 7, 7);
  int yellow = matrix.Color333(7, 7, 0);

  //Fills the screen one color
  matrix.fillScreen(darkRed);

  //Set a single LED to one color
  //This puts the pixel in the lower right corner
  matrix.drawPixel(WIDTH - 1, HEIGHT - 1, white);

  //Draw and fill a box, the (x,y) coordinates are the upper left corner
  matrix.fillRect(x, y, wide, high, brightRed);

  //You can notice each element stacks based on order of method call
  delay(1000);
  
  //Draw the perimeter of a box, the (x,y) coordinates are the upper left corner
  matrix.drawRect(x, y, wide, high, black);

  //Without delays each element can appear to be drawn simultaneously
  //But if you introduce simulated movement screen refreshing becomes noticeable
  delay(500);
  
  //Draw and fill a circle with the (x,y) coordinate at the center
  matrix.drawCircle(x,y,radius,yellow);
  //Draw the perimeter of a circle with the (x,y) coordinate at the center
  matrix.fillCircle(x2,y2,radius,cyan);

  //Draw a line from one point to another
  matrix.drawLine(x,y,x2,y2,green);

  //Text can be printed to the matrix
  //When drawn text starts in the upper left corner by default
  //If the cursor position is set that will be the upper left of the text
  //Text color can be set 
  //Text size can be set with size * 8 = height of text, size 1 = 8 LEDs, size 2 = 16 LEDs, etc
  matrix.setTextColor(blue);
  matrix.print("Blue");
  matrix.setTextColor(magenta);
  matrix.setTextSize(2);
  matrix.setCursor(x2,y2);
  matrix.println("Not blue");
  
  
}

void loop() {

}

Credits

Maddy
3 projects • 58 followers
Computer Science Engineering second year at The Ohio State University, in love with code and electronics.
Contact

Comments

Please log in or sign up to comment.