Public post

5.2 Controlling a TM1637 4 Digit 7 segment display with Arduino

· 5. KEYPADS AND 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.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 6x
Breadboard 400 points 1x
TM1637 4-digit 7-segment display 1x

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 the display.

A 7-segment display contains 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-Display

TM1637-LED-Display-Module-For-Arduino

TM1637 4-Digit 7-Segment Display Specifications

FeatureSpecification
Operating voltage3.3V – 5.5V
Operating Current80 mA
Operating Temperature-10ºC to +80ºC
Alpha-numeric digits4
Luminance levels8

BreadBoard Layout

TM1637 Display Connections

TM1637 4-Digit DisplayArduino 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.

The 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….

TM 1637 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.

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

The OFF constant consists 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 found it useful and informative. If you did, please subscribe to my Instagram or Pinterest account found at the top of this page.

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.