Arduino 溫度感應

由於手頭上的專案是跟溫度感應有關,先在網上購買了 DS18B20 防水的感應器,DS18B20 很好找到,露天或Yahoo拍賣很多人都在賣,有關 1-Wire 可以上 Dallas Semiconductor's 1-Wire Protocol 有很詳盡的說明。
DS18B20防水感應器
最痛苦的事情就是要用麵包板來跳線,因為DS18B20 與 Arduino 連接須接一個 4.7k pull-up resistor, 如果你去電子材料行跟老闆說要買一個 4.7k 的電阻,老闆一定不理你,買一片電阻板比較快:
電阻板
電阻該怎麼接?
DS18B20 接法

用麵包板接 Arduino:
麵包板接 DS18B20
接著下載 One-Wire 的 Library,我下載的是這一個版本 OneWire Library,打開 example:
OneWire example
原本的 example 先要把腳位改成自己接的Arduino 板上的腳位:

修改程式腳位
另外還要修改 程式中 addr[0] != 0x28 , 原本 0x10 是給DS18S20用的。
DS18B20 傳回 0x28
打開 Arduino 開發環境中的 Serial Monitor 就會看到:
Serial Monitor
收到傳回的字串了,接下來就是轉換成溫度顯示了,接著 follow Dallas Semiconductor's 1-Wire Protocol 上的說明修改程式,加入以下的片段:

First off, you need to define some variables, (put right under loop() above)int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract; Then for a DS18B20 series you will add the following code below the
Serial.println(); above LowByte = data[0]; HighByte = data[1]; TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 Whole = Tc_100 / 100; // separate off the whole and fractional portions Fract = Tc_100 % 100; if (SignBit) // If its negative { Serial.print("-"); } Serial.print(Whole); Serial.print("."); if (Fract < 10) { Serial.print("0"); } Serial.print(Fract); Serial.print("\n");
這下 Serial Monitor 會傳回:
Serial Monitor 傳回溫度
到此溫度感應先成功一半。








留言

  1. hello你好
    我們新竹電料行可以單買電阻耶
    一個6毛

    這篇文章很有參考價值~~~
    推~

    回覆刪除

張貼留言

請多指教

這個網誌中的熱門文章

Arduino 模擬 Modbus Slave

Arduino IDE 1.6.5 + BH1750 + CD74HC4067 多工器

【輕鬆工作家】使用 3D 印表機 製作一台 Arduino CNC GRBL 繪圖機