In this scenario, you will present absolute air pressure reading, on the LCD screen.
Beginners
You need to know, how to print and position text on the LCD display. Use LCD I2C library to control it:
#include <LiquidCrystal_I2C.h>
The air pressure sensor is Bosch BMP280 sensor, connected to the I2C bus, shared with LCD display. To read data you may use a dedicated library (libraries):
#include <Adafruit_Sensor.h> #include <Adafruit_BMP280.h>
Once you initialise sensor and LCD display, read sensor data (air pressure) and then display them in the loop. Give each loop execution some 5-10s delay.
The first line of the LCD should display: “Air pressure is:“
The second line should provide temperature within the ”NNNN.NN hPa” form, in hPa.
You will use sprintf
function to format string and convert from float to string.
You should be able to see air pressure readings on the LCD display using the video stream. Some fluctuations are natural.
There are no special steps to be performed.
Include LCD and sensor driver libraries:
#include <LiquidCrystal_I2C.h> #include <Adafruit_Sensor.h> #include <Adafruit_BMP280.h>
Instantiate software controler component for the LCD display:
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x3F for nodes 1 through 5, 8 and 9 for a 20 chars and 4 line display //LiquidCrystal_I2C lcd(0x27,20,4); // for nodes 10 and 11 only! Adafruit_BMP280 bmp;
Declare a buffer for sprintf
function and floating point value (single precision is enough) for storing last air pressure reading:
char buffer [11]; float pres;
Initialize LCD and BMP sensors:
... lcd.init(D2,D1); // initialize the lcd lcd.backlight(); lcd.home(); ... if(!bmp.begin(0x76)) { lcd.print("Bosch Sensor Error!"); delay(1000); }; lcd.home(); bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ Adafruit_BMP280::FILTER_X16, /* Filtering. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ ...
The BMP sensor address is 0x76 and, if not initialised, display error on the LCD screen.
Read in the loop:
... pres = bmp.readPressure()/100; ...
bmp.readPressure()
function returns air pressure in Pa. You must convert it hPa, dividing by 100.
To convert float into the string with formatting use sprintf
function:
... sprintf(buffer,"%4.2f hPa",pres); ... delay(5000); ...
sprintf
uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on sprintf
. Here %4.2f
means: having float number render it to string using four digits before decimal point and two after it. Air pressure readings should be somewhere between 890 and 1060 hPa.
delay(time)
is measured in milliseconds.
Observe air pressure readings on the LCD. Note - small oscillations are natural - you can implement moving average (i.e. of 10 subsequent reads, FIFO model) to “flatten” readings.