工位上摆个小东西,比手办还能提神。我一直想给桌面加个能看时间、又能当倒计时器的摆件,翻了半天方案,最后发现 TM1637 四位数码管是最省事的选择:两根信号线加两根电源线就能驱动,十几块钱,库也是现成的。这篇从接线到代码一次讲完,顺手把它变成一个真时钟。

先认识这块屏:四位八段加一个冒号
数码管的每一位就是一个"日"字加一个小数点,七个发光段加一个段点,业内叫八段显示。TM1637 这块板子把四位数码管和驱动芯片集成在一起,中间还带一个冒号——做时钟显示 12:30 这种格式就靠它。亮度有 0 到 7 八档,靠板上的驱动芯片控制,不用自己再外接限流电路。
驱动 TM1637 只需要两根信号线:CLK 时钟和 DIO 数据,再加 5V 和 GND,一共四根。它走的是类似 I2C 的私有协议,好在 Arduino 有现成的 TM1637Display 库,库管理器直接装。
接线与基础例程
接线就两步:CLK 接 D2,DIO 接 D3,VCC 接 5V、GND 接 GND。下面这份官方例程把常用功能全演示了一遍:全亮、计数、指定位显示、亮度渐变、带冒号显示、还有用段位拼出符号:
/* Example code for TM1637 4 digit 7 segment display with Arduino.
www.www.makerguides.com */
// Include the library:
#include "TM1637Display.h"
// Define the connections pins:
#define CLK 2
#define DIO 3
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);
// Create array that turns all segments on:
const uint8_t data[] = {0xff, 0xff, 0xff, 0xff};
// Create array that turns all segments off:
const uint8_t blank[] = {0x00, 0x00, 0x00, 0x00};
// You can set the individual segments per digit to spell words or create other symbols:
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 degree Celsius symbol:
const uint8_t celsius[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_A | SEG_D | SEG_E | SEG_F // C
};
void setup() {
// Clear the display:
display.clear();
delay(1000);
}
void loop() {
// Set the brightness:
display.setBrightness(7);
// All segments on:
display.setSegments(data);
delay(1000);
display.clear();
delay(1000);
// Show counter:
int i;
for (i = 0; i < 101; i++) {
display.showNumberDec(i);
delay(50);
}
delay(1000);
display.clear();
delay(1000);
// Print number in different locations, loops 2 times:
int j;
for (j = 0; j < 2; j++) {
for (i = 0; i < 4; i++) {
display.showNumberDec(i, false, 1, i);
delay(500);
display.clear();
}
}
delay(1000);
display.clear();
delay(1000);
// Set brightness (0-7):
int k;
for (k = 0; k < 8; k++) {
display.setBrightness(k);
display.setSegments(data);
delay(500);
}
delay(1000);
display.clear();
delay(1000);
// Print 1234 with the center colon:
display.showNumberDecEx(1234, 0b11100000, false, 4, 0);
delay(1000);
display.clear();
delay(1000);
int temperature = 24;
display.showNumberDec(temperature, false, 2, 0);
display.setSegments(celsius, 2, 2);
delay(1000);
display.clear();
delay(1000);
display.setSegments(done);
delay(1000);
display.clear();
delay(1000);
}几个函数值得单独说:showNumberDec(number, leading_zero, length, pos) 后两个参数控制显示几位、从哪一位开始显示,比如 showNumberDec(12, false, 2, 1) 是把 12 显示在中间两位。setSegments() 是手工点亮任意段的接口,示例里的 celsius[] 数组就是用段位拼出一个℃符号——想做温度计、倒计时结束显示 dOnE,全靠它。
升级成真时钟:配一颗 DS3231
纯数码管断电就归零,做时钟要配 RTC 模块。DS3231 接 I2C 口(A4 SDA、A5 SCL),TM1637 还是 D2、D3 不变:

下面这份例程从 RTC 读时间,用带冒号的格式显示成 HH:MM,还可以让冒号一秒一闪:
/* Arduino example code to display a 24 hour time format clock on a
TM1637 4 digit 7 segment display with a DS32321 RTC.
www.www.makerguides.com */
// Include the libraries:
#include "RTClib.h"
#include "TM1637Display.h"
// Define the connections pins:
#define CLK 2
#define DIO 3
// Create rtc and display object:
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
// Wait for console opening:
delay(3000);
// Check if RTC is connected correctly:
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Set the display brightness (0-7):
display.setBrightness(5);
// Clear the display:
display.clear();
}
void loop() {
// Get current date and time:
DateTime now = rtc.now();
// Create time format to display:
int displaytime = (now.hour() * 100) + now.minute();
// Print displaytime to the Serial Monitor:
Serial.println(displaytime);
// Display the current time in 24 hour format with leading zeros enabled and a center colon:
display.showNumberDecEx(displaytime, 0b11100000, true);
// Remove the following lines of code if you want a static instead of a blinking center colon:
delay(1000);
display.showNumberDec(displaytime, true); // Prints displaytime without center colon.
delay(1000);
}DS3231 断电靠纽扣电池走时,掉电后再上电 lostPower() 会提示重新校准。桌面时钟到这一步就算成了,亮度建议开 5 档以下,晚上不刺眼。
换个玩法:温度显示器
把 RTC 换成 DHT11 温度传感器(数据脚接 D4),用 showNumberDec 配合 celsius 段位,就能做一个室内温度显示牌:

代码思路是先 dht.readTemperature() 读温度,showNumberDec(temperature, false, 2, 0) 显示在前两位,再用 setSegments(celsius, 2, 2) 在后两位拼出℃。
数着数着,这块屏的用途其实不少:煮蛋倒计时、作业计时器、钓鱼打窝提醒。四根线接上,晚上花半小时烧个例程,周末就能折腾出一个像样的桌面小件。











桌面上摆个这种数码管时钟确实有格调,亮度调低晚上不晃眼。
DS3231断电走时这点太重要了,DS1302掉电就归零的教训还在眼前。
用段位拼字那段有意思,拼个DONE倒计时结束还挺仪式感。
煮蛋倒计时这个场景真实,明天就给老婆做一个。