Getting Started With ESP32: Build a Weather Station
Getting Started With ESP32: Build a Weather Station
This tutorial demonstrates how you can create a weather station that interprets and displays weather data on an LCD screen using sensor readings.
Join the DZone community and get the full member experience.Join For Free
This tutorial with ESP32 describes how to build a weather station using ESP32 using BMP280 and SSD1306. This ESP32 project displays sensor readings using an LCD display. 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. This is a very interesting IDE that helps us.
The final result is shown in the picture below:
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 a 128 × 64 monochrome display. SSD1306 uses I2C protocol, but 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:
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.
Read Data From BMP280 Sensor 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.
The code is quite simple. After including the definitions, the ESP32 code declares, at line 9, 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:
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 ESP32 and SSD1306
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:
A few things to notice in this piece of ESP32 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.
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 set up 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:
Once you have found the Adafruit libraries you can import it into your project and you are ready to use them.
At the end of this article, you've 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.
Published at DZone with permission of Francesco Azzola , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.