Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:remotelab:sut:generalpurpose2:b6 [2020/04/28 19:20] – created pczekalskien:iot-open:remotelab:sut:generalpurpose2:b6 [2020/07/20 12:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +==== 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:
 +<code c>
 +#include <LiquidCrystal_I2C.h>
 +</code>
 +
 +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):
 +
 +<code c>
 +#include <Adafruit_Sensor.h>
 +#include <Adafruit_BMP280.h>
 +</code>
 +=== 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.
 +<note warning>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.</note>
 +
 +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>Note - your readings are in Pa (not hPa) so you're supposed to divide them by 100 before presenting on the screen!</note>
 +
 +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.\\
 +<note important>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.</note>
 +
 +=== Start ===
 +There are no special steps to be performed.
 +
 +=== Steps ===
 +
 +== Step 1 ==
 +Include LCD and sensor driver libraries:
 +<code c>
 +#include <LiquidCrystal_I2C.h>
 +#include <Adafruit_Sensor.h>
 +#include <Adafruit_BMP280.h>
 +</code>
 +
 +== Step 2 ==
 +Instantiate software controler component for the LCD display:
 +<code c>
 +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;
 +</code>
 +
 +== Step 3 ==
 +Declare a buffer for ''sprintf'' function and floating point value (single precision is enough) for storing last air pressure reading:
 +<code c>
 +char buffer [11];
 +float pres;
 +</code>
 +
 +== Step 4 ==
 +Initialize LCD and BMP sensors:
 +<code c>
 +...
 +  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. */
 +...
 +</code>
 +The BMP sensor address is 0x76 and, if not initialised, display error on the LCD screen.
 +
 +== Step 5 ==
 +Read in the loop:
 +<code c>
 +...
 +  pres = bmp.readPressure()/100;
 +...
 +</code>
 +''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:
 +<code c>
 +...
 +  sprintf(buffer,"%4.2f hPa",pres);
 +...
 +  delay(5000);
 +...
 +</code>
 +<note tip>''sprintf'' uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on ''sprintf''. Here <code c>%4.2f</code> 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.</note>
 +
 +=== 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.
  
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