Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use Raspberry Pi to monitor and log environmental parameters

By Usman ali Butt April 25, 2021

In this tutorial, we’ll learn how to interface the four-in-one BME680 pressure, gas, humidity, and temperature sensor with Raspberry Pi (RPi). This will include understanding how to log and retrieve data from a database.

The BME680 can “communicate” with external controllers on either the serial peripheral interface (SPI) and I2C protocols. For this project, we’ll use RPi’s I2C port to connect with the sensor. Much like Arduino, RPi offers predefined libraries, including one for the BME680 and its predecessors.

To begin, install Raspbian OS on your RPi, enabling the I2C interface/port. To do so, use the graphical user interface or GUI — go to Start Menu > Preferences > Raspberry Pi Configuration and select the radio button that’s next to the I2C interface).

You can also do so from the Command Prompt/Shell by entering sudo raspi-config and enabling the I2C port from the blue screen.

Next, install the latest version of Python.

sudo apt update

sudo apt install python3 idle3

Next, install the Python package that’s for the BME680 sensor. Note: if the system says, “pip not found,” you’ll need to install pip first.

sudo pip install bme680

For data logging purposes, we used the “PyMySQL” Python RPI package. We’re going to simultaneously use RPI as a server to host our database.

To install PyMySQL:

python pip install pymysql

The above installation statement for PIP will only work with Python 2.0. For Python 3, you’ll need to use PIP 3 instead. After the packages are installed, it’s necessary to import them into the code.

The code
First, import the BME680 sensor and PyMySQL library. The time library is included so it’s possible to input a delay between statement executions if/when necessary.

The connection to the database must be established next. Since we’re using RPI as a server, the localhost is passed as a server parameter.

Note:

  • The username is the database’s administrator name
  • The password is the database’s password (if set)
  • The name of the database (DB) is “dbname”

It’s necessary to ensure these names are correct or replaced if possible. The sensor is initialized with this statement: bme680.BME680() statement.

Next, the sensor’s individual parameters sampling rate must be defined. 

For the: 

  • Humidity — 2X
  • Pressure — 4X
  • Temperature — 8X
  • Filter all of the background noises

The body of the program follows. The loop continuously reads data from the sensor, filters it, and passes it onto the database for storage.

  • The statement, get_sensor_data(), checks for the presence of any data. If found “true,” the data is read and stored in the output variable.
  • The sensor.data.temperature statement provides the temperature reading. This reading is placed in the output string variable.
  • Where in {0:.2f}, 0 means the first placeholder, and .2f means the floating-point number (with two decimal places).
  • The rest 2{1 and 2} parameters relate to the pressure and humidity.
  • The heat_stable statement enables the gas reading. If the data is fairly stable (and not fluctuating drastically), then it prints the output variable — which stores the temperature, humidity, and pressure values with the gas readings.
  • A delay of three minutes is included to stabilize the sensor.

To log data in the database, it’s necessary to insert data in the DB. SQL query statement. The “%s” serve as placeholders for each of the values. Finally, commit the data to update the database and then close the connection.

The SQL insert query enlists the table name as LOGTABLE. However, you must first create this table to log data in it. Let’s assume we create a database name of “MasterDB,” with a table inside it called, “LOGTABLE.”

The SQL statements would be:

CREATE DATABASE MasterDB; #Creates a database named MasterDB

USE MasterDB;                                       #Set’s MasterDB in use- Every query now goes to this DB

CREATE TABLE MasterDB (   #Creates table with coulombs
    Temperature floating,
    Pressure    floating,
    Humidity    floating,
    Gas         floating
);

The SQL statements are general MySQL statements. To run them in the Python code, first put them in the appropriate Python MySQL binder — just like we did with PyMySQL in the above code (INSERT statement).

To extract data from the database, the Python statement for the PyMySQL binder is:

Select the coulombs (for the temperature, pressure, humidity, gas) from the MasterDB. Get one record and print it on the console.

Any Python RPI IDE can be used for this project. We used Thonny.

Circuit diagram

Where to purchase the parts?

  • Raspberry Pi: Mouser
  • BME680 sensor: Mouser

You may also like:


  • How does LoRa modulation enable long-range communication?

  • What is the role of embedded software in electric vehicles?

  • What are the different types of temperature sensors and their…

  • How to use the BME680 multi-purpose sensor with Arduino

  • Interfacing Raspberry Pi with Arduino

  • Getting started with Raspberry Pi

Filed Under: Electronic Projects, Raspberry pi
Tagged With: python, raspberrypi
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Question on options of set_clock_group
  • Egs002
  • CMOS ring oscillator
  • BA4116 reference resistance
  • is there vay for credit card size phone charger?

RSS Electro-Tech-Online.com Discussions

  • Fixing board, Easy question HEX SCHMITT
  • Can I make two inputs from one??
  • Display TFT ST7789 (OshonSoft Basic).
  • Home Smoke detectors are all Beeping Batteries are not dead.???
  • The Analog Gods Hate Me

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How to monitor temperature and humidity on a TFT display with graphics
  • Tria modules integrate edge AI processing with multi-core processors
  • pSemi introduces RF switch with 52 dBm PMAX,PEAK and 90-dBm IIP3 linearity
  • XP Power launches 1.3 kW power supply with 58.9 W/cm³ density
  • How to enable Wi-Fi provisioning in ESP32-based IoT products

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe