ESP8266 NodeMCU Interface with SSD1306 OLED and BMP280 sensor

This post shows how to interface ESP8266 NodeMCU WiFi development board (ESP12-E module) with BMP280 barometric pressure and temperature sensor.
The NodeMCU reads temperature & pressure values from the BMP280 sensor and prints them (respectively in °C and hPa) on SSD1306 OLED display (128×64 pixel).
In this project the BMP280 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

About the BMP280 sensor:
The BMP280 sensor from Bosch Sensortec is a low cost pressure and temperature 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
Interface: I2C and SPI

Hardware Required:

  • ESP8266 NodeMCU development board
  • SSD1306 OLED display with 128×64 Pixel resolution
  • BMP280 sensor module   —->  datasheet
  • micro USB cable (for programming and powering the circuit)
  • Breadboard
  • Jumper wires

ESP8266 NodeMCU with SSD1306 OLED and BMP280 sensor circuit:
Project circuit schematic diagram is shown below.

ESP8266 NodeMCU BMP280 sensor SSD1306 OLED I2C mode

Generally, the BMP280 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 BMP280 sensor are connected to the same I2C bus (slave devices).
The I2C slave address of the SSD1306 OLED differs from the one of the BMP280 sensor, this difference allows the master device (ESP8266EX microcontroller) to talk to one of them (only one at a time).

ESP8266 NodeMCU with SSD1306 OLED and BMP280 sensor 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 BMP280 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 BMP280 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.

In the code there are total of 3 libraries, they’re included in the code as follows:

Definition of the SSD1306 OLED reset pin (RES) and initialization of its library are as shown below:

As any other I2C device, the BMP280 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 BMP280 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:

The initialization of the BMP280 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:

Note that the BMP280 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 and pressure values are displayed on the SSD1306 OLED screen.
If there is a problem with the BMP280 sensor (for example wrong device address) the screen will display Connection Error.

Full Arduino Code:

The following picture shows my simple hardware circuit result:

ESP8266 NodeMCU with SSD1306 OLED and BMP280 sensor

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.