Getting started with ESP32: Build a weather station (BMP280 + SSD1306)

This getting started tutorial with ESP32 describes how to build a weather station using ESP32 using BMP280 and SSD1306. In this project, the ESP32 displays sensor (BMP280) readings using an LCD display (SSD1306). Building a weather station with ESP32 is a simple IoT project but it is useful to get started with ESP32 and explore its main features. In this project, to monitor the pressure and temperature we will use BMP280. So, this ESP32 tutorial explores how to:

  • use BMP280 or BME280 to get the current temperature and pressure
  • use SSD1306: an LCD display that will show the readings from sensors

To build this project, we will use VS Code with PlatformIO Plugin. Below the link to download the Weather station source code to run in your ESP32.

The final result is shown in the picture below:

Getting started with ESP32 Building a weather station

Components required

To follow this tutorial, you need these components:

If you don’t have a BMP280 sensor, you can use a BME280. If you use the BME280 sensor you can monitor the humidity too.

BMP280 sensor module description

BMP280 is a sensor module that measures pressure and temperature. If you have BME280 it is possible to measure the humidity too. There are several versions of this sensor. The one used in this ESP32 weather station uses I2C communication protocol.

SSD1306 LCD display

SSD1306 is an LCD display used to display readings from the sensor. There are several versions of this LCD display. They have different width and height in terms of pixels. The one used in this project is 128×64 monochrome display. SSD1306 uses I2C protocol, anyway you can use the SPI version too. In this case, you have to change the connections.

Schematic: ESP32 + BMP280 + SSD1306

First of all, let’s see how to connect the ESP32 to the BMP280 and to the SSD1306. The schema is shown below:

Getting started with ESP32 Building a weather station

The BMP280 and the SSD1306 are I2C devices so we have to connect four pins:

  • The Vcc pin
  • The ground GND pin
  • The clock CLK pin
  • The data SDA pin

The clock and the data pins connect to the EPS32 I2C pins as shown above.

Get BMP280 sensor readings using ESP32

In this first step, we will read the data from the sensor (BMP280) connected to the ESP32. This is very simple. First of all, we have to import the Adafruit Library to manage the BMP280. If you don’t know how to do read the Setting up the ESP32 IDE paragraph to know how to get started with ESP32 and PlatformIO.

#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#define BMP_SDA 21
#define BMP_SCL 22
Adafruit_BMP280 bmp280;
void setup() {
  Serial.println("Initializing BMP280");
  boolean status = bmp280.begin(0x76);
  if (!status) {
    Serial.println("Not connected");
void loop() {
  float temp = bmp280.readTemperature();
  float press = bmp280.readPressure() / 100;
Code language: PHP (php)

The code is quite simple. After including the definitions, the ESP32 code declares, at line 7, the object that we will use to connect to the BMP280 sensor.

In the setup() method, we establish the connection between the ESP32 and the BMP280. As you may notice, the code uses the address 0x76. Be sure that your sensor has the same I2C address or change it according to your needs.

Finally, in the loop() method, we read the temperature and the pressure:

  • bmp280.readTemperature()
  • bmp280.readPressure()

The temperature is in Celsius while the pressure is expressed in Pascal. To convert it in millibar it is necessary to divide the sensor reading by 100.

If you are using a BME280, you can read, in the same way, the humidity too. That’s all, we are ready to show the result.

Display sensor readings using SSD1306 display

In this last part of this getting started with ESP32 project, we will show the values read from the sensor and show them using an LCD display (SSD1306). You can use another compatible display to show the result. Let us see the code:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
void loop() {
  float temp = bmp280.readTemperature();
  float press = bmp280.readPressure() / 100;
}Code language: PHP (php)

A few things to notice in this piece of code. First of all, if you don’t know how to import the library to manage the SSD1306 please go to the next paragraph. After importing the definitions, at line 4, we define the object that will handle the display. The display used in this ESP32 weather station is an LCD 128×64, the width and the height are the values used in the definition. If your display has different dimensions replace the values with your width and height. At line 8, the code attempts to connect to the display. Finally, it configures the display setting the text size and the text color.

In the loop() method, we simply show the temperature and the pressure read from the sensor. That’s all. You have built your weather station using ESP32.

ESP32 is a great device and it can be used in several scenarios. For example, you can read how to connect ESP32 to AWS IoT Core.

Setting up the ESP32 IDE

Once the pins are connected, we can focus our attention on how to setup the IDE that we will use to build the ESP32 weather station project. As said before, we will use Visual Studio Code with Platform IO plugin. You can download Visual Studio code from this link. Then click on the Extensions on the left side menu and look for PlatformIO plugin. When you have installed it you will get something like this:

That’s all we are ready to use the IDE.

Creating a new ESP32 project

It is time to create a new ESP32 project. Open the PlatformIO plugin and create a new project:

Then, select the ESP32 platform:

That’s all…now you can create the new project where we will develop the getting started with ESP32 weather station.

Importing the libraries

Once the project is created, we can import the libraries to handle the BMP280 and the SSD1306. Click on Libraries (left side menu) and look for:

  • BMP280
  • SSD1306

Once you have found the Adafruit libraries you can import it into your project and you are ready to use them.

If you a Wio Terminal you can read how to monitor temperature, humidity and pressure using Wio Terminal. It uses the same BME280 sensor and display several information.

Wrapping up….

At the end of this article, you learned how to get started with ESP32 building a simple weather station. This tutorial discovered how to connect ESP32 with BMP280 and how to show sensor readings using SSD1306. You can further improve this ESP32 project adding new sensors or new features.


  • Add Your Comment