Interfacing Arduino with DHT11 sensor and ST7735 TFT display
- Arduino board
- ST7735S (ST7735R) TFT screen
- DHT11 (RHT01) humidity and temperature sensor —-> datasheet
- 5 x 1k ohm resistor
- 4.7k ohm resistor
- Breadboard
- Jumper wires

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