Arduino - TM1637 4 位 7 段显示器
Arduino-TM1637 4 位 7 段显示器
A standard 4-digit 7-segment display is needed for clock, timer and counter projects, but it usually requires 12 connections. The TM1637 module makes it easier by only requiring 4 connections: 2 for power and 2 for controlling the segments.
时钟、定时器和计数器项目需要标准的 4 位 7 段显示器,但通常需要 12 个连接。TM1637 模块只需 4 个连接即可简化操作:2 个用于电源,2 个用于控制段。
This tutorial will not overload you by deep driving into hardware. Instead, We will learn how to connect the 4-digit 7-segment display to Arduino, how to program it do display what we want.
本教程不会通过深入硬件来使您超载。相反,我们将学习如何将 4 位 7 段显示器连接到 Arduino,如何对其进行编程以显示我们想要的内容。
This tutorial are going to use the colon-separated 4-digit 7-segment display module. If you want to display the float numbers, please use the 74HC595 4-digit 7-segment Display Module
本教程将使用冒号分隔的 4 位 7 段显示模块。如果要显示浮点数,请使用 74HC595 4 位 7 段显示模块
关于 TM1637 4 位 7 段显示器
A TM1637 module typically consists of four 7-segment LEDs and a colon-shaped LED in the middle: It is ideal for displaying time in hours and minutes, or minutes and seconds, or scores of two teams.
TM1637 模块通常由四个 7 段 LED 和一个中间的冒号形 LED 组成:它非常适合以小时和分钟、分钟和秒或两个团队的分数显示时间。
Pinout 引脚排列
TM1637 4-digit 7-segment display module includes 4 pins:
TM1637 4 位 7 段显示模块包括 4 个引脚:
- CLK pin: is a clock input pin. Connect to any digital pin on Arduino.
CLK引脚:是时钟输入引脚。连接到Arduino上的任何数字引脚。 - DIO pin: is a Data I/O pin. Connect to any digital pin on Arduino.
DIO 引脚:是数据 I/O 引脚。连接到Arduino上的任何数字引脚。 - VCC pin: pin supplies power to the module. Connect it to the 3.3V to 5V power supply.
VCC引脚:引脚为模块供电。将其连接到 3.3V 至 5V 电源。 - GND pin: is a ground pin.
GND 引脚:是接地引脚。
Wiring Diagram 接线图
To connect a TM1637 to an Arduino, connect four wires: two for power and two for controlling the display. The module can be powered from the 5-volt output of the Arduino. Connect the CLK and DIO pins to any digital pins of Arduino. For example, 2 and 3 on the Arduino. The pin numbers in the code should be changed if different pins are used.
要将 TM1637 连接到 Arduino,请连接四根电线:两根用于电源,两根用于控制显示器。该模块可由 Arduino 的 5 伏输出供电。将 CLK 和 DIO 引脚连接到 Arduino 的任何数字引脚。例如,Arduino 上的 2 和 3。如果使用不同的引脚,则应更改代码中的引脚编号。
This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片
Library Installation 库安装
To program easily for TM1637 4-digit 7-segment Display, we need to install TM1637Display library by Avishay Orpaz. Follow the below steps to install the library:
为了轻松对 TM1637 4 位 7 段显示器进行编程,我们需要安装 Avishay Orpaz 的 TM1637Display 库。按照以下步骤安装库:
- Navigate to the Libraries icon on the left bar of the Arduino IDE.
导航到 Arduino IDE 左侧栏上的 Libraries 图标。 - Search “TM1637”, then find the TM1637Display library by Avishay Orpaz
搜索“TM1637”,然后找到 Avishay Orpaz 的 TM1637Display 库 - Click Install button. 单击“安装”按钮。
如何使用Arduino对TM1637 4位7段进行编程
- Include the library 包括库
#include <TM1637Display.h>
- Define Arduino’s pins that connects to CLK and DIO of the display module. For example, pin D9 and D10
定义连接到显示模块的 CLK 和 DIO 的 Arduino 引脚。例如,引脚 D9 和 D10
#define CLK 9
#define DIO 10
- Create a display object of type TM1637Display
创建 TM1637Display 类型的显示对象
TM1637Display display = TM1637Display(CLK, DIO);
TM1637Display display = TM1637Display(CLK, DIO);
- Then you can display number, number with decimal, number with minus sign, or letter. In the case of leter, you need to define the letter form. Let’s see one by one.
然后,您可以显示数字、带十进制的数字、带减号的数字或字母。对于 leter,您需要定义字母形式。让我们一一看看。 - Display number: see below examples, '’ in below description represents for a digit that does not display anything in pratice:
显示数字:请参阅以下示例,以下描述中的“”表示不显示任何内容的数字:
display.showNumberDec(-12); // displayed _-12
display.showNumberDec(-999); // displayed -999
display.showNumberDec(42); // displayed __42
display.showNumberDec(42, false); // displayed __42
display.showNumberDec(42, false, 2, 0); // displayed 42__ => display 2 digit at position 0
display.showNumberDec(42, true); // displayed 0042 => zero padding
display.showNumberDec(14, false, 2, 1); // displayed _14_
display.showNumberDec(-5, false, 3, 0); // displayed _-5_
display.showNumberDec(1234); // displayed 1234
- Display the number with a colon or dot:
用冒号或圆点显示数字:
// displayed 15:30 in the colon-separated module, or 15.30 in the colon-separated module
display.showNumberDecEx(1530, 0b11100000, false, 4, 0);
// displayed 15:30 in the colon-separated module, or 15.30 in the colon-separated module display.showNumberDecEx(1530, 0b11100000, false, 4, 0);
在冒号分隔的模块中显示 15:30,或在冒号分隔的模块中显示 15:30
You can see more detail in the function references at the end of this tutorial
您可以在本教程末尾的函数参考中查看更多详细信息
Arduino Code Arduino代码
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-tm1637-4-digit-7-segment-display
*/
#include <TM1637Display.h>
// define the connections pins
#define CLK 9
#define DIO 10
// create a display object of type TM1637Display
TM1637Display display = TM1637Display(CLK, DIO);
// an array that sets individual segments per digit to display the word "dOnE"
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
};
// degree celsius symbol
const uint8_t celsius[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Degree symbol
SEG_A | SEG_D | SEG_E | SEG_F // C
};
void setup() {
display.clear();
display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
}
void loop() {
// show counter 0-9
int i;
for (i = 0; i < 10; i++) {
display.showNumberDec(i);
delay(500);
display.clear();
}
display.showNumberDec(-91); // displayed _-91
delay(2000);
display.clear();
display.showNumberDec(-109); // displayed -109
delay(2000);
display.clear();
display.showNumberDec(21, false); // displayed __21
delay(2000);
display.clear();
display.showNumberDec(21, true); // displayed 0021
delay(2000);
display.clear();
display.showNumberDec(28, false, 2, 1); // displayed _28_
delay(2000);
display.clear();
display.showNumberDec(-9, false, 3, 0); // displayed _-9_
delay(2000);
display.clear();
// displayed 15:30
display.showNumberDecEx(1530, 0b11100000, false, 4, 0);
delay(2000);
display.clear();
// displayed 23°C
int temperature = 23; // or read from temperature sensor
display.showNumberDec(temperature, false, 2, 0);
display.setSegments(celsius, 2, 2);
delay(2000);
display.clear();
// displayed letters: dOnE
display.setSegments(done);
delay(2000);
display.clear();
}
Quick Steps 快速步骤
- Copy the above code and open with Arduino IDE
复制上面的代码并使用Arduino IDE打开 - Click Upload button on Arduino IDE to upload code to Arduino
单击Arduino IDE上的“上传”按钮,将代码上传到Arduino - See the states of the 7-segment display
查看 7 段显示器的状态
Function References
The below are references for the following functions:
- display.clear()
- display.showNumberDec()
- display.showNumberDecEx()
- display.setSegments()
- display.setBrightness()
display.clear()
Description
This function clear the display. It turns all LEDs off
display.showNumberDec()
Description 描述
The function is used to display a decimal number on the 7-segment display.
该函数用于在 7 段显示器上显示十进制数。
Syntax 语法
void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
Parameter 参数
- num: This is the number to be displayed on the 7-segment display. It should be within the range of -9999 to 9999.
num:这是要在 7 段显示屏上显示的数字。它应该在 -9999 到 9999 的范围内。 - leading_zero: This is an optional parameter with a default value of false. If it is set to true, leading zeros will be displayed.
leading_zero:这是一个可选参数,默认值为 false。如果设置为 true,则将显示前导零。 - length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
length:这是一个可选参数,默认值为 4。它设置要在 7 段显示器上显示的位数。 - pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.
pos:这是一个可选参数,默认值为 0。它设置数字最高有效数字的位置。
Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.
请注意,如果数字超出范围或长度值大于 4,则该函数将不会显示任何内容。
showNumberDecEx() showNumberDecEx()
Description 描述
The function is used to display a decimal number on the 7-segment display with additional features compared to the showNumberDec() function. It is an advanced version of showNumberDec() that allows you to control the dot or colon segments of each digit individually.
该函数用于在 7 段显示器上显示十进制数,与 showNumberDec() 函数相比具有附加功能。它是 showNumberDec() 的高级版本,允许您单独控制每个数字的点段或冒号段。
Syntax 语法
void showNumberDecEx(int num, uint8_t dots, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
void showNumberDecEx(int num, uint8_t 点, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
Parameter 参数
- num1: This is the number to be displayed on the 7-segment display. It should be within the range of -9999 to 9999.
num1:这是要显示在 7 段显示屏上的数字。它应该在 -9999 到 9999 的范围内。 - dots: This parameter is used to specify which segments of the display should be turned on as dots. Each bit of the value corresponds to a digit on the display: Valid value
dots:此参数用于指定应以点的形式打开显示器的哪些部分。该值的每一位对应于显示屏上的一个数字: 有效值- 0b10000000: display the first dot: 0.000
0b10000000:显示第一个点:0.000 - 0b01000000: display the second dot: 00.00
0b01000000:显示第二个点:00.00 - 0b00100000: display the third dot: 000.0
0b00100000:显示第三个点:000.0 - 0b01000000: For displays with just a colon: 00:00
0b01000000:对于仅带有冒号的显示器:00:00
- 0b10000000: display the first dot: 0.000
- leading_zero: This is an optional parameter with a default value of false. If it is set to true, leading zeros will be displayed.
leading_zero:这是一个可选参数,默认值为 false。如果设置为 true,则将显示前导零。 - length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
length:这是一个可选参数,默认值为 4。它设置要在 7 段显示器上显示的位数。 - pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.
pos:这是一个可选参数,默认值为 0。它设置数字最高有效数字的位置。
For example, if you call display.showNumberDecEx(1530,0b01000000); it will display the number 15:30 on the 7-segment display.
例如,如果调用 display.showNumberDecEx(1530,0b01000000);它将在 15 段显示屏上显示数字 30:7。
Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.
请注意,如果数字超出范围或长度值大于 4,则该函数将不会显示任何内容。
setSegments()
Description 描述
The function is used to set the segments of the 7-segment display directly. It can be used to dislay letters, special character, or turn all all LED segment.
该功能用于直接设置 7 段显示的段。它可用于铺设字母、特殊字符或转动所有 LED 段。
Syntax 语法
void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
Parameter 参数
- segments: This parameter sets the segments of the 7-segment display, it’s an array of bytes, where each byte represents the segments of each digit. Each segment is represented by a bit in the byte.
segments:此参数设置 7 段显示的段,它是一个字节数组,其中每个字节代表每个数字的段。每个段都由字节中的位表示。 - length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
length:这是一个可选参数,默认值为 4。它设置要在 7 段显示器上显示的位数。 - pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.
pos:这是一个可选参数,默认值为 0。它设置数字最高有效数字的位置。
This function is useful when you want to display characters or symbols that are not included in the basic 7-segment display. By setting the segments directly, you can display any pattern you want.
当您想要显示基本 7 段显示中未包含的字符或符号时,此功能非常有用。通过直接设置段,您可以显示所需的任何图案。
Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.
请注意,如果数字超出范围或长度值大于 4,则该函数将不会显示任何内容。
setBrightness()
Description 描述
The function is used to set the brightness of the 7-segment display.
该功能用于设置 7 段显示器的亮度。
Syntax 语法
void setBrightness(uint8_t brightness, bool on = true);
Parameter 参数
brightness: This parameter sets the brightness level of the 7-segment display. The value should be in the range of 0 to 7. A higher value results in a brighter display.
亮度:此参数设置 7 段显示器的亮度级别。该值应在 0 到 7 的范围内。值越高,显示效果越亮。
on: This is an optional parameter with a default value of true. It’s used to turn on or off the display. If it’s set to false, the display will be turned off.
on:这是一个可选参数,默认值为 true。它用于打开或关闭显示器。如果设置为 false,则显示将关闭。