最近天氣變冷了,女友一直說很冷,但是實際到底溫度多少,我還真的不知道,手機看也是地區的平均氣溫,身為工程師,當然是自己來土炮一個溫度計阿!!!哈哈哈
可惜的是手邊只有BMP180 模組,想說都它有個功能可以拿來量溫度,那就決定是他了!BMP180!
目標
透過TFT螢幕顯示房間的溫度與氣壓
材料
Arduino NANO
TFT 1.8 SPI
BMP180
杜邦縣若干條
軟體
Arduino IDE
1.8.12
Arduino lib 安裝
開啟程式庫管理員->輸入BMP180並安裝
開啟程式庫管理員->輸入TFT並安裝
接線說明
TFT模組 |
Arduino NANO |
LED |
3.3V |
SCK |
D13 |
SDA |
D11 |
A0 |
D9 |
RESET |
D8 |
CS |
D10 |
GND |
GND |
VCC |
+5V |
BMP180 |
Arduino NANO |
VIN |
+5V |
GND |
GND |
SCL |
A5 |
SDA |
A4 |
程式碼
程式碼部分小弟就全數奉上了,希望有幫到各位看官
#include<Wire.h> #include#include #include // pin definition for Arduino UNO #define cs 10 #define dc 9 #define rst 8 Adafruit_BMP085 bmp; TFT TFTscreen = TFT(cs, dc, rst); String TempTextShow=""; String PresTextShow=""; char TempPrintout[5]; char PresPrintout[5]; float LastTmp = 0.0; float LastPressure = 0.0; void setup() { Serial.begin(9600); //initialize the library TFTscreen.begin(); // clear the screen with a black background TFTscreen.background(0, 0, 0); //set the text size TFTscreen.setTextSize(2); TFTscreen.stroke(155,155,155); TFTscreen.text("Temperture:",12, 10); TFTscreen.text("Pressure:",12, 50); if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); TFTscreen.text("ERROR", 70, 30); TFTscreen.text("ERROR", 70, 70); while (1) {} } LastTmp = bmp.readTemperature(); LastPressure = bmp.readSealevelPressure(); } void loop() { if(LastTmp != bmp.readTemperature()) { //Clear Old Data TFTscreen.stroke(0, 0, 0); TFTscreen.text(TempPrintout, 70, 30); LastTmp = bmp.readTemperature(); TempTextShow = String(LastTmp); TempTextShow.toCharArray(TempPrintout, 5); } if(LastPressure != bmp.readSealevelPressure()) { //Clear Old Data TFTscreen.stroke(0, 0, 0); TFTscreen.text(PresPrintout, 70, 70); LastTmp = bmp.readSealevelPressure(); PresTextShow = String(LastPressure); PresTextShow.toCharArray(PresPrintout, 5); } TFTscreen.stroke(155, 155, 155); TFTscreen.text(TempPrintout, 70, 30); TFTscreen.text(PresPrintout, 70, 70); delay(1000); }
參考資料
https://randomnerdtutorials.com/guide-to-1-8-tft-display-with-arduino/
https://www.arduino.cc/en/Reference/TFTConstructor
https://www.arduino.cc/en/Tutorial/LibraryExamples/EsploraTFTTemp
沒有留言:
發佈留言