2015年11月9日 星期一

Arduino 8:溫度量測,來個LCD顯示

DS18B20

需要引入函式庫


  1. #include <OneWire.h>

  2. int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

  3. //Temperature chip i/o
  4. OneWire ds(DS18S20_Pin);  // on digital pin 2

  5. void setup(void) {
  6.   Serial.begin(9600);     //從螢幕上監測
  7. }

  8. void loop(void) {
  9.   float temperature = getTemp();
  10.   Serial.println(temperature);
  11.   
  12.   delay(100); //just here to slow down the output so it is easier to read
  13. }



  14. float getTemp(){
  15.                             //returns the temperature from one DS18S20 in DEG Celsius
  16.   byte data[12];
  17.   byte addr[8];

  18.   if ( !ds.search(addr)) {
  19.       //no more sensors on chain, reset search
  20.       ds.reset_search();
  21.       return -1000;
  22.   }

  23.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  24.       Serial.println("CRC is not valid!");
  25.       return -1000;
  26.   }

  27.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
  28.       Serial.print("Device is not recognized");
  29.       return -1000;
  30.   }

  31.   ds.reset();
  32.   ds.select(addr);
  33.   ds.write(0x44,1); // start conversion, with parasite power on at the end

  34.   byte present = ds.reset();
  35.   ds.select(addr);    
  36.   ds.write(0xBE); // Read Scratchpad

  37.   
  38.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  39.     data[i] = ds.read();
  40.   }
  41.   
  42.   ds.reset_search();
  43.   
  44.   byte MSB = data[1];
  45.   byte LSB = data[0];

  46.   float tempRead = ((MSB << 8) | LSB); //using two's compliment
  47.   float TemperatureSum = tempRead / 16;
  48.   return TemperatureSum;
  49. }

改成將結果輸出到LCD

  1. //-------------------------   LCD 
  2.                                              //Library version:1.1
  3.  #include <OneWire.h>      // DS18S20 waterproof溫度模組
  4.  #include <Wire.h>
  5.  #include <LiquidCrystal_I2C.h>
  6.  #if defined(ARDUINO) && ARDUINO >= 100
  7.  #define printByte(args)  write(args);
  8.  #else
  9.  #define printByte(args)  print(args,BYTE);
  10.  #endif
  11.  LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line
  12. //-------------------------

  13.  int DS18S20_Pin = 2;     //DS18S20 溫度Signal pin on digital 2
  14.                                          //Temperature chip i/o
  15.  OneWire ds(DS18S20_Pin);  // on digital pin 2
  16.  
  17. void setup() {
  18.     //-------------------------   LCD  
  19.      lcd.init();                      // initialize the lcd 
  20.      lcd.backlight();

  21.      lcd.setCursor(0, 0);
  22.      lcd.print   ("Temp.=");
  23.     //-------------------------   LCD 
  24. }


  25. void loop(){       
  26.         float temperature = getTemp();
  27.                                    //Serial.println(temperature);
  28.        //-------------------------   LCD  
  29.          lcd.setCursor(6, 0);
  30.          lcd.print(temperature, 3  );
  31.          lcd.print((char)223);            //度
  32.          lcd.print("C");
  33.        //-------------------------   LCD 
  34.  
  35.   delay(100);   //just here to slow down the output so it is easier to read
  36.                                 // delay(1000);          // 等待一秒鐘(每隔一秒測量一次)
  37. }



  38. float getTemp(){
  39.                             //returns the temperature from one DS18S20 in DEG Celsius
  40.   byte data[12];
  41.   byte addr[8];
  42.   if ( !ds.search(addr)) {
  43.       //no more sensors on chain, reset search
  44.       ds.reset_search();
  45.       return -1000;
  46.   }

  47.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  48.       Serial.println("CRC is not valid!");
  49.       return -1000;
  50.   }

  51.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
  52.       Serial.print("Device is not recognized");
  53.       return -1000;
  54.   }

  55.   ds.reset();
  56.   ds.select(addr);
  57.   ds.write(0x44,1); // start conversion, with parasite power on at the end

  58.   byte present = ds.reset();
  59.   ds.select(addr);    
  60.   ds.write(0xBE); // Read Scratchpad

  61.   
  62.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  63.     data[i] = ds.read();
  64.   }
  65.   
  66.   ds.reset_search();
  67.   
  68.   byte MSB = data[1];
  69.   byte LSB = data[0];

  70.   float tempRead = ((MSB << 8) | LSB); //using two's compliment
  71.   float TemperatureSum = tempRead / 16;
  72.   return TemperatureSum;
  73. }


沒有留言:

張貼留言