Learn to code

ARDUINO & CODES

Previous Post
How to connect and use a 16×2 character Display with Arduino
Next Post
Creating Custom Characters on a 16×2 LCD with Arduino

Controlling a TM1637 4 Digit 7 segment display with Arduino

in Arduino Displays

In this tutorial, you will learn how you can control TM1637 4-digit 7-segment displays with Arduino. This display is perfect for displaying sensor data.

In this tutorial you will learn how to display numbers and text on the display. You will learn to use two methods for displaying them.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 6
TM1637 4-digit 7-segment display TM1637 4-digit 7-segment display × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

TM163 Display

Most displays require a lot of connection pins. That is quite complex and does not leave much space for other sensors or modules to connect with the Arduino Board. The TM1637 display module only uses four pins. Two pins are required for the power connections and the other two are used to control display.

A 7-segment display contain individually addressable LEDs. The segments are labeled from A to G. To be able to set the LEDs you should know which segment corresponds to which letter. The image shows how the display segments are labeled.

TM1637 4-Digit 7-Segment Display Specifications

FeatureSpecification
Operating voltage 3.3V – 5.5V
Operating Current 80 mA
Operating Temperature-10ºC to +80ºC
Alpha-numeric digits4
Luminance levels8

BreadBoard Layout

TM1637 Display Connections

TM1637 4-Digit Display Arduino Board
VCC5V
GNDGND
Clock Pin2
Data Pin3

The Code

// Code to control a TM1637 diplay. 
// Firstly, it will display the text PLAY. 
// Secondly, a countdown starts.
// Thridly, the display will show the text DONE.
// https://www.arduinoplatform.com

#include <TM1637Display.h>

// CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
// In this library, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};
const uint8_t DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};


// Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(2, 3);

void setup(){
  // Set brightness of the LED
  display.setBrightness(4);
  // Clear the display
  display.setSegments(OFF);
}

void countdown() {
  for(int i=1500; i>0; i--) {
    // showNumberDec is a function for displaying numeric values,
    display.showNumberDec(i, true, 4, 0);
    delay(80);
  }
}
void displayPlay() {
  display.setSegments(PLAY);
  delay(2000);
}
void displayDone() {
  display.setSegments(DONE);
  delay(2000);
}
void loop(){
  displayPlay();
  countdown();
  displayDone();
}

Code Explanation

The code used in this project is quite simple however, there are some functions that I want to highlight.

Main function that we will use in this tutorial:

setSegments() – Set the value of the segments of each digit (A – G)

showNumberDec() – Display a decimal number on the display

setBrightness() – Set the brightness of the display

clear() – Clear the display

To be able to use the above functions you will need to install the tm1637 library. To install the library you can download a zip from github or install it. In the Arduino IDE,  go to Sketch -> Include Library -> Add .ZIP Library….

Another way is to install the library from the Arduino IDE directly. Go to Tools -> Manage Libraries. The Library Manager will open and update the list of installed libraries.

You can search for ‘tm1637’ and look for the library by Avishay Orpaz. Select the latest version and install it.

Constant variables

After including the libraries you can start coding the sketch we are defining the pins that are connected to the display. You are going to connect the clock and data pin to the Arduino Board. In the setup part, you do not have to set these Arduino Pins to an output mode since that will be handled by the library.

We are going to define three constants OFF, PLAY and DONE.  The PLAY array contains a set of 4 binary values. The binary values represent the values that are going to be turned on to display the word PLAY. So every 1 and 0 correspond to one of the seven segments of the display. These segments are explained in an earlier part of the tutorial.

For instance, if we want to display the letter P we can turn on segments by using the binary number 1. Segments A, B, E, F and G are going to be turned on. While the other segments are will be off. If you use this function with the library that I use to remember that it is used in reverse order. So the last number, 1 in this case,  in one set of binary values is segment A.

Furtheremore, the DONE array consists of segments associated with the display. You can see that the binary values are replaced by code that represent the segments of the display.

The OFF constant consist of an array with zero’s.

// Code to control a TM1637 diplay. 
// Firstly, it will display the text PLAY. 
// Secondly, a countdown starts.
// Thridly, the display will show the text DONE.
// https://www.arduinoplatform.com

#include <TM1637Display.h>

// Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(2, 3);

// CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
// In this library, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};
const uint8_t DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};

Setup Part

The set.segment function passes an array of values to the display. In our case, it will set every segment of each digit off.

This function sets the brightness of the display (as the name suggests). You can specify a brightness level from 0 (lowest brightness) to 7 (highest brightness). The second parameter can be used to turn the display on or off, false means off. You can choose any type of brightness level you would like.

void setup(){
  // Set brightness of the LED
  display.setBrightness(4);
  // Clear the display
  display.setSegments(OFF);
}

Countdown and Text Function

The countdown is the first function of the sketch. It is a simple loop that counts down from any number you would like (i). The functions use the display.showNumberDec ().  The functions consist of 4 parameters. Firstly I, that represents the number of the countdown function. The second parameter determines whether or not you want leading zeros before the number that is represented. The third parameter determines how many digits should be used and the last parameter determines from which digit the function should start.

There are also two functions that display the Text “PLAY” and “DONE”. The two functions both send a text to the display but the underlying method used is different, as explained above. After the text is displayed a delay is added of 2 seconds.

void countdown() {
  for(int i=1500; i>0; i--) {
    // showNumberDec is a function for displaying numeric values,
    display.showNumberDec(i, true, 4, 0);
    delay(80);
  }
}
void displayPlay() {
  display.setSegments(PLAY);
  delay(2000);
}
void displayDone() {
  display.setSegments(DONE);
  delay(2000);
}

Executing the code

The only thing left is to start the loop of the Arduino sketch. In the loop, you will use the three functions that we defined above. Firstly, the function of Play is executed, after two seconds, the countdown start and finally, the Done function is executed.

void loop(){
  displayPlay();
  countdown();
  displayDone();
}

In this tutorial I have demonstrated how to use a TM1637 4-digit 7-segment display with the Arduino Uno Board. We created a count down function and two functions to display text to the display.

I hope that you enjoyed reading the tutorial and fount is useful and informative. If you did please subscribe to my Instagram or Pinterest account found at the top of this page.

In any case that you have questions, feel free to ask them in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu