MAX7219 and 8 x 7 Segment LED Display

 

The code for this can be downloaded from my GitHub page here.

This display can be found on eBay here. It consists of two 4 digit seven segment LEDS with a MAX7219 driver IC. I have used the LedControl library available from here, the examples show the basic library use, but I wanted to be able to send data over a Serial connection. The MAX7219 presents some problems in sending a string of characters, unlike some other drivers like the TM1638 where a string can be sent, the MAX7219 needs to set each LED individually.

The sketch below allows a number up to 8 characters (or 7 with a minus sign) to be sent to the Serial port. It can be typed into the Serial Monitor, or sent from, say, a Bluetooth receiver. Characters are received one at a time and stored in an array, once all the data has been received the contents of the array are then sent to the relevant LEDs. The number is also available in the string stringNumber once all characters have been received.

The sketch shows the connections for the board to Arduino, five in total are needed, 5 volts, Ground, Data In is connected Arduino pin 12, CLK is connected to Arduino pin 11 and Load OS connected to Arduino pin 10.

Upload the sketch and switch to the Serial Monitor, select 9600 Baud and NL/CR. Type a number and it will appear on the LEDs, to clear and blank the display send a letter.

/***********************************************************
 * 
 * MAX7219 and 8x7 Segment LED Display
 * 
 * by Chris Rouse November 2015
 * 
 Connections
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have a single MAX7219. 
 * 
 * The number to display is read from the Serial Input
 * either type the number in the Serial Monitor
 * or via, for example, Bluetooth.
 * 
 * Only numbers are displayed, but negative 
 * and decimal fractions can be displayedz
 * 
 * For numbers less than zero, for example 0.125
 * must have a leading zero, ie 0.125 and not .125
 * 
 * The LED display can be cleared by sending any letter
 * 
 **********************************************************/
#include "LedControl.h" 
LedControl lc=LedControl(12,11,10,1);
int number[8];
int numLength = 0; // number of digitss
int i = 0;
int dp = 0; // position of decimal point, if any
int j = 0;
boolean minusSign = false; // false if negative number
boolean valid = false;
double displayNumber = 0;
String stringNumber;
void setup() {
  /*
   The MAX7219 is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
  /* Start Serial Monitor */
  Serial.begin(9600);
}

void printDigits() {
lc.clearDisplay(0);
for(i=0;i<numLength;i++) {
if( dp == numLength-i){
lc.setDigit(0,i,number[numLength-(i+1)],true);
}
else{
lc.setDigit(0,i,number[numLength-(i+1)],false);
}
}
}
//
void loop() {
if(Serial.available()){
j = 0;
numLength = 0;
dp=0;
stringNumber ="";
}
// get number to display
while (Serial.available()){
delay(10);
int a = Serial.read();
stringNumber = stringNumber + char(a);
if((a>47 && a< 58) || a == 45){ // ignore line end and decimal point
a = a - 48;
number[j] = a;
numLength=numLength+1;
if(numLength > 8){
numLength = 8; // stop overflow
}
j = j +1;
}
if(a == 46) { // decimal point
dp = numLength;
}
printDigits();
valid = true;
}
// check for a minus
if (number[0] == -3){
lc.setRow(0,numLength-1,B00000001); // print a minus sign
}
if(valid == true){
Serial.println(stringNumber); // this is the string sent to the Serial Port.
valid = false;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s