酒と薔薇の日々(その2)

つれづれなるままに好きなことだけ求めて生きるINTJ人間

Arduino温湿度計作成(中華LCD+keyPad)

2016年11月17日 20時07分59秒 | マイコン(ESP32・Arduino等)

※この記事当時は動いていたが、2022/2/14日 この1個めのソースのDHTを外したソースでは動かなかった。
なので追記記事を参考に動かすこと。Pinアサインが違ってるが・・・?よくわからん。

下側のピンは、印刷があるので解り易いが、

右上のピンは、右からデジタルの 0,1,2となる。今回はD2ピンを温湿度モジュールに使用している。

やっと動きマスタ。

しかし、中華シールド何か癖が悪い。と思う。

LCDシールドを乗せたままでプログラムを書き込むとかなりの頻度で温度計モジュールの初期化エラーで、書き込みが失敗する。

一度エラーになりだすと、シールドをArduinoから外さないと二度と書き込めなくなるようである。

ちょっと困る。300円だからか?

と思ったが、Arduinoの基本的なエラーのようだ。

「avrdude stk500_getsync attempt 10 of 10 not in sync resp 0x00」エラー問題

リセットボタンを押しながら、USBケーブルを抜いて、さらに押しながら再度挿入。

リセットボタンは、かなり長く押すのがコツ。10秒とか

で治るらしい。

//http://www.geocities.jp/bokunimowakaru/diy/arduino/lcds.html

//https://github.com/jdattilo/DHT11LIB からライブラリをDLしてインストールすること
//http://www.eleki-jack.com/FC/2009/06/41arduinolcd4.html 表示方法の詳細
//赤は+5V、黒はGND、緑は2Pinへ接続
//
#include
dht11 DHT11;
int last_t, last_h, now_t, now_h;
#include
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

void setup(){
  DHT11.attach(2);
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  // ---- LCD -----
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Humidity & temp");
  last_t = 0;
  last_h = 0;
  delay(5000);
}
 
void loop(){
  int chk = DHT11.read();
  now_h = DHT11.humidity;
  now_t = DHT11.temperature;
  Serial.println("\n");
  Serial.print("Read sensor: ");
  Serial.print("Humidity (%): ");      // 湿度
  Serial.println((float)DHT11.humidity, DEC);
  Serial.print("Temperature (C): ");   // 温度(摂氏)
  Serial.println((float)DHT11.temperature, DEC);
 
  lcd.clear();
  lcd.home();    //上段左端へ
  lcd.print("Hum : ");
  lcd.print(DHT11.humidity, DEC); // 10進で DEC/OCT/HEX
  lcd.setCursor(0,1);   // 2段目左端 (x,y)
  lcd.print("Temp: ");
  lcd.print(DHT11.temperature, DEC);


  lcd.setCursor(12,0);   // 湿度 1段目12 (x12,y0)
  if (last_h == now_h){
    lcd.print("    ");
  }else{
    if (now_h > last_h){
      lcd.print("Up  ");
    }else{
      lcd.print("Down");
    }
  }
    
  lcd.setCursor(12,1);   // 温度 2段目12 (x12,y1)
  if (last_t == now_t){
    lcd.print("    ");
  }else{
    if (now_t > last_t){
      lcd.print("Up  ");
    }else{
      lcd.print("Down");
    }
  }
    
  last_h = now_h;
  last_t = now_t;
  delay(5000);
}

追記 2022/2/14

メーカーサンプルソース
https://wiki.dfrobot.com/LCD_KeyPad_Shield_For_Arduino_SKU__DFR0009

#include 

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);        // select the pins used on the LCD panel

unsigned long tepTimer ;

void setup(){
    lcd.begin(16, 2);                       // start the library
}

void loop(){
    lcd.setCursor(0, 0);                   // set the LCD cursor   position
    int val;                               // variable to store the value coming from the analog pin
    double data;                           // variable to store the temperature value coming from the conversion formula
    val=analogRead(1);                     // read the analog in value:
    data = (double) val * (5/10.24);       // temperature conversion formula

    if(millis() - tepTimer > 500){         // output a temperature value per 500ms
             tepTimer = millis();

             // print the results to the lcd
             lcd.print("T: ");
             lcd.print(data);
             lcd.print("C");
     }
}

 

この記事についてブログを書く
« 半田ゴテゴテ( ^ω^)・・... | トップ | 忘年会1でタイーホ »