2021年11月16日星期二

Arduino 小專題 TFT+BMP180

 最近天氣變冷了,女友一直說很冷,但是實際到底溫度多少,我還真的不知道,手機看也是地區的平均氣溫,身為工程師,當然是自己來土炮一個溫度計阿!!!哈哈哈

 可惜的是手邊只有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


程式碼

程式碼部分小弟就全數奉上了,希望有幫到各位看官

  1. #include<Wire.h>
  2. #include
  3. #include
  4. #include
  5. // pin definition for Arduino UNO
  6. #define cs 10
  7. #define dc 9
  8. #define rst 8
  9. Adafruit_BMP085 bmp;
  10. TFT TFTscreen = TFT(cs, dc, rst);
  11. String TempTextShow="";
  12. String PresTextShow="";
  13. char TempPrintout[5];
  14. char PresPrintout[5];
  15. float LastTmp = 0.0;
  16. float LastPressure = 0.0;
  17. void setup() {
  18. Serial.begin(9600);
  19. //initialize the library
  20. TFTscreen.begin();
  21. // clear the screen with a black background
  22. TFTscreen.background(0, 0, 0);
  23. //set the text size
  24. TFTscreen.setTextSize(2);
  25. TFTscreen.stroke(155,155,155);
  26. TFTscreen.text("Temperture:",12, 10);
  27. TFTscreen.text("Pressure:",12, 50);
  28. if (!bmp.begin()) {
  29. Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  30. TFTscreen.text("ERROR", 70, 30);
  31. TFTscreen.text("ERROR", 70, 70);
  32. while (1) {}
  33. }
  34. LastTmp = bmp.readTemperature();
  35. LastPressure = bmp.readSealevelPressure();
  36. }
  37. void loop() {
  38. if(LastTmp != bmp.readTemperature())
  39. {
  40. //Clear Old Data
  41. TFTscreen.stroke(0, 0, 0);
  42. TFTscreen.text(TempPrintout, 70, 30);
  43. LastTmp = bmp.readTemperature();
  44. TempTextShow = String(LastTmp);
  45. TempTextShow.toCharArray(TempPrintout, 5);
  46. }
  47. if(LastPressure != bmp.readSealevelPressure())
  48. {
  49. //Clear Old Data
  50. TFTscreen.stroke(0, 0, 0);
  51. TFTscreen.text(PresPrintout, 70, 70);
  52. LastTmp = bmp.readSealevelPressure();
  53. PresTextShow = String(LastPressure);
  54. PresTextShow.toCharArray(PresPrintout, 5);
  55. }
  56. TFTscreen.stroke(155, 155, 155);
  57. TFTscreen.text(TempPrintout, 70, 30);
  58. TFTscreen.text(PresPrintout, 70, 70);
  59. delay(1000);
  60. }

參考資料

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


沒有留言:

發佈留言

打賞按讚