Arduino weather station with ST7735 color TFT display
- Arduino uno board
- ST7735S (ST7735R) TFT display
- BME280 sensor module (with built-in 3.3V regulator and level shifter) —-> BME280 datasheet
- 5 x 1k 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 <Adafruit_BME280.h> // include Adafruit BME280 sensor 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
4
|
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BME280_I2C_ADDRESS 0x76
Adafruit_BME280 bme280; // initialize Adafruit BME280 library
|
1
|
bme280.begin(BME280_I2C_ADDRESS)
|
1
2
3
4
|
// read temperature, humidity and pressure from the BME280 sensor
float temp = bme280.readTemperature(); // get temperature in degree Celsius
float humi = bme280.readHumidity(); // get humidity in rH%
float pres = bme280.readPressure(); // get pressure in Pa
|
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* Arduino weather station with ST7735 color TFT display (128x160 pixel) and
* BME280 barometric pressure, temperature & humidity 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 <Adafruit_BME280.h> // include Adafruit BME280 sensor 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 device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BME280_I2C_ADDRESS 0x76
Adafruit_BME280 bme280; // initialize Adafruit BME280 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, 30, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 30)
tft.setTextColor(ST7735_WHITE, ST7735_BLACK); // set text color to white and black background
tft.setTextSize(1); // text size = 1
tft.setCursor(4, 0); // move cursor to position (4, 0) pixel
tft.print("ARDUINO + ST7735 TFT");
tft.setCursor(19, 15); // move cursor to position (19, 15) pixel
tft.print("WEATHER STATION");
// initialize the BME280 sensor
if( bme280.begin(BME280_I2C_ADDRESS) == 0 )
{ // connection error or device address wrong!
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setTextSize(2); // text size = 2
tft.setCursor(5, 76); // move cursor to position (5, 76) pixel
tft.print("Connection");
tft.setCursor(35, 100); // move cursor to position (35, 100) pixel
tft.print("Error");
while(1); // stay here
}
tft.drawFastHLine(0, 76, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 76)
tft.drawFastHLine(0, 122, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 122)
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setCursor(25, 39); // move cursor to position (25, 39) pixel
tft.print("TEMPERATURE =");
tft.setTextColor(ST7735_CYAN, ST7735_BLACK); // set text color to cyan and black background
tft.setCursor(34, 85); // move cursor to position (34, 85) pixel
tft.print("HUMIDITY =");
tft.setTextColor(ST7735_GREEN, ST7735_BLACK); // set text color to green and black background
tft.setCursor(34, 131); // move cursor to position (34, 131) pixel
tft.print("PRESSURE =");
tft.setTextSize(2); // text size = 2
}
// main loop
void loop()
{
char _buffer[8];
// read temperature, humidity and pressure from the BME280 sensor
float temp = bme280.readTemperature(); // get temperature in °C
float humi = bme280.readHumidity(); // get humidity in rH%
float pres = bme280.readPressure(); // get pressure in Pa
// print temperature (in °C)
if(temp < 0) // if temperature < 0
sprintf( _buffer, "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
else // temperature >= 0
sprintf( _buffer, " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // set text color to yellow and black background
tft.setCursor(11, 54);
tft.print(_buffer);
tft.drawCircle(89, 56, 2, ST7735_YELLOW); // print degree symbol ( ° )
tft.setCursor(95, 54);
tft.print("C");
// 2: print humidity
sprintf( _buffer, "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK); // set text color to magenta and black background
tft.setCursor(23, 100);
tft.print(_buffer);
// 3: print pressure (in hPa)
sprintf( _buffer, "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) );
tft.setTextColor(0xFD00, ST7735_BLACK); // set text color to orange and black background
tft.setCursor(3, 146);
tft.print(_buffer);
tft.setCursor(91, 146);
tft.print("hPa");
delay(1000); // wait a second
}
// end of code.
|