Lesson 96-4: Barometric Pressure, Temperature, and Approximate Altitude Sensor BMP390 with Arduino as a Thermostat
In this lesson, the BMP390 is explained, with wiring shown for I2C and SPI. It is then shown how to display barometric pressure, temperature, and approximate altitude on the serial monitor, on an LCD1602, and on an LCD2004. A full wiring diagram is shown. Library installation is shown, and approximate altitude is measured on the first floor and compared with the altitude on the 25th floor. The code is fully explained and demonstrated using an Arduino UNO and an Arduino MEGA.
This code is designed to use the BMP390 as a thermostat to control a heater or cooler.
437-Lesson 96: Barometric Pressure, Temperature, and Approximate Altitude Sensor BMP390 with LCD
语言: C++
/*
* Lesson 96: Using Precision BMP390 Barometric Pressure and Temperature Sensor
* with Arduino using it as a Thermostat to control a heater or cooler
*
* Watch video instruction on YouTube: https://youtu.be/XevQYG_A5xA
*
* Code updated by Ahmad Shamshiri for Robojax.com
* on Jan 13, 2022 at 17:10 in Ajax, Ontario, Canada
This video is part of the Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
If you found this tutorial helpful, please support me so I can continue creating content like this
and make a donation using PayPal http://robojax.com/L/?id=64
This is a library for the BMP390 temperature & pressure sensor
Designed specifically to work with the Adafruit BMP388 Breakout
----> http://www.adafruit.com/products/3966
These sensors use I2C or SPI to communicate; 2 or 4 pins are required
to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**************************************************************************
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BMP3XX bmp;
int bmpVCCPin = 12;//define pin 12 as VCC for bmp sensor
// all thermostat settings
const int relayPin =8;
const int relayON = HIGH;//
const int relayOFF = LOW; //
int relayState = relayOFF;//initial state of relay
const int TEMPERATURE_UNIT =1;//1=Celsius, 2=Fahrenheit, 3=Kelvin
const float START_TEMPERATURE = 32.0;//unit above
const float STOP_TEMPERATURE = 45.0;//unit above
const int CONTROL_TYPE = 2;// 1= heater, 2=cooler
//do not change the line below
float temperature, pressure, altitude;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Adafruit BMP388 / BMP390 test");
pinMode(bmpVCCPin, OUTPUT);// set pin as output
digitalWrite(bmpVCCPin,HIGH);// always keep it high (5V) for BMB390 module
delay(500);//wait for the bmpVCCPin to turn ON.
pinMode(relayPin, OUTPUT);//pin for relay
digitalWrite(relayPin, relayState);//change initial state of relay
if (!bmp.begin_I2C()) { // hardware I2C mode, can pass in address & alt Wire
Serial.println("Could not find a valid BMP3 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}
void loop() {
if (! bmp.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
readValues();
printTemperature();
Serial.println();
loadControl();
//do extra action if temperature is greater than 89.5
if(temperature >=89.5)
{
///and your code here
}
delay(2000);//wait 2 seconds or make is less or more
}
/*
* loadControl()
* @brief controls the load based
* @param state can be either : relayON or relayOFF
* @return returns none
* Written by Ahmad Shamshiri for robojax.com
* on Jan 15, 2022 at 08:11 in Ajax, Ontario, Canada
*/
void loadControl()
{
//Robojax.com heater/cooler with BMP390 Thermocoupler
// Serial.print("Start: ");
// Serial.print(START_TEMPERATURE);
// Serial.print(" Stop: ");
//Serial.println(STOP_TEMPERATURE);
if(CONTROL_TYPE ==1)
{
if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
{
relayControl(relayON);
}
if(STOP_TEMPERATURE <=temperature)
{
relayControl(relayOFF);
}
}else{
if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
{
relayControl(relayOFF);
}
if(STOP_TEMPERATURE <=temperature)
{
relayControl(relayON);
}
}
//Robojax.com heater/cooler with BMP390 Thermocoupler
}//loadControl()
/*
* relayControl(int state))
* @brief turns the relay ON or OFF
* @param state is "relayON" or "relayOFF" defined at the top of the code
* @return returns none
* Written by Ahmad Shamshiri for robojax.com
* on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
*/
void relayControl(int state)
{
//Robojax.com heater/cooler with BMP390 Thermocoupler
if(state ==relayON)
{
digitalWrite(relayPin, relayON);
Serial.println("Relay ON");
}else{
digitalWrite(relayPin, relayOFF);
Serial.println("Relay OFF");
}
//Robojax.com heater/cooler with BMP390 Thermocoupler
}//relayControl()
/*
* readValues()
* @brief reads the temperature based on the TEMPERATURE_UNIT
* @param average temperature
* @return returns one of the values above
* Written by Ahmad Shamshiri for robojax.com
* on Jan 15, 2022 at 08:02 in Ajax, Ontario, Canada
*/
void readValues()
{
//Robojax.com heater/cooler with BMP390 Thermocoupler
altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);// read altitude
pressure = bmp.pressure / 100.0F; // get pressure in hecto pascal
if(TEMPERATURE_UNIT ==2)
{
temperature = bmp.temperature *9/5 + 32;//convert to Fahrenheit
}else if(TEMPERATURE_UNIT ==3)
{
temperature = bmp.temperature + 273.15;//convert to Kelvin
}else{
temperature = bmp.temperature;// return Celsius
}
//Robojax.com heater/cooler with BMP390 Thermocoupler
}// readValues()
/*
* printTemperature()
* @brief prints temperature on serial monitor
* @param character type
* @param "type" is character
* C = Celsius
* K = Kelvin
* F = Fahrenheit
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on Jan 15, 2022 at 08:09 in Ajax, Ontario, Canada
* www.Robojax.com
*/
void printTemperature()
{
//Robojax.com heater/cooler with BMP390 Thermocoupler
Serial.print(temperature);
printDegree();
if(TEMPERATURE_UNIT ==2)
{
Serial.print("F");
}else if(TEMPERATURE_UNIT ==3)
{
Serial.print("K");
}else{
Serial.print("C");
}
Serial.println();
//Robojax.com heater/cooler with BMP390 Thermocoupler
}//printTemperature()
/*
* @brief prints degree symbol on serial monitor
* @param none
* @return returns nothing
* Written by Ahmad Shamshiri on July 13, 2019
* for Robojax Tutorial Robojax.com
*/
void printDegree()
{
Serial.print("\\xC2");
Serial.print("\\xB0");
}
|||您可能需要的东西
-
全球速卖通从AliExpress购买它s.click.aliexpress.com
-
Banggood从Banggood购买它banggood.com
资源与参考
-
外部BMP390 博世官方产品页面bosch-sensortec.com
-
外部从AliExpress购买它s.click.aliexpress.com
-
外部从Banggood购买它banggood.com
-
外部从亚马逊美国购买。amzn.to
-
外部从加拿大亚马逊购买它amzn.to
-
外部从所有其他亚马逊网站购买。amzn.to
文件📁
没有可用的文件。