2015年7月24日 星期五

Arduino DS18B20 + I2C LCD

Arduino DS18B20 + I2C LCD 


         DS1820 只有三隻接腳,外觀就如同一顆電晶體,可將溫度直接轉換成12位元的數位值,量測範圍從+125度到-55度,最高解析度可達0.0652度,相關基本資料如下圖所示。
如果要參考詳細資料請自行下載資料手冊。(另有一顆功能相近,解析度較低的DS1820簡體中文版使用手冊,亦可自行下載參考)


        其接線方式說起,此晶片與單晶片連接的方式長使用圖5的接法,利用一個4.7k歐姆的電阻將資料線提升至高電位。
當CPU要與DS18B20溝通時,先輸出0,將此接線電壓拉至0V,持續至少480 us,再將輸出訊號轉為1。DS18B20感應到此起始訊號後,也會產生持續60~240 us的低電壓做回應,CPU只要從有無此回應訊號,就知道DS18B20有沒有接上。





I2C 16x2 LCD:



接線方式為:
  • SDA   –  接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
  • SCL    –  接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
  • GND   –  接 GND
  • VCC   –  接 +5V



//==========================================
//1-Wire  Library 
#include <OneWire.h>
#include <DallasTemperature.h>
//==========================================
//I2C + I2C LCD Library 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
/*==========================================
SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V
==========================================*/

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

String lcdDegC="" , lcdDegF="";  //, SerialDegC="" ,SerialDegF="";

void setup(void)
{
  Serial.begin(9600); //Begin serial communication
  Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
  sensors.begin();

  lcdDegC += char(223);              // Setup a Degrees C LCD Symbol
  lcdDegC += "C ";
  lcdDegF += char(223);              // Setup a Degrees F LCD Symbol
  lcdDegF += "F ";

  /*
  SerialDegC += char(167);           // Setup a Degrees C Serial symbol
  SerialDegC += " C ";
  SerialDegF += char(167);           // Setup a Degrees F Serial symbol
  SerialDegF += " F ";
  */
  
  lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
    // 輸出初始化文字
  lcd.setCursor(0, 0); // 設定游標位置在第一行行首
  lcd.print("Hello, world!");
  delay(1000);
  lcd.clear();
}

void loop(void)
  // Send the command to get temperatures
  sensors.requestTemperatures();  
  float celsius = sensors.getTempCByIndex(0);
  float fahrenheit = (1.8 * celsius) + 32;  
  
  lcd.setCursor(0, 0);                   // 設定游標位置在第1行行首
  lcd.print("Temperature is: ");
  lcd.setCursor(0, 1);                   // 設定游標位置在第2行行首
  lcd.print(celsius);
  lcd.setCursor(5, 1);                  
  lcd.print(lcdDegC);
  
  lcd.setCursor(9, 1);                 
  lcd.print(fahrenheit);
  lcd.print(lcdDegF);

  Serial.print("Celsius: ");
  Serial.println(celsius);
  
  // Serial.println("degree C");
   
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);
  // Serial.println("degree F");
  /*
  Serial.print("Temperature is: ");
  Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  //Update value every 1 sec.
  */
  delay(1000);
  
}















參考文件

1)  http://www.prodtech.biz/sensor/DS18B20.pdf
2)  http://forum.arduino.cc/index.php?topic=19002.0
3)  http://yehnan.blogspot.tw/2013/01/arduinods18b20.html
4)  http://faculty.stust.edu.tw/~wjshieh/ds18b20.html
4)  程式庫

1 則留言:

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...