Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

MicroPython: BME280 with ESP32 and ESP8266 – Measure Temperature,

Humidity, and Pressure


In this user guide, we will take a look at the BME280 sensor which is used to pressure, humidity, and temperature. Firstly, we will
learn about BME280 and how to connect with Esp32 and with Esp8266. Secondly, we will write and upload its library in our
Integrated Development Environment (uPyCraft IDE). Next, we will work on a simple example to demonstrate its working and lastly,
we will measure Temperature, Humidity, and Pressure.

Table of Contents

Prerequisites
Before we start this lesson make sure you are familiar with and have the latest version of MicroPython firmware installed in your
ESP boards and have a running Integrated Development Environment(IDE) in which we will be doing the programming. We will be
using the same uPyCraft IDE as we have done previously when we learned how to blink and chase LEDs in microPython.
 Getting Started with MicroPython on ESP32 and ESP8266
 ESP32 and ESP8266 GPIO Programming with MicroPython – LED Blinking Example
If you are using Thonny IDE, you can check this getting started guide:
Learn more

 Getting Started with Thonny MicroPython IDE for ESP32 and ESP8266
We will cover the following content in this MicroPython tutorial: 
 How to create a BM280 web server using ESP32/ESP8266 and MicroPython 
 Display Temperature, Humidity, and Pressure readings on a web page with ESP32 and ESP8266 
BME280 Introduction
The BME280 sensor is used to measure readings regarding ambient temperature, barometric pressure, and relative humidity. It is
mostly used in web and mobile applications where low power consumption is key. This sensor uses I2C or SPI to communicate
data with the micro-controllers. Although there are several different versions of BME280 available in the market, the one we will be
studying uses I2C communication protocol and SPI.

1
I2C means Inter-Integrated Circuit and works on the principle of the synchronous, multi-master multi-slave system. With BME280
and the ESP boards, the ESP32/ESP8266 acts as a master, and the BME280 sensor as a slave because it is an external device,
acts as a slave. The ESP development boards communicate with the BME280 sensor through the I2C protocol to temperature,
barometric pressure, and relative humidity.
Pinout Diagram
The figure below shows the BME280 sensor and its pinout.
Learn more

2
  BME280 Pinout
 VCC: connected with 3.3V
 SCL: used to generate the clock signal
 SDA: used in sending and receiving data

ESP32 and ESP8266 Schematic with BME280


The connection of BME280 with the ESP boards is very easy. We have to connect the VCC terminal with 3.3V, ground with the
ground (common ground), SCL of the sensor with SCL of the module, and SDA of the sensor with the SDA pin of the ESP modules.
The I2C pin in ESP32 for SDA is GPIO21 and for SCL is GPIO22. Whereas, in ESP8266 the default I2C pins for SDA are GPIO4
and for SCL are GPIO5. The connections between the two devices can be seen below.

ESP32 ESP8266 BME280


VCC=3.3V 3.3V Vin
GPIO21(I2C SDA) GPIO4 (D2) SDA
GPIO22 (I2C SCL) GPIO5 (D1) SCL
GROUND GROUND GROUND
ESP32 I2C Pins
The I2C pins stated above are set in default. If we want to change the GPIO pins we have to set them in code. The diagrams below
show the pinout for the ESP32 and Esp8266 respectively.

3
ESP32 I2C Pins
ESP8266 I2C Pins
The following figure shows the I2C pins of ESP8266 NodeMCU:
Learn more

4
5
ESP8266 I2C Pins
More information on ESP32 and ESP8266 pins is available here:
 ESP32 GPIO Pins
 ESP8266 GPIO Pins
Components Required
We will need the following components to connect our ESP board with the BME280 sensor.
1. ESP32/ESP8266
2. BME280 Sensor
3. Connecting Wires
4. Breadboard
Follow the schematic diagrams below for both the ESP modules and connect them accordingly. If you are using ESP32 for this
project, connect the ESP32 device with BME280 as shown in the schematic diagram below:

6
Similarly, you are using ESP8266 NodeMCU for this project, connect the ESP8266 device with BME280 as shown in the schematic
diagram below:
Learn more

7
In some BME280 sensors as seen in the above connection diagram, the SCK terminal means the SCL pin and is connected with its
respective GPIO pin on the ESP board. Likewise, the SDI terminal means the SDA pin and is connected with its respective GPIO
pin on the board. Vin is connected with a 3.3V pin on the module and both the ESP board and the sensor is commonly grounded.

8
BME280 MicroPython Library
By default, MicroPython does not have an implementation of the BME280 library. But, MicroPyhon provides I2C API of ESP32 and
ESP8266 which can be used to read the temperature, humidity, and pressure values from the BME280 sensor. Fortunately, there is
one library available which is developed by Robert and can be downloaded from this link.
Hence, download the following library and upload it to ESP32/ESP8266 board with the name of BME280.py.
VIEW RAW CODE
Uploading BME280 Library with uPyCraft IDE
Now, we will look at how to install the BME280 library to be used in MicroPython. This step is required because MicroPython itself
does not contain the sensor’s library. Follow the steps in order to successfully install the library in the IDE.
 First, in the Tools section, click on the NEW FILE button and open a file.

 Then replicate the following BME280 library in that file. You can copy the library by clicking here.
 Name the file BME280.py and save it by choosing your desired directory.

 Now press the button DOWNLOAD AND RUN in the tools section.

You have now successfully uploaded the BME280 library o ESP32/ESP8266 using uPyCraft IDE. After that, we can use the above
library functions to read temperature, pressure, and humidity from the BM280 sensor. You can use a similar procedure to upload
files using Thonny IDE.
Measuring Temperature, Pressure & Humidity with BME280

9
As we have already uploaded the BME280 library to ESP32/ESP8266 boards. Now we can use the functions available in the
BME280 library to get sensor readings.
Let’s now look at an example to show the working of the sensor. We will connect our BME280 sensor with the ESP module via the
I2C protocol as shown above in the connection diagrams. We will see a MicroPython script code and after uploading it to our ESP
boards, we will see readings of temperature, pressure, and relative humidity printed on the MicroPython shell terminal.
BME280 MicroPython Code
Now let’s look at the MicroPython script for BME280 to get sensor readings. Copy the following code to the main.py file and upload
the main.py file to ESP32/ESP8266. This microPython script reads Pressure, Temperature and Humidity values from BME280 over
I2C lines and prints them on MicroPython shell console.
from machine import Pin, I2C #importing relevant modules & classes
from time import sleep
import BME280 #importing BME280 library

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266

while True:
bme = BME280.BME280(i2c=i2c) #BME280 object created
temperature = bme.temperature #reading the value of temperature
humidity = bme.humidity #reading the value of humidity
pressure = bme.pressure #reading the value of pressure

print('Temperature is: ', temperature) #printing BME280 values


print('Humidity is: ', humidity)
print('Pressure is: ', pressure)
sleep(10) #delay of 10s
How the Code Works?

Firstly, we will be importing the Pin class and I2C class from the machine module. This is because we have to specify the pin for
I2C communication. We also import the sleep module so that we will be able to add a delay of 10 seconds in between our readings.
Also, import the BME280 library which we have previously uploaded to ESP32 or ESP8266.

10
from machine import Pin, I2C #importing relevant modules & classes
from time import sleep
import BME280 #importing BME280 library
Defining ESP32/ESP8266 GPIO Pins for BME280

Now, we initialize the I2C method by giving it three arguments. The first argument specifies the GPIO pin for SCL. This is given as
GPIO22 for ESP32 and GPIO5 for ESP8266. The second parameter specifies the GPIO pin for the SDA. This is given as GPIO21
for ESP32 and GPIO4 for ESP8266. Keep in mind, these are the default I2C pins for SCL and SDA which we have used for both
the ESP boards respectively. The third parameter specifies the maximum frequency for SCL to be used.
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266
Next, we run an infinite loop inside which we create an object of BME280 named bme and access the temperature, humidity and
pressure through it.
while True:
bme = BME280.BME280(i2c=i2c) #BME280 object created
temperature = bme.temperature #reading the value of temperature
humidity = bme.humidity #reading the value of humidity
pressure = bme.pressure #reading the value of pressure
Finally, we will print the individual values in the terminal by giving the print command. We also add a delay of 10s after each set of
readings are displayed.
print('Temperature is: ', temperature) #printing BME280 values
print('Humidity is: ', humidity)
print('Pressure is: ', pressure)
sleep(10) #delay of 10s
Demo
To test the MicroPython script for BME280 with ESP32 and ESP8266, upload all main.py file to ESP32/ESP8266. After uploading
the MicroPython script, click on Enable/Reset button of ESP32 or ESP8266:
 

11
You will see Pressure, Temperature and Humidity readings on shell console:

MicroPython: Displaying BME280 Sensor values on OLED Display

12
In this section, we will see how to display BME280 Pressure, Temperature, Humidity values on a 0.96 SSD1306 OLED display
using MicroPython and ESP32/ESP8266. 
SSD1306 OLED Display MicroPython Library
We have already uploaded the BME280 MicroPython library to ESP32 and ESP8266 NodeMCU. For an OLED display, we will also
need to upload a library to ESP boards. 
Copy the following code which is a MicroPython library of OLED and upload the library using either uPycraft or Thonny IDE by
giving it the name of ssd1306.py. You should follow the same procedure to upload the library as we did for bme280.py.
Schematic – OLED with ESP32 and BME280
The table below shows the terminals of the three devices which would be connected together.

OLED Display ESP32 BME280


VCC VCC=3.3V VCC
GND GND GND
SCL GPIO22 SCL
SDA GPIO21 SDA
Assemble the circuit as shown in the schematic diagram below:

13
14
As you can see above, we have connected all the VCC terminals with a 3.3V power supply. The SCL terminals are connected with
GPIO22 and the SDA terminals are connected with GPIO21. The grounds are also common.
Schematic – OLED with ESP8266 NodeMCU and BME280
The table below shows the terminals of the three devices which would be connected together.

OLED Display ESP8266 BME280


VCC VCC=3.3V VCC
GND GND GND
SCL GPIO5 SCL
SDA GPIO4 SDA
Assemble the circuit as shown in the schematic diagram below:

15
16
As you can see above, we have connected all the VCC terminals with a 3.3V power supply. The SCL terminals are connected with
GPIO5 and the SDA terminals are connected with GPIO5. The grounds are also common.
MicroPython Code: Displaying BME280 readings on OLED Display

from machine import Pin, I2C #importing relevant modules & classes
from time import sleep
import BME280 #importing BME280 library
import ssd1306 #importing OLED library

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266
oled=ssd1306.SSD1306_I2C(128,64,i2c)

while True:
oled.fill(0)
bme = BME280.BME280(i2c=i2c) #BME280 object created
temperature = str(bme.temperature) #reading the value of temperature
humidity = str(bme.humidity) #reading the value of humidity
pressure = str(bme.pressure) #reading the value of pressure

print('Temperature is: ', temperature) #printing BME280 values


print('Humidity is: ', humidity)
print('Pressure is: ', pressure)
oled.text("Temp. "+temperature, 0, 0)
oled.text("PA "+pressure, 0, 20)
oled.text("Hum. "+humidity, 0,40)
oled.show() #display pix
sleep(10) #delay of 10s
How does the code work?

In the section, we only explained the Micropython code part which is used to display sensor values on OLED. Because reset of the
code is the same as we have used in previous sections to display sensor readings on the shell console. 

17
Import SSD1306 OLED library.
import ssd1306 #importing OLED library
Create an instance of the SSD1306_I2C() method by giving it the name of oled. The first two arguments to SSD1306_I2C() are the
size of the OLED, that is the number of columns and rows. A last argument is an object of I2C pins which we define with SoftI2C().
oled=ssd1306.SSD1306_I2C(128,64,i2c)
Clears the OLED display with led.fill() routine.
Learn more
oled.fill(0)
Take BME280 sensor temperature, humidity, and pressure samples and convert them into string. Also,concatenate units of these
values with strings.
bme = BME280.BME280(i2c=i2c) #BME280 object created
temperature = str(bme.temperature) #reading the value of temperature
humidity = str(bme.humidity) #reading the value of humidity
pressure = str(bme.pressure) #reading the value of pressure

print('Temperature is: ', temperature) #printing BME280 values


print('Humidity is: ', humidity)
print('Pressure is: ', pressure)
Finally, display the text along with sensor readings on OLED.
oled.text("Temp. "+temperature, 0, 0)
oled.text("PA "+pressure, 0, 20)
oled.text("Hum. "+humidity, 0,40)
At the end, call the show() method on oled method for changes to show on OLED. 
oled.show() #display pix
sleep(10) #delay of 10s
Demonstration
Upload the above code as a main.py file along with bme280.py and ssd1306.py to ESP32 or ESP8266. After that press the reset
button on your ESP board. You will see BME280 temperature, pressure, and humidity values on the OLED display as follows:
Learn more
18
Video Demo:

19

You might also like