This post shows how to build a weather station using ESP8266 NodeMCU development board (ESP12-E module) and BME280 barometric pressure, temperature & humidity sensor.
The NodeMCU reads temperature & humidity & pressure values from the BME280 sensor and prints them (respectively in °C & RH% & hPa) on SSD1306 OLED display (128×64 pixel).
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.
The SSD1306 OLED display used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode and I2C mode) such as soldering, placing jumpers …
To see how to interface ESP8266 NodeMCU with SSD1306 OLED display, visit the following post:
ESP8266 NodeMCU interfacing with SSD1306 OLED
Hardware Required:
- ESP8266 NodeMCU development board
- SSD1306 OLED display with 128×64 pixel resolution
- BME280 sensor module —-> datasheet
- micro USB cable (for programming and powering the circuit)
- Breadboard
- Jumper wires
NodeMCU with BME280 sensor SSD1306 OLED 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. For the I2C mode we need 4 pins: VCC, GND, SDA and SCL where:
GND (ground) is connected to NodeMCU GND pin,
VCC is the supply pin which is connected to NodeMCU 3V3 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 D3 (GPIO0).
The SSD1306 OLED display is connected to the NodeMCU board as follows:
SSD1306 OLED GND goes to NodeMCU GND pin,
SSD1306 OLED VDD to NodeMCU 3V3,
SSD1306 OLED SDA pin (serial data) to NodeMCU pin D2 (GPIO4),
SSD1306 OLED SCK pin (serial clock) to NodeMCU pin D3 (GPIO0),
SSD1306 OLED RES pin (reset) to NodeMCU pin D1 (GPIO5).
The SSD1306 OLED display DC pin is connected to VDD (3.3V) which means I2C slave address of the device is 0x3D. If the DC pin is connected to ground (GND) then the I2C slave address becomes 0x3C.
The SSD1306 OLED and the BME280 sensor are connected to the same I2C bus (slave devices).
The I2C slave address of the SSD1306 OLED differs from the one of the BME280 sensor, this difference allows the master device (ESP8266EX microcontroller) to talk to one of them (only one at a time).
NodeMCU with BME280 sensor SSD1306 OLED code:
The following Arduino code requires 3 libraries from Adafruit Industries:
The first library is a driver for the SSD1306 OLED display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “ssd1306” and install the one from Adafruit).
The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.
The third library is for the BME280 sensor.
The previous 3 libraries can also be installed manually, download links are below:
Adafruit SSD1306 OLED driver —-> direct link
Adafruit graphics library —-> direct link
Adafruit BME280 Library —-> direct link
You may need to install the Adafruit Unified Sensor library if it’s not already installed, download link is below:
Adafruit Unified Sensor 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.
The 3 libraries are included in the code as:
1 2 3 | #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #include <Adafruit_BME280.h> // include Adafruit library for BME280 sensor |
Definition of the SSD1306 OLED reset pin (RES) and initialization of its library are as shown below:
1 2 3 4 | // define SSD1306 OLED reset at ESP8266 GPIO5 (NodeMCU D1) #define OLED_RESET 5 // initialize Adafruit display library Adafruit_SSD1306 display(OLED_RESET); |
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:
1 2 3 4 | // define device I2C address: 0x76 or 0x77 (0x77 is library default address) #define BME280_I2C_ADDRESS 0x76 // initialize Adafruit BME280 library Adafruit_BME280 bme280; |
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:
1 | bme280.begin(BME280_I2C_ADDRESS) |
Reading the values of temperature and pressure is done as shown below:
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 |
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)
Pa: Pascal
hPa: hectoPascal
Temperature, humidity and pressure values are displayed on the SSD1306 OLED screen.
If there is a problem with the BME280 sensor (for example wrong device address) the screen will display Connection Error.
Full Arduino Code:
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 106 107 108 | /************************************************************************************** ESP8266 NodeMCU with SSD1306 OLED display (128x64 Pixel) and BME280 sensor. BME280 is barometric pressure, temperature and humidity sensor. This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #include <Adafruit_BME280.h> // include Adafruit BME280 sensor library // define SSD1306 OLED reset at ESP8266 GPIO5 (NodeMCU D1) #define OLED_RESET 5 // initialize Adafruit display library Adafruit_SSD1306 display(OLED_RESET); // 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) { delay(1000); // wait a second // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz Wire.begin(4, 0); // initialize the SSD1306 OLED display with I2C address = 0x3D display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // clear the display buffer. display.clearDisplay(); display.setTextSize(1); // text size = 1 display.setTextColor(WHITE, BLACK); // set text color to white and black background display.setTextWrap(false); // disable text wrap display.setCursor(0, 4); // move cursor to position (0, 4) pixel display.print("W\nE\nA\nT\nH\nE\nR"); display.setCursor(123, 4); // move cursor to position (123, 4) pixel display.println("S"); display.setCursor(123, display.getCursorY()); display.println("T"); display.setCursor(123, display.getCursorY()); display.println("A"); display.setCursor(123, display.getCursorY()); display.println("T"); display.setCursor(123, display.getCursorY()); display.println("I"); display.setCursor(123, display.getCursorY()); display.println("O"); display.setCursor(123, display.getCursorY()); display.println("N"); display.display(); // update the display // initialize the BME280 sensor if( bme280.begin(BME280_I2C_ADDRESS) == 0 ) { // connection error or device address wrong! display.setCursor(34, 23); display.print("Connection"); display.setCursor(49, 33); display.print("Error"); display.display(); // update the display while(1) // stay here delay(1000); } display.setCursor(29, 0); display.print("TEMPERATURE:"); display.setCursor(38, 23); display.print("HUMIDITY:"); display.setCursor(38, 46); display.print("PRESSURE:"); display.display(); // update the display } void loop() { // 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 delay(1000); // wait a second // print data on the LCD // 1: print temperature display.setCursor(37, 10); if(temp < 0) display.printf("-%02u.%02u C", (int)abs(temp) % 100, (int)(abs(temp) * 100) % 100 ); else display.printf(" %02u.%02u C", (int)temp % 100, (int)(temp * 100) % 100 ); // print degree symbols ( ° ) display.drawRect(75, 10, 3, 3, WHITE); // 2: print humidity display.setCursor(43, 33); display.printf("%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 ); // 3: print pressure display.setCursor(31, 56); display.printf("%04u.%02u hPa", (int)(pres/100), (int)((uint32_t)pres % 100) ); // update the display display.display(); delay(1000); // wait a second } // end of code. |
The following picture shows my simple hardware circuit:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.