Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clive520/007abb14e4ad9da834d53bff7f51f835 to your computer and use it in GitHub Desktop.
Save clive520/007abb14e4ad9da834d53bff7f51f835 to your computer and use it in GitHub Desktop.
兩個DS18B20 溫度感測器,由LCD輸出
// LCD接線 GND==>GND VCC==>5V SDA==>A4 SDL==>A5
// 多個DS18B20 溫度感測器資料都接在同一個腳位 (數位腳 D6)
//需要安裝程式庫 1. LiquidCrystal_I2C.h(LCD用) 2. OneWire.h(DS18B20用) 3. DallasTemperature.h(DS18B20用)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#include <OneWire.h> // 引用 OneWire 程式庫
#include <DallasTemperature.h> // 引用 DallasTemperature 程式庫
#define ONE_WIRE_BUS 6 // 定義 DS18B20 資料腳位(數位腳 D6)((所有)溫度感測器都接在同一個腳位)
OneWire oneWire(ONE_WIRE_BUS); // 定義 oneWire 物件類型
DallasTemperature sensors(&oneWire); // 定義溫度感測器物件
void setup(void)
{
lcd.begin(); // 開啟lcd
Serial.begin(9600); // 開啟序列埠
sensors.begin(); // 開啟溫度感測器
}
void loop(void)
{
sensors.requestTemperatures(); // 下指令要求取得(所有)溫度感測器的溫度值
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0)); // 取得(第0個溫度感測器的溫度值)並顯示溫度值
lcd.setCursor(0, 0);
lcd.print(sensors.getTempCByIndex(0));
Serial.print("Temperature for the device 2 (index 1) is: ");
Serial.println(sensors.getTempCByIndex(1)); // 取得(第1個溫度感測器的溫度值)並顯示溫度值
lcd.setCursor(0, 1);
lcd.print(sensors.getTempCByIndex(1));
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment