Wio TerminalのADCを使って温度計にしてみた。使ったセンサは、LM35DZ。
以前に秋月電子で買っていたものだけど、今見たらもう、置いてないみたい。
まぁ、センサは何でも良いのだけど、LM35の場合、使用電圧が4Vからになっていた。
Wioの5Vから引っ張ってきてAD変換してみたら、なんか、45度とかになる???
どうやら、Wioの基準電圧は、電源電圧にしても5Vではなく、3.3Vみたいですね。
3.3Vで計算しなおしたら、それっぽい値になりました。
配線は、電源とGNDとアナログ端子の3本だけ。 電圧は、3.3Vでも5Vでもどっちでも同じ値が出てました。アナログ端子は、A3を使いました。折れ線グラフのライブラリを用意してくれているので、これを使って、折れ線グラフを表示させて、データーのサンプリングのインターバルを5wayスイッチで1秒から30秒まで変えられるようにしてみた。
#include"TFT_eSPI.h" #include"seeed_line_chart.h" //include the library TFT_eSPI tft; #define LED BCM21 #define max_size 50 //maximum size of data doubles data; //Initilising a doubles type to store data TFT_eSprite spr = TFT_eSprite(&tft); // Sprite int interval = 1000; void blink() { //LED blink every sampling digitalWrite(LED,HIGH); delay(50); digitalWrite(LED,LOW); delay(50); } void interval_up() { //Interval counter increase interval += 1000; if(interval>30000) interval = 30000; tft.fillRect(300,200,50,30,TFT_BLACK); tft.setTextColor(TFT_NAVY); tft.setTextSize(2); tft.drawString(String(interval/1000), 300, 215); } void interval_down() { //Interval counter decrease interval -= 1000; if(interval<1000) interval = 1000; tft.fillRect(300,200,50,30,TFT_BLACK); tft.setTextColor(TFT_NAVY); tft.setTextSize(2); tft.drawString(String(interval/1000), 300, 215); } void setup(){ pinMode(A3, INPUT); pinMode(LED,OUTPUT); //Interrupt pinMode(WIO_5S_UP, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(WIO_5S_UP), interval_up, FALLING); pinMode(WIO_5S_DOWN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(WIO_5S_DOWN), interval_down, FALLING); tft.begin(); tft.setRotation(3); spr.createSprite(TFT_HEIGHT,200); tft.fillScreen(TFT_BLACK); } void loop(){ int value = analogRead(A3); //read analog data on the pin A3 blink(); double tmp = value*3.3/1023.0/0.01; //calculate temparature spr.fillSprite(TFT_WHITE); if (data.size() == max_size) { data.pop();//remove the first read variable } data.push(tmp); //store in data auto header = text(0, 0) .value("Temparature") .align(center) .valign(vcenter) .width(tft.width()) .thickness(2); header.height(header.font_height() * 2); header.draw(); //Header height is the twice the height of the font auto content = line_chart(20, header.height()); //(x,y) where the line graph begins content .height(200 - header.height() * 1.5) //actual height of the line chart .width(tft.width() - content.x() * 2) //actual width of the line chart .based_on(0.0) //Starting point of y-axis, must be a float .show_circle(false) //drawing a cirle at each point, default is on. .value(data) //passing through the data to line graph .color(TFT_PURPLE) //Setting the color for the line .draw(); spr.pushSprite(0, 0); // display current temarature tft.setTextColor(TFT_YELLOW); tft.setTextSize(2); tft.drawString("Current Temp. : ", 10, 215); tft.fillRect(200,200,100,30,TFT_BLACK); tft.setTextSize(3); tft.drawString(String(tmp), 200, 208); tft.fillRect(300,200,50,30,TFT_BLACK); tft.setTextColor(TFT_NAVY); tft.setTextSize(2); tft.drawString(String(interval/1000), 300, 215); delay(interval); }