B6: Presenting air pressure on the LCD

In this scenario, you will present absolute air pressure reading, on the LCD screen.

Target group

Beginners

Prerequisites

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>

Scenario

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.

Do not try to read air pressure too frequent. Once every 5s is quite enough both for information and safe enough to not let your readings race with the communication protocol.

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.

Note - your readings are in Pa (not hPa) so you're supposed to divide them by 100 before presenting on the screen!

You will use sprintf function to format string and convert from float to string.

Result

You should be able to see air pressure readings on the LCD display using the video stream. Some fluctuations are natural.

If the fan connected to the air chamber is on, air pressure measured may be higher than the ambient air pressure observed and higher disturbances will be observed.

Start

There are no special steps to be performed.

Steps

Step 1

Include LCD and sensor driver libraries:

#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
Step 2

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;
Step 3

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;
Step 4

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.

Step 5

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.

Result validation

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.

en/iot-open/remotelab/sut/generalpurpose2/b6.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0