Arduino Tutorials

How to connect a 7-segment display to the Arduino

7-segment display on the Arduino

There are a lot of displays for the Arduino, but none of them has such an old-school factor as the 7-segment display. Back To The Future? Here you go! In this tutorial you will learn how to connect this display and show numbers on it.

We use a MAX7219 display on which you can display a total of 8 digits.

AZDelivery MAX7219 Led Modul 8 Bit 7-Segmentanzeige LED Display für Arduino und Raspberry Pi
✔️ Versorgungsspannung: 5V; ✔️ 0.5" 7-Segmentanzeige; ✔️ Driver IC: MAX7219;...
5,49 EUR

How to connect it to the Arduino

In order to connect the 7-segment display, you need three free digital pins on the Arduino. You can operate the display either with 3.3V or 5V. In this tutorial the connection is done as follows:

Arduino7-segment display
GNDGND
3.3V or 5VVCC
10CS
11CLK
12DIN

Mostly the display comes with soldered pins, so you need 5 cables of the type male-female.

The right library for the 7-segment display

For the control of the display there is a library to make your life easier. So open your Library Manager in the Arduino IDE, search for LedControl and install the current version.

Library LedControl.h in Library Manager to control the 7-Segment display

The sketch for your first test

For the very first test with the 7-segment display, a single digit on the display should be sufficient. As so often, the first step is to integrate the above mentioned library:

#include "LedControl.h"

Then you define to which digital pins you have connected the display. Here the order DIN, CLK, CS is crucial. The last argument in the following line of code is the number of displays you want to control. Theoretically you could use the library to display so many digits, that it would be enough for the global debt clock of the next decades – but for now we stay with a 7-segment display 😉

LedControl lc=LedControl(12,11,10,1);

Switch on the display and show one digit

Let us come to the setup() function. Here you do three things at the beginning of the sketch: wake the display from sleep mode, adjust the brightness and delete all digits that might still be visible on it.

lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);

As for brightness, you can give the function lc.setIntensity() a number from 0 to 15.

So let’s come to the crucial moment. Also in the setup function, we write the number 9 in the first field of the 7-segment display (far right):

  lc.setDigit(0, 0, 9, false);

If you want to write the 9 in the first field on the very left, the code would be as follows. As usual, you start counting at 0 – from the righthand side. The leftmost field is then numbered 7:

  lc.setDigit(0, 7, 9, false);

What happens if you enter a 10 instead of the 9? Then this decimal number is represented in the hexadecimal system, i.e. with the letter A. This goes up to the number 15, which is then output accordingly as F.

Show long numbers on the 7-segment display

So, assigning a number to each field of the display works, but may be quite cumbersome. What if you just want to output the number 12345678 at once? With two helper functions this is no problem.

First you define 8 variables, which later contain the single digits of your number. The variable a will contain the first digit and h the last digit.

int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;

Then you need a (at first sight a bit complicated) function that divides your number into these digits. You can do this with the operator Modulo, which calculates the rest of the division of one integer by another.

The last digit of the number 12345678 is the 8, which should be in the field on the very right. With the following calculation you get this digit and store it in the variable h:

h = number % 10;

This calculation is written out like this: h = 12345678 : 10 = 1234567,8 – the rest behind the decimal point is the 8, our number on the very right.

Continue with the penultimate digit. Here you first check, if the number to represent has two digits at all (it has, your number even has 8). If so, you first divide it by 10 to get the number 1234567. Here the digit after the decimal point is omitted, because we created the variable number with long and with this type the digits after the decimal point are deleted automatically, because it can only store integer numbers.

Another calculation with the Modulo then gives you the second last digit 7:

if (number > 9) {
  g = (number / 10) % 10;
}

These calculations are done for all the digits in your number, as you can see in the full sketch below. Once the function has run through, all you have to do is to bring all the digits to the 7-segment display. To do this, you use another function that checks how many digits your number has and displays these digits stored in the variables a – h:

lc.setDigit(0, 0, h, false);

if (number > 9) {
  lc.setDigit(0, 1, g, false);
}
.
.
.

The whole sketch for the 7-segment display

Here is the whole sketch with which you bring numbers to the display. Save different numbers with a maximum of eight digits in the variable number for testing.

#include "LedControl.h"

long number = 1234;

//Variables for the digits
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;

LedControl lc=LedControl(12,11,10,1);

void numberIntoDigits() {

  h = number % 10;

  if (number > 9) {
    g = (number / 10) % 10;
  }
  if (number > 99) {
    f = (number / 100) % 10;
  }
  if (number > 999) {
    e = (number / 1000) % 10;
  }
  if (number > 9999) {
    d = (number / 10000) % 10;
  }
  if (number > 99999) {
    c = (number / 100000) % 10;
  }
  if (number > 999999) {
    b = (number / 1000000) % 10;
  }
  if (number > 9999999) {
    a = (number / 10000000) % 10;
  }
}

void displayNumber() {

  lc.setDigit(0, 0, h, false);

  if (number > 9) {
    lc.setDigit(0, 1, g, false);
  }
  if (number > 99) {
    lc.setDigit(0, 2, f, false);
  }
  if (number > 999) {
    lc.setDigit(0, 3, e, false);
  }
  if (number > 9999) {
    lc.setDigit(0, 4, d, false);
  }
  if (number > 99999) {
    lc.setDigit(0, 5, c, false);
  }
  if (number > 999999) {
    lc.setDigit(0, 6, b, false);
  }
  if (number > 9999999) {
    lc.setDigit(0, 7, a, false);
  }
}

void setup() {
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  numberIntoDigits();
  displayNumber();
  }

void loop() { 
}

What next?

Put the 7-segment display in your DeLorean and take off to 1985! Joking aside, how about a retro timer, for example? Or you could give the ISS cube or your LEGO ISS a countdown so you don’t miss the opportunity to watch the real ISS fly by.

Letzte Aktualisierung am 2021-01-09 / Affiliate Links / Bilder von der Amazon Product Advertising API

You may also like

Comments are closed.