Advertisement
Guest User

arduino temperature/humidity code

a guest
Aug 19th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1.  
  2. #include <Adafruit_GFX.h>      // include Adafruit graphics library
  3. #include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
  4. #include <DHT.h>               // include DHT library
  5.  
  6. #define TFT_RST   8      // TFT RST pin is connected to arduino pin 8
  7. #define TFT_CS    9      // TFT CS  pin is connected to arduino pin 9
  8. #define TFT_DC    10     // TFT DC  pin is connected to arduino pin 10
  9. // initialize ST7735 TFT library
  10. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  11.  
  12. #define DHTPIN  A0           // DHT11 data pin is connected to Arduino analog pin 0
  13. #define DHTTYPE DHT11        // DHT11 sensor is used
  14. DHT dht11(DHTPIN, DHTTYPE);  // initialize DHT library
  15.  
  16. void setup(void)
  17. {
  18.   tft.initR(INITR_BLACKTAB);     // initialize a ST7735S chip, black tab
  19.   tft.fillScreen(ST7735_BLACK);  // fill screen with black color
  20.   tft.drawFastHLine(0, 50,  tft.width(), ST7735_BLUE);   // draw horizontal blue line at position (0, 50)
  21.   tft.drawFastHLine(0, 102,  tft.width(), ST7735_BLUE);  // draw horizontal blue line at position (0, 102)
  22.  
  23.   tft.setTextColor(ST7735_WHITE, ST7735_BLACK);  // set text color to white and black background
  24.   tft.setTextSize(1);                 // text size = 1
  25.   tft.setCursor(4, 16);               // move cursor to position (4, 16) pixel
  26.   tft.print("ARDUINO + ST7735 TFT");
  27.   tft.setCursor(22, 33);              // move cursor to position (22, 33) pixel
  28.   tft.print("+ DHT11 SENSOR");
  29.   tft.setTextColor(ST7735_GREEN, ST7735_BLACK);     // set text color to green and black background
  30.   tft.setCursor(25, 61);              // move cursor to position (25, 61) pixel
  31.   tft.print("TEMPERATURE =");
  32.   tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);  // set text color to yellow and black background
  33.   tft.setCursor(34, 113);              // move cursor to position (34, 113) pixel
  34.   tft.print("HUMIDITY =");
  35.   tft.setTextSize(2);                 // text size = 2
  36.  
  37.   // initialize DHT11 sensor
  38.   dht11.begin();
  39. }
  40.  
  41. char _buffer[7];
  42.  
  43. // main loop
  44. void loop()
  45. {
  46.   // read humidity
  47.   byte humi = dht11.readHumidity();
  48.   // read temperature
  49.   byte temp = dht11.readTemperature();
  50.  
  51.   // print temperature (in °C)
  52.   sprintf(_buffer, "%02u.0", temp);
  53.   tft.setTextColor(ST7735_RED, ST7735_BLACK);  // set text color to red and black background
  54.   tft.setCursor(29, 78);
  55.   tft.print(_buffer);
  56.   tft.drawCircle(83, 80, 2, ST7735_RED);  // print degree symbol ( ° )
  57.   tft.setCursor(89, 78);
  58.   tft.print("C");
  59.  
  60.   // print humidity (in %)
  61.   sprintf(_buffer, "%02u.0 %%", humi);
  62.   tft.setTextColor(ST7735_CYAN, ST7735_BLACK);  // set text color to cyan and black background
  63.   tft.setCursor(29, 130);
  64.   tft.print(_buffer);
  65.  
  66.   delay(1000);    // wait a second
  67.  
  68. }
  69.  
  70. // end of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement