Arduino Uno LED Led 8×8 Dot Matrix MAX7219 + 2 Led 8×8

Arduino LED Matrix Tutorial


You can use practically any Arduino model. For LED matrix make sure you have a board with MAX7219 or 7221 included.
SainSmart MAX7219 Red LED Dot Matrix Display Module MCU Control DIY Kit for Arduino

Theory

The integrated circuit MAX7219 or 7221 is meant to control either 64 individual LEDs or 8 digits of 7-segment displays. If offers two major advantages:
  1. With just 3 digital output pins you can control 64 LEDs per matrix – achieved by using communication over a synchronous Serial Peripherial Interface (SPI) bus
  2. Less current required to light up all LEDs – achieved by very quickly switching on and off individual rows (max 8 LEDs) at a time, so fast it is invisible to a human eye. For example if you consider lighting up the whole matrix with 64 LEDs, normally it would take approximately 64 x 20mA, that is 1.3 A for just one matrix (you might cascade more). With MAX72xx it is decreased to 8 x 20mA = 160 mA.
You can find more details about how it works in the datasheet.
The board with MAX72xx has five input pins:
  1. VCC – this is +5 V power, might be connected directly to Arduino 5 V pin
  2. GND – ground
  3. DIn
  4. CS (Load)
  5. CLK – clock
Pins 3 – 5 can be connected to any of Arduino digital outputs (2, 3, 4 in my example).
There are another five pins on the MAX72xx board – output pins. You can use them to cascade several LED matrices in series (two in my example). I did not find any strict limit for how many you can cascade – up to 8 should be ok. Make sure they are close to each other (recommended less than 10 cm cables). In the code each LED matrix is then referenced by an address (starting with zero for the first one).
I tried to power an Arduino with both my LED matrices from a 9 V battery and it worked, but you cannot count on it for more than just a few minutes.
The purpose of my project is to build a robot – LED matrices are supposed to be the robot’s eyes, so my code contains “bitmaps” displaying three emotions: neutral, anger and sadness:
IMG_20160422_140534
IMG_20160422_140537
IMG_20160422_140541

Circuit

LEDmatrix_bb

LedControl Library

To make the code as simple as possible I utilized LedControl.h library – it was built for applications with MAX72xx. You can install it directly using Arduino IDE, menu Sketch -> Include Library -> Manage Libraries. It allows to control individual LEDs with “setLed” function. In my code, however, I used “setRow” function which offers a better performance and makes the code cleaner.
In the setup function I turned on and cleared the displays, then I lightened up all 128 LEDs to make sure they are working. Finally all three emotions are changing in an infinite loop.

Code

#include "LedControl.h"

/*
 pin 2 is connected to the DataIn 
 pin 4 is connected to the CLK 
 pin 3 is connected to LOAD 
 ***** Please set the number of devices you have *****
 */
LedControl lc=LedControl(2,4,3,2);

const int addrL = 0;  // first LED matrix - Left robot eye
const int addrR = 1;  // second LED matrix - Right robot eye

void setup() {
  /*The MAX72XX is in power-saving mode on startup*/
  lc.shutdown(addrL,false);
  lc.shutdown(addrR,false);
  /* Set the brightness to max values */
  lc.setIntensity(addrL,15);
  lc.setIntensity(addrR,15);
  /* and clear the display */
  lc.clearDisplay(addrL);
  lc.clearDisplay(addrR);

  // turn on all LEDs for a test
  for(int row=0;row<8;row++) {
    lc.setRow(addrL, row, 255);
    lc.setRow(addrR, row, 255);
    delay(100);
  }
  delay(300);
}

void showNeutral() {
  byte left[8] =  {
B01110000,
B10001100,
B10000010,
B10011010,
B01011001,
B01000001,
B00110001,
B00001110};

  displayEmotion(left, left);
}

 

void showSadness() {
  byte left[8] =  {                  //pacman with mouth full open
  B01000000,
  B11100000,
  B01110000,
  B00111000,
  B00011100,
  B00001110,
  B00000111,
  B00000010} ;
  byte right[8] = {                  //pacman with mouth full open
  B01000000,
  B11100000,
  B01110000,
  B00111000,
  B00011100,
  B00001110,
  B00000111,
  B00000010} ;

  displayEmotion(left, right);
}


void showAnger() {
  byte left[8] = {
B01110000,
B10001100,
B10000010,
B10011010,
B01011001,
B01000001,
B00110001,
B00001110};
  byte right[8] = {
B01110000,
B10001100,
B10000010,
B10011010,
B01011001,
B01000001,
B00110001,
B00001110};

  displayEmotion(left, right);
}

void displayEmotion(byte left[8], byte right[8]) {
  lc.clearDisplay(addrL);
  lc.clearDisplay(addrR);
  for(int row=0;row<8;row++) {
    lc.setRow(addrL,row,left[row]);
    lc.setRow(addrR,row,right[row]);
  }
}

void loop() {
  showNeutral();
  delay(300);
  showAnger();
  delay(300);
  showSadness();
  delay(300);
}
Labels: Android, LED Matrix

Thanks for reading Arduino Uno LED Led 8×8 Dot Matrix MAX7219 + 2 Led 8×8 . Please share...!

0 Comment for "Arduino Uno LED Led 8×8 Dot Matrix MAX7219 + 2 Led 8×8 "

Popular Posts

Back To Top