20 July, 2015

Arduino: output number into digital LED display MAX7219

There are some LED modules for Arduino to display numbers.

For example, this LED display with 8-digits based on the chip MAX7219:

It connects to the Arduino board via five pins:

  1. DIN - data in
  2. CS - chip select
  3. CLK - clock pulse source
  4. GND
  5. 5 V

In this example all elements are connected in this way:

Output number to led display

Here DIN, CS and CLK are connected with pins 7, 6 and 5.

Arduino site has a big LedControl article related to control over the LED matrix. But it doesn't contain a sample that shows how to output whole number.

The code below implements a simple counter, which increases its value once per second and displays it on a digital display:

#include <LedControl.h>

const int DIN_PIN = 7;
const int CS_PIN = 6;
const int CLK_PIN = 5;

LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);

void setup() {
  display.clearDisplay(0);
  display.shutdown(0, false);
  display.setIntensity(0, 10);
}

void displayString(const char *chars8) {
  display.clearDisplay(0);
  for (int i = 0; i < 8 && chars8[i] != 0; i++) {
    display.setChar(0, 7 - i, chars8[i], false);
  }
}

int counter1 = 0;
long counter2 = 99999;

char chars[9] = {};

void loop() {
  snprintf(chars, 9, "%-2d %5ld", counter1, counter2);
  displayString(chars);

  counter1++;
  counter2--;

  if (counter1 > 99) {
    counter1 = 0;
  }

  if (counter2 < 0) {
    counter2 = 99999;
  }

  delay(250);
}

In this code I use displayNumber() function, that writes value for each decimal digit into the appropriate LED segment.

This code requires LedControl library.

Electronic components used in this example:

No comments: