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.
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:
Arduino | 7-segment display |
GND | GND |
3.3V or 5V | VCC |
10 | CS |
11 | CLK |
12 | DIN |
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
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
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
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
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 = 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.
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
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
#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