Program to display temperature and humidity on the Raspberry Pi | そう備忘録

Program to display temperature and humidity on the Raspberry Pi

BME280

An article on connecting a temperature, humidity, and barometric pressure sensor (BME280) to a Raspberry Pi 3 B+ to measure temperature, humidity, and barometric pressure.

When I bought the sensor, the connector and module were separate and needed to be soldered together.

BME280 module

Connectors

Connectors are from the top of the picture.

  • Vcc: 3.3 to 5.0 V
  • GND:GND
  • SCL: I2C SCL
  • SDA: I2C SDA

Connectors
Connectors are from the top of the picture.

Vcc: 3.3 to 5.0 V
GND:GND
SCL: I2C SCL
SDA: I2C SDA

Connect to the pins on the Raspberry Pi.

Specifications

The specifications are as follows.

Interface

I2C(max 3.4MHz)

Supply voltage

3.3 – 5V(DC)

Measurement range

  • Temperature: -40 to +85 °C
  • humidity: 0 to 100 %
  • barometric pressure: 300 to 1100 hPa (elevation 9000m to -500m)

Minimum Unit

  • Temperature: 0.01 ℃
  • humidity: 0.008 %
  • barometric pressure: 0.18 Pa

Measurement error

  • Temperature: ±1 ℃
  • humidity: ±3 %
  • barometric pressure: ±1 Pa

The BME280 can measure not only temperature and humidity, but also barometric pressure, compared to the DHT11 and DHT22 that we tested earlier.

In terms of measurement error, the BME280 is more accurate than the DHT22 in humidity, but less accurate in temperature.

I2C address

  • SDO LOW: 0x76
  • SDO HIGH: 0x77

Sets purchased

I bought the BME280 not as a standalone unit, but as a set that includes a 0.96″ display module and a Wi-Fi module.

BME280,display module,Wi-Fi Modelu

I didn’t need the Wi-Fi module because it’s a Raspberry Pi, but I bought it because it was a cheap set.

I’d like to try out the display module and Wi-Fi module at some point.
This time, however, I’m only going to try the temperature, humidity, and pressure sensors.

Circuit Diagram

Circuit Diagram

advance preparation

Enabling I2C

Enable I2C in the Raspberry Pi menu.

Preferences, Raspberry Pi Configuration, Interfaces tab.

Enable I2C

I2C (Inter-Integrated Circuit) is a form of serial communication that allows multiple slaves (in this case, the BME280) to be connected to a master (in this case, the Raspberry Pi).

Two signal lines are used for data communication: SCL, which sends clock signals from the master to the slaves, and SDA, which is used for data input and output.

A small display module is also connected as a slave to display the temperature, humidity, and barometric pressure results on the display.

Module installation

Install the modules required to control I2C from Python using the following command.

sudo apt-get update
sudo apt install -y python-smbus
sudo pip install smbus2

Most of the modules for controlling I2C from the Raspberry Pi with Python seem to be python-smbus, but the BME280 used smbus2.

smbus2 has the same syntax as the python-smbus package, but is designed from scratch to extend the functionality.

For more details, please refer to this page.

If the above command successfully installs smbus2, you will see the following message

Successfully installed smbus2-0.3.0

Checking the connection address

From the LXTerminal of the Raspberry Pi, use the following command to check the connection address.

sudo i2cdetect -y 1

The display result shows that the address is 0x76.

Check the connection address.

Sample Program

Obtaining the program

Copy the SWITCH SCIENCE sample program (bme280_sample.py), which is available on github, onto your Raspberry Pi and run it.

The program source is not posted here, so please refer to the link above.

Checking and fixing

I copied bme280_sample.py to a suitable directory, opened it in the Thonny Python IDE, and ran it.

There are a few things to check and fix before running.

Make sure that the i2c_address = 0x76 in the 7th line of the program matches the connection address mentioned above.

Next, since this sample program is based on Python 2.7, we need to correct the syntax of the print statements in lines 95, 103, and 117, which are syntax errors in Python 3.

before : print "pressure : %7.2f hPa" % (pressure/100)
after : print ("pressure : {:7.2f} hPa".format(pressure/100))

Supplementary explanation

The following is a supplementary explanation of the sample program.

Adjustment parameters

The get_calib_param function in lines 21-59 obtains the adjustment parameters.

The adjustment parameters are values written to the non-volatile memory of the device during manufacturing, and contain parameter values to correct the acquired values of temperature, humidity, and barometric pressure.

Acquisition of data

The temperature, pressure, and humidity data are acquired together from the addresses F7h to FEh by the readData function in lines 61 to 71.

Calculation of temperature, humidity, and barometric pressure

The compensate_P function on lines 73 to 95 calculates the atmospheric pressure, the compensate_T function on lines 97 to 103 calculates the temperature, and the compensate_H function on lines 105 to 117 calculates the humidity.

The corrections are made from the correction values obtained by the get_calib_param function and the values obtained by the readData function.

Initial processing

The setup function in lines 120 to 135 is the initial processing.

Main processing

Lines 142-144 are the main processing.

It calls the readData function.

Execution result

Run Thonny Python IDE (application) on RaspberryPi and press F5. When executed with F5, temp (temperature), pressure (air pressure), and hum (humidity) are output.

Sample program execution results

This concludes this article.

Finally.

I hope this article will be useful to someone somewhere.

souichirou kikuchi

I'm Japanese. A reminder to remember what I've done. I'm blogging in the hope that it will be helpful to others who want to do similar things. I mainly write blogs about LEGO, AWS (Amazon Web Services), WordPress, Deep Learning and Raspberry Pi. At work, I'm working on installing collaborative robots and IoT in factories. I passed the JDLA (Japan Deep Learning Association) Deep Learning for GENERAL in July 2019. If you have any questions, please leave them in the comments at the bottom of the article.

comment

Name, Email, and Website are optional.
and, your Email address will not be published.