Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(10) Four digit, 7-segment led display for Arduino based on the TM1637 driver

by Floris Wouterlood – August 9, 2018

Sometimes it is functional and pleasant to display Arduino sensor data on a no nonsense, high contrast numerical display instead of a lcd display or a small graphical screen. For those applications 7-segment led displays exist that can supply the desired visual output. Here we discuss the basics of connecting a four-digit, 7-segment display to an Arduino. The display is controlled by a TM1637 integrated circuit which does the nitty gritty work of lighting up the correct segments that compose each of the digits when input from the microcontroller board arrives. In this example a Dallas DS18B20 temperature sensor is connected with one of the pins of the microcontroller board to supply sensor data. Two sketches are provided: one test sketch and one that governs collecting and displaying temperature data.

Data collected with a sensor connected to an Arduino can be presented in many ways. Most simple is via Serial Monitor. There are visually more exciting ways, for instance through lcd-, OLED-, TFT- or other displays. Here we focus on what is in fact forty odd years digital technology: the 7-segment led display (these devices superseded even older Nixie vacuum tubes with 10-digit filament constructions – history goes back to 1908 – Wikipedia). The advantage of a numerical digital display is that there is immediate visual output. Many manufacturers, mostly Chinese, are flooding the market with cheap, four-digit, 7-segment displays in different colors, ready to be connected with an Arduino. The type discussed here is based on the TM1637 integrated circuit manufactured by Titan Micro Electronics. This chip is surface mounted on the back of the board (figure 1).

Figure 1: Front and back view of a board with a four-digit, TM1637 driven 7-segment led display. The inset shows the Titan TM1736 chip on the back of the board. The advantage of this display is that only two pins of the Arduino are required to control the display. The board is 3.3 – 5V compatible. No extra resistors or capacitors are needed.

Short excursion into 7-segment led display technology

Figure 2: Basics of 7-segment display technology. TecWiring diagram. One ‘digit’ of the display consists of a mask that creates the segments. Any number between 0 and 9 can be created by lighting up a combination of segments. Each segment and the decimal ‘dot’ has its own led.

A 7-segment display is just a mask with 7 leds behind it (figure 2). These leds are identified with letters: ‘a’ through ‘ g’ while ‘DP’ is reserved for the decimal (separator) dot. It is amazing that seven leds lighting up in various combinations can be used to display any number between 0 and 9 (or, in hexadecimal, between 0 and F). That the construction allows an 8th led available for generating a decimal point is even more wonderful. The on-off state of eight leds can be controlled with one byte of information. That, and the robustness and the modest energy consumption of individual leds makes the 7-segment display interesting for application as display in an Arduino microcontroller environment. Even some letters can be generated, for instance my first initial, ‘F’ can be generated by illuminating bars ‘a’,’f’,’g’ and ‘e’. We will exploit customization to generate “oC” (degrees Celsius) in combination with a numerical representation of the ambient temperature.

Wiring the four-digit 7-segment display
As can be seen in figure 3 the device needs 5V power supply and GND. The Arduino provides enough power to supply the device.

Figure 3: Wiring diagram. A Dallas DS18B20 temperature sensor provides input to pin 10 of an Arduino Nano (white wire). Output of the Nano ) to the 7-segment led display runs via pins D8 (data; yellow wire) and D9 (clock; green wire. On the display the third and fourth ‘digits’ are custom characters.

Pin connectivity with an Arduino
The pins of the 4-digit, 7-segment board are well marked: GND, VCC, DIO and CLK. DIO stands for data in-out, and CLK stands for clock.
DIO and CLK can be connected to any unused available pin on the Arduino. Via software instructions the selected pins are communicated to the microprocessor.

Basic sketch for testing the display
A very basic sketch that brings the led display to life (and no more than that) is discussed in this section: TM1637_7_seg_led_display.ino. After compiling and uploading the display will start counting from 0 to 9999. To compile the program the sketch uses the library ‘TM1637Display.h’ This particular library (there are several ‘competing’ libraries each with its own pros and cons) can be downloaded from https://github.com/avishorp/TM1637.

// TM1637_7_seg_led_display
// adapted by Floris Wouterlood
// open source – use as you wish

#include <TM1637Display.h> // library needed!

const int CLK = 9;                        // set the CLK pin connection to the display
const int DIO = 8;                        // set the DIO pin connection to the display
int i = 0;                                         // variable to interate

TM1637Display display(CLK, DIO); // set up the 4-Digit Display.

void setup(){
display.setBrightness(0x0a);           // set the display to maximum brightness

void loop(){

for(i = 0; i < 9999; i++){                     // raise counter one unit
display.showNumberDec(i);            // display i
delay (250);                                        // quarter second delay

Program your own custom characters
The library TM1637Display.h offers the option to create custom characters. Because the next step is to attach a temperature sensor to the display we need to create ‘oC’ to indicate that temperature is expressed in degrees Celsius. This is done by creating an array named DEGREES [ ] :

const uint8_t DEGREES [ ] = {
0x0, 0x0,
SEG_A | SEG_B | SEG_G | SEG_F, // superscript o
SEG_A | SEG_F | SEG_E | SEG_D, // C

In this array the first two ‘digits’ are blank (0x0), the third ‘digit’ is created by activating segments ‘a’, ‘b’ ,’g’ and ‘f’, while the fourth ‘digit’ creates the ‘C’ by firing segments ‘a’, ‘f’, ‘e’ and ‘d’.
An example sketch to generate your own 7-segment custom characters is provided at the end of this post: TM1637_7_seg_led_display_customchar.ino

Determining temperature

For temperature measurement my favorite sensor is the Dallas DS18B20 (figure 4). In a previous post this sensor has been discussed. Briefly, this is a small, cheap and accurate sensor with a range between -55 and 125 oC (-67°F to +257°F). Accuracy in the working range (-10 to +85 ºC) is 0.5 ºC.

The DS18B20 is a one-wire device: for communication with an Arduino only one pin on the microcontroller board is necessary. A DS18B20 has a unique 64-bit identification number (48 bits serial number and 8-bit family number). Calling this ID number produces the response only of the sensor with that ID while other, identical sensors wired through the same line do not respond. A pull-up resistor with a value of 4.7 kΩ between the data wire and 5V is necessary to maintain a stable signal (see wiring diagram, figure 3). This resistor is compulsory; the sensor may not be recognized by the Arduino or readings may be unreliable if it is not present or has an incorrect value.

The sketch is called TM1637_7_seg_display_DS18B20.ino. You will see that the sketch needs the following libraries: <TM1637Display.h>, <OneWire.h> and <DallasTemperature.h>. As the DS18B20 is exhaustively discussed in previous paper I refer here to those paper.

The sketch initializes the 7-segment display and the DS18B20, then requests data (temperature) from the DS18B20, and displays the temperature on the first two ‘digits’ followed by the two custom characters, as in figure 3. It is a very simple and crude sketch that needs improvement, but it serves here mainly as ‘proof of the pudding’


Big advantages of the four digit, 7-segment display are that the presentation of data is cool and straightforward and that it does not need ‘hard’ pins on the Arduino, meaning that in applications that heavily rely on specific pins two ‘leftover ‘ pins can be selected and software assigned.
It is important to remember that a four-digit led device has as many as 28 segments and, in some versions, an extra set of four dots plus two dots that act as the colon separating the left and right two digits (displays used in digital clocks). So in the end there may be 34 leds. As one segment can draw about 15 mA, total power consumption while displaying the number ‘8888’ and all the dots/colon can theoretically be expected to be as much as 510 mA. That is a lot of power for a small Arduino. Four-digit, 7-segment displays are therefore known as power hungry. It is noticeable here that in the 1970’s the first 7-segment led displays were hailed as ‘extremely energy saving’. In those days their 10-filament Nixie vacuum tube predecessors could each consume 600 mW which made them unsuitable for battery-operated devices. Of course it is not the intention to display all the time but nevertheless, in power bank operated environments one may experience rapid exhaustion of the power supply. One solution to reduce power consumption is by means of multiplexing (implemented in a competing design based on the MAX7219 chip). Another solution is to use high intensity, low power leds to light up the segments. LCD displays, OLED displays and E-paper displays are known to be much less power hungry than 7-segment led displays and compete with 7-segment led displays because of this feature in many applications.
A very nice example of a nostalgic four-tube ‘Nixie’ vacuum tube display can be found at https://www.tubedepot.com/products/nixie-clock-with-4-in-14-tubes-acrylic-base

Downloadable sketches (packed into a zip file: ‘TM1637_7_seg_led_display.zip

the zip file contains:
• TM1637_7_seg_led_display.ino
• TM1637_7_seg_led_display_customchar.ino
• TM1637_7_seg_display_DS18B20.ino

Additional sources of information