This tutorial shows how to build a simple weather station using Arduino board and BME280 barometric pressure, temperature & humidity sensor.
The Arduino microcontroller (ATmega328P for uno, Nano …) reads temperature & humidity & pressure values from the BME280 sensor and prints them (respectively in °C & rH% & hPa) on ST7789 TFT display.
The ST7789 TFT module contains a display controller with the same name: ST7789. It’s a color display that uses SPI interface protocol and requires 3, 4 or 5 control pins, it’s low cost and easy to use.
The ST7789 TFT is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel (some may come with 320×240 pixel), this means it has 57600 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).
TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.
To see how to interface Arduino with ST7789 TFT display, visit this post:
Interfacing Arduino with ST7789 TFT Display – Graphics Test Example
About the BME280 sensor:
The BME280 sensor from Bosch Sensortec is a low cost digital pressure, temperature and humidity sensor with good accuracy. Because pressure changes with altitude we can use it as an altimeter with ±1 meter accuracy (pressure accuracy = ±1 hPa). Some parameters of the sensor are listed below:
Pressure range: 300 … 1100 hPa (equivalent to +9000…-500m above/below sea level)
Pressure resolution: 0.01 hPa ( < 10 cm)
Temperature range: -40 … 85 °C
Temperature resolution: 0.01 °C
Humidity range: 0 … 100 %
Interface: I2C and SPI
Supply voltage range: 1.71 … 3.6 V
In this project the BME280 sensor is used in I2C mode.
Hardware Required:
- Arduino board
- ST7789 TFT display module (1.3″, 1.54″ …)
- BME280 sensor module (with built-in 3.3V regulator and level shifter) —-> BME280 datasheet
- 4 x 3.3k ohm resistor (+1 if the display module has CS pin)
- 4 x 2.2k ohm resistor (+1 if the display module has CS pin)
- Breadboard
- Jumper wires

1
2
3
|
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
#include <Adafruit_BME280.h> // Adafruit BME280 sensor library
|
1
2
3
4
|
// ST7789 TFT module connections
#define TFT_CS 10 // define chip select pin
#define TFT_DC 9 // define data/command pin
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
|
1
|
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
|
1
2
3
|
// initialize the ST7789 display (240x240 pixel)
// if the display has CS pin try with SPI_MODE0
tft.init(240, 240, SPI_MODE2);
|
1
2
3
|
// 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 °C
float humi = bme280.readHumidity(); // get humidity in %
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
103
104
105
|
/***********************************************************************
*
* Arduino with ST7789 TFT display (240x240 pixel) and BME280
* barometric pressure & temperature & humidity sensor..
* This is a free software with NO WARRANTY.
* http://simple-circuit.com/
*
***********************************************************************/
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
#include <Adafruit_BME280.h> // Adafruit BME280 sensor library
// ST7789 TFT module connections
#define TFT_CS 10 // define chip select pin
#define TFT_DC 9 // define data/command pin
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
// initialize Adafruit ST7789 TFT library with hardware SPI module
// MOSI(SDA) ---> Arduino digital pin 11
// SCK (SCL) ---> Arduino digital pin 13
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BME280_I2C_ADDRESS 0x76
// initialize Adafruit BME280 library
Adafruit_BME280 bme280;
void setup(void) {
// initialize the ST7789 display (240x240 pixel)
// if the display has CS pin try with SPI_MODE0
tft.init(240, 240, SPI_MODE2);
// if the screen is flipped, remove this command
tft.setRotation(2);
// fill the screen with black color
tft.fillScreen(ST77XX_BLACK);
tft.setTextWrap(false); // turn off text wrap option
// initialize the BME280 sensor
if( bme280.begin(BME280_I2C_ADDRESS) == 0 )
{ // connection error or device address wrong!
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); // set text color to white and black background
tft.setTextSize(4); // text size = 4
tft.setCursor(3, 88); // move cursor to position (3, 88) pixel
tft.print("Connection");
tft.setCursor(63, 126); // move cursor to position (63, 126) pixel
tft.print("Error");
while(1); // stay here
}
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); // set text color to green and black background
tft.setTextSize(3); // text size = 3
tft.setCursor(17, 0); // move cursor to position (17, 0) pixel
tft.print("TEMPERATURE:");
tft.setCursor(44, 89); // move cursor to position (44, 89) pixel
tft.print("HUMIDITY:");
tft.setCursor(44, 178); // move cursor to position (44, 178) pixel
tft.print("PRESSURE:");
tft.setTextSize(4); // text size = 4
}
// variables
char _buffer[11];
// main loop
void loop() {
delay(1000); // wait a second
// read temperature, humidity and pressure from the BME280 sensor
float temp = bme280.readTemperature(); // get temperature in °C
float humi = bme280.readHumidity(); // get humidity in %
float pres = bme280.readPressure(); // get pressure in Pa
// print data on the display
// 1: 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(ST77XX_RED, ST77XX_BLACK); // set text color to red and black background
tft.setCursor(15, 34);
tft.print(_buffer);
tft.drawCircle(173, 40, 4, ST77XX_RED); // print degree symbol ( ° )
tft.drawCircle(173, 40, 5, ST77XX_RED);
tft.setCursor(183, 34);
tft.print("C");
// 2: print humidity
sprintf( _buffer, "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK); // set text color to cyan and black background
tft.setCursor(39, 123);
tft.print(_buffer);
// 3: print pressure (in hPa)
sprintf( _buffer, "%04u.%02uhPa", (int)(pres/100), (int)((uint32_t)pres % 100) );
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK); // set text color to yellow and black background
tft.setCursor(0, 212);
tft.print(_buffer);
}
// end of code.
|