
目录一、所需库文件二、硬件连接三、完整程序代码四、测试1、数码管显示温度2、串口中显示温度五、温度超限报警指示1、温度超限报警实现2、完整的程序如下3、实际测试1温度低于24摄氏度2温度超过24摄氏度一、所需库文件在Arduino IDE中通过 工具 - 管理库 搜索并安装以下三个库1、OneWire — 用于单总线通信2、DallasTemperature — 用于DS18B20温度传感器3、TM1637显示部分推荐使用 TM1637TinyDisplay 库支持负数显示--同样在库管理器中搜索安装。二、硬件连接三、完整程序代码/* * Arduino Nano DS18B20 TM1637 温度显示 * 支持正负数精确到0.1℃兼容Arduino 1.8.12 */ #include OneWire.h #include DallasTemperature.h #include TM1637TinyDisplay.h // 引脚定义 #define ONE_WIRE_BUS 8 // DS18B20 数据引脚 #define TM1637_CLK 3 // TM1637 时钟引脚 #define TM1637_DIO 2 // TM1637 数据引脚 // 全局对象 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(oneWire); TM1637TinyDisplay display(TM1637_CLK, TM1637_DIO); // 段码表 (共阴极0~9) const uint8_t SEG[] { 0x3F, // 0 0x06, // 1 0x5B, // 2 0x4F, // 3 0x66, // 4 0x6D, // 5 0x7D, // 6 0x07, // 7 0x7F, // 8 0x6F // 9 }; const uint8_t SEG_MINUS 0x40; // 负号 - const uint8_t SEG_BLANK 0x00; // 空白 void setup() { Serial.begin(9600); sensors.begin(); display.setBrightness(7); // 亮度 0~7 display.clear(); // 修正点使用 getDeviceCount() 检测传感器 if (sensors.getDeviceCount() 0) { Serial.println(Err:No checked DS18B20!); } else { Serial.print(Checked DS18B20 Count:); Serial.print(sensors.getDeviceCount()); Serial.println(); } delay(500); } void loop() { sensors.requestTemperatures(); float temperature sensors.getTempCByIndex(0); // 传感器故障检测DS18B20读取失败返回 -127℃ if (temperature -127.00) { display.showString(ERR ); delay(1000); return; } // 转换为整数精确到0.1℃例如 25.3 - 253-5.2 - -52 int tempInt (int)(temperature * 10); // 在数码管上显示 displayTemperature(tempInt); // 串口调试输出 Serial.print(Temp: ); Serial.print(temperature, 1); Serial.println( Degrees); delay(1000); // 刷新间隔 } /** * 核心显示函数纯段码操作兼容所有版本库 * param temp 温度值×10如 253 表示 25.3℃ */ void displayTemperature(int temp) { uint8_t segs[4] {SEG_BLANK, SEG_BLANK, SEG_BLANK, SEG_BLANK}; bool negative false; int dotPos 1; // 小数点默认放在第1位从0开始计数 // ------------------ 超量程处理±100℃以上 ------------------ if (temp 1000) { // ≥ 100.0℃ segs[0] SEG[9]; segs[1] SEG[9]; segs[2] SEG[9]; display.setSegments(segs); return; } if (temp -1000) { // ≤ -100.0℃ segs[0] SEG_MINUS; segs[1] SEG[9]; segs[2] SEG[9]; display.setSegments(segs); return; } // ------------------ 处理符号 ------------------ if (temp 0) { negative true; temp -temp; // 转为绝对值 } int whole temp / 10; // 整数部分如 253 - 25 int frac temp % 10; // 小数部分如 253 - 3 // ------------------ 两位数整数部分10.0℃ ~ 99.9℃ ------------------ if (whole 10) { int tens whole / 10; // 十位 int ones whole % 10; // 个位 if (negative) { // 显示 -XX.X 格式如 -12.3 segs[0] SEG_MINUS; segs[1] SEG[tens]; segs[2] SEG[ones]; segs[3] SEG[frac]; dotPos 2; // 小数点在个位后面位置2 } else { // 显示 XX.X 格式如 25.3 segs[0] SEG[tens]; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; dotPos 1; // 小数点在个位后面位置1 } } // ------------------ 一位数整数部分-9.9℃ ~ 9.9℃ ------------------ else { int ones whole % 10; if (negative) { // 显示 -X.X 格式如 -5.2 segs[0] SEG_MINUS; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; } else { // 显示 X.X 格式首位留空如 5.2 segs[0] SEG_BLANK; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; } dotPos 1; // 小数点在个位后面位置1 } // ------------------ 点亮小数点 (bit7 置1) ------------------ if (dotPos 0 dotPos 4) { segs[dotPos] | 0x80; } // 发送到TM1637显示 display.setSegments(segs); }四、测试1、数码管显示温度2、串口中显示温度五、温度超限报警指示1、温度超限报警实现设置当温度达到或者超过24摄氏度时候LED指示灯点亮。当温度低于24摄氏度LED灯熄灭。LED指示灯使用IO引脚13高电平点亮低电平熄灭。温度超限报警代码可用作温度控制器使用//温度超限报警LED指示灯。温度达到24度LED指示灯亮温度低于24度LED指示灯灭 if(temperature24.0f) { digitalWrite(LED_LIGHT, HIGH); } if(temperature24.0f) { digitalWrite(LED_LIGHT, LOW); }2、完整的程序如下/* * Arduino Nano DS18B20 TM1637 温度显示 * 支持正负数精确到0.1℃兼容Arduino 1.8.12 */ #include OneWire.h #include DallasTemperature.h #include TM1637TinyDisplay.h // 引脚定义 #define LED_LIGHT 13 // led 温度指示灯 #define ONE_WIRE_BUS 8 // DS18B20 数据引脚 #define TM1637_CLK 3 // TM1637 时钟引脚 #define TM1637_DIO 2 // TM1637 数据引脚 // 全局对象 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(oneWire); TM1637TinyDisplay display(TM1637_CLK, TM1637_DIO); // 段码表 (共阴极0~9) const uint8_t SEG[] { 0x3F, // 0 0x06, // 1 0x5B, // 2 0x4F, // 3 0x66, // 4 0x6D, // 5 0x7D, // 6 0x07, // 7 0x7F, // 8 0x6F // 9 }; const uint8_t SEG_MINUS 0x40; // 负号 - const uint8_t SEG_BLANK 0x00; // 空白 void setup() { Serial.begin(9600); pinMode(LED_LIGHT, OUTPUT); sensors.begin(); display.setBrightness(7); // 亮度 0~7 display.clear(); // 修正点使用 getDeviceCount() 检测传感器 if (sensors.getDeviceCount() 0) { Serial.println(Err:No checked DS18B20!); } else { Serial.print(Checked DS18B20 Count:); Serial.print(sensors.getDeviceCount()); Serial.println(); } delay(500); } void loop() { sensors.requestTemperatures(); float temperature sensors.getTempCByIndex(0); // 传感器故障检测DS18B20读取失败返回 -127℃ if (temperature -127.00) { display.showString(ERR ); delay(1000); return; } // 转换为整数精确到0.1℃例如 25.3 - 253-5.2 - -52 int tempInt (int)(temperature * 10); // 在数码管上显示 displayTemperature(tempInt); //温度超限报警LED指示灯。温度达到24度LED指示灯亮温度低于24度LED指示灯灭 if(temperature24.0f) { digitalWrite(LED_LIGHT, HIGH); } if(temperature24.0f) { digitalWrite(LED_LIGHT, LOW); } // 串口调试输出 Serial.print(Temp: ); Serial.print(temperature, 1); Serial.println( Degrees); delay(1000); // 刷新间隔 } /** * 核心显示函数纯段码操作兼容所有版本库 * param temp 温度值×10如 253 表示 25.3℃ */ void displayTemperature(int temp) { uint8_t segs[4] {SEG_BLANK, SEG_BLANK, SEG_BLANK, SEG_BLANK}; bool negative false; int dotPos 1; // 小数点默认放在第1位从0开始计数 // ------------------ 超量程处理±100℃以上 ------------------ if (temp 1000) { // ≥ 100.0℃ segs[0] SEG[9]; segs[1] SEG[9]; segs[2] SEG[9]; display.setSegments(segs); return; } if (temp -1000) { // ≤ -100.0℃ segs[0] SEG_MINUS; segs[1] SEG[9]; segs[2] SEG[9]; display.setSegments(segs); return; } // ------------------ 处理符号 ------------------ if (temp 0) { negative true; temp -temp; // 转为绝对值 } int whole temp / 10; // 整数部分如 253 - 25 int frac temp % 10; // 小数部分如 253 - 3 // ------------------ 两位数整数部分10.0℃ ~ 99.9℃ ------------------ if (whole 10) { int tens whole / 10; // 十位 int ones whole % 10; // 个位 if (negative) { // 显示 -XX.X 格式如 -12.3 segs[0] SEG_MINUS; segs[1] SEG[tens]; segs[2] SEG[ones]; segs[3] SEG[frac]; dotPos 2; // 小数点在个位后面位置2 } else { // 显示 XX.X 格式如 25.3 segs[0] SEG[tens]; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; dotPos 1; // 小数点在个位后面位置1 } } // ------------------ 一位数整数部分-9.9℃ ~ 9.9℃ ------------------ else { int ones whole % 10; if (negative) { // 显示 -X.X 格式如 -5.2 segs[0] SEG_MINUS; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; } else { // 显示 X.X 格式首位留空如 5.2 segs[0] SEG_BLANK; segs[1] SEG[ones]; segs[2] SEG[frac]; segs[3] SEG_BLANK; } dotPos 1; // 小数点在个位后面位置1 } // ------------------ 点亮小数点 (bit7 置1) ------------------ if (dotPos 0 dotPos 4) { segs[dotPos] | 0x80; } // 发送到TM1637显示 display.setSegments(segs); }3、实际测试1温度低于24摄氏度2温度超过24摄氏度