This tutorial shows how to build a simple weather station using ESP8266 NodeMCU board (ESP12-E module) and BME280 barometric pressure, temperature & humidity sensor.
The NodeMCU microcontroller (ESP8266EX) 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. This display 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, this means it has 57600 pixels. This module works with 3.3V only and it does not support 5V.
TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.
To see how to interface the ESP8266 NodeMCU board with ST7789 TFT display, visit this post:
Interfacing ESP8266 NodeMCU With ST7789 TFT Display
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.
- ESP8266 NodeMCU board
- ST7789 TFT display module
- BME280 sensor module —-> Datasheet
- micro USB cable (for programming and powering the circuit)
- Jumper wires
ESP8266 NodeMCU with BME280 sensor and ST7789 TFT circuit:
Project circuit schematic diagram is shown below.
Generally, the BME280 Sensor module has at least 4 pins because it can work in SPI mode or I2C mode, it’s connected to the ESP8266 NodeMCU board as described below.
For the I2C mode we need 4 pins: VCC, GND, SDA and SCL where:
VCC is the supply pin which is connected to NodeMCU 3V3 pin,
GND (ground) is connected to NodeMCU GND pin,
SDA is I2C bus serial data line, connected to NodeMCU pin D2 (GPIO4),
SCL is I2C bus serial clock line, connected to NodeMCU pin D1 (GPIO5).
The ST7789 Display Module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).
The ST7789 TFT Display Module is connected to the NodeMCU board as follows:
GND is connected to pin GND of the NodeMCU board,
VCC and BL are connected to pin 3V3,
SCL pin is connected to D5 (ESP8266EX GPIO14),
SDA pin is connected to D7 (ESP8266EX GPIO13),
RES pin is connected to D4 (ESP8266EX GPIO2),
DC pin is connected to D3 (ESP8266EX GPIO0).
If the display module has a CS pin (Chip Select) then it should be connected to NodeMCU pin D8 (GPIO15).
Connecting the BLK pin is optional. The back light turns off when the BLK pin connected to the ground (GND).
Pins D5 (GPIO14) and D7 (GPIO13) are hardware SPI module pins of the ESP8266EX microcontroller respectively for SCK (serial clock) and MOSI (master-out slave-in).
ESP8266 NodeMCU with BME280 sensor and ST7789 TFT code:
The following Arduino code requires 3 libraries from Adafruit Industries:
The first library is a driver for the ST7789 TFT display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “st7789” and install the one from Adafruit).
The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.
The 3rd library is for the BME280 sensor, it may be installed using library manager (in the search box write “bme280” and choose the one from Adafruit).
The 3 libraries can be installed manually, first download them from the following links:
Adafruit ST7789 TFT Library —-> Direct Link
Adafruit Graphics Library —-> Direct Link
Adafruit BME280 Library —-> Direct Link
After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for the other library files.
In the code there are total of 4 libraries, they’re included in the code as follows:
|#include <Wire.h> // Wire library (required for I2C devices)|
#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
The connection of the ST7789 TFT Display with the NodeMCU board is as shown below where the display is connected to hardware SPI module of the NodeMCU (pins: SCK and MOSI):
|// ST7789 TFT module connections|
#define TFT_DC D3 // TFT DC pin is connected to NodeMCU pin D3 (GPIO0)
#define TFT_RST D4 // TFT RST pin is connected to NodeMCU pin D4 (GPIO2)
#define TFT_CS D8 // TFT CS pin is connected to NodeMCU pin D8 (GPIO15)
// initialize ST7789 TFT library with hardware SPI module
// SCK (CLK) —> NodeMCU pin D5 (GPIO14)
// MOSI(DIN) —> NodeMCU pin D7 (GPIO13)
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
And the TFT display is initialized using the following command:
|// if the display has CS pin try with SPI_MODE0|
tft.init(240, 240, SPI_MODE2); // init ST7789 display 240×240 pixel
As any other I2C device, the BME280 Sensor has an I2C slave address which may be 0x76 or 0x77. This address depends on the connection of the SDO pin (used for SPI mode as serial data out or MISO), if the SDO pin is connected (directly or through resistor) to VCC (3.3V) the address will be 0x77, and if it’s connected to GND the address will be 0x76.
The default I2C address of the BME280 library is defined as 0x77 and my device I2C address is 0x76.
In the code, the definition of the I2C slave address and the initialization of its library are shown below:
|// define device I2C address: 0x76 or 0x77 (0x77 is library default address)|
#define BME280_I2C_ADDRESS 0x76
// initialize Adafruit BME280 library
The initialization of the BME280 sensor is done using the function begin() which returns 1 if OK and 0 if error. In the code the initialization with the previously defined address is as shown below:
Reading the values of temperature and pressure:C
|// 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
Note that the BME280 sensor library returns the value of the pressure in Pa unit and to convert it to hPa we’ve to divide it by 100.
1 bar = 10000 Pa = 100 hPa. ( 1 hPa = 100 Pa = 1 millibar)
Temperature, humidity and pressure values are printed on the ST7789 TFT display.
If there is a problem with the BME280 sensor (for example wrong device address) the screen will display Connection Error
Full Arduino Code:
The picture below shows my simple protoboad circuit: