==== IU1: Showing temperature, humidity and dust values on the 2x16 LCD screen ==== === Target group === This hands-on lab guide is intended for the Undergraduate but other target groups may benefit from it, treating it as basics for advanced projects. === Prerequisites === == Liquid Crystal == For this library, you may refer to the [[en:iot-open:remotelab:ume:smartme:b1|B1 exercise]]. == DHT == For this library, you may refer to the [[en:iot-open:remotelab:ume:smartme:b2|B2 exercise]]. == PMS7003 == All the SmartME Network Laboratory Arduino nodes are equipped with a PMS7003 sensor. This sensor allows measuring the air quality by taking into account the PMx concentration. The working principle of such a sensor is based on producing scattering by using a laser to radiate suspending particles in the air, then collecting scattering light to a certain degree, and finally obtaining the curve of scattering light change with time. In the end, the equivalent particle diameter and the number of particles with different diameters per unit volume can be calculated [1]. So, the values of PM1.0, PM2.5, and PM10, will be available for our scopes. In order to physically interface the 10 bus lines of the dust sensor with Arduino Uno, we used the PMS7003 connector [2]. By supplying the adapter at 5V, the particle sensor will operate correctly and the data will be available at RX and TX pins. However, the serial voltage level is 3.3V typical, while Arduino Uno needs 5V. For this reason, to supply the HV (High Voltage) side at 5V and the LV (Low Voltage), we introduced a bi-directional level converter [3] which allows correcting the serial levels of Arduino (5V) and of PMS7003 (3.3V). [1]https://download.kamami.com/p564008-p564008-PMS7003%20series%20data%20manua_English_V2.5.pdf [2]https://kamami.com/others/564553-adapter-from-127mm-pitch-to-254mm-for-pms7003.html [3]https://kamami.com/voltage-level-converters/234535-logic-level-converter-bi-directional.html == Arduino Serial == This function is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port. On Uno pins 0 and 1 are used for communication with the computer. Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board) [4]. Since is not possible to deploy the code by using, at the same time, the default RX, TX pins (0,1) of Arduino Uno, the PMS7003 is connected to pins 9 and 10: by using the library [5], such pins can be used as RX and TX of Arduino Uno, respectively. [4]https://www.arduino.cc/reference/en/language/functions/communication/serial/ [5] https://www.arduino.cc/en/Reference/softwareSerial === Scenario === First, initialize LCD screen with the labels of temperature (“T”), relative humidity (“H”), and dust particles (“PM10”). Then, after the sensor detection, next to the labels, the sensor values will be displayed, every 1 second. Notice that, by enabling the serial print section, data will be available to the serial monitor too. The sensor is able to measure three types of dust particles: PM1.0, PM2.5, and PM10. It may take several seconds to have more precious values about these values. Such values are represented by two bytes: the MSB and the LSB one, as shown in [6] (appendix I). Only the MSB byte can be shown or, alternatively, both values, separated by a point, as for the display management in this example. Various options and combinations are possible for you. For example, the PM2.5 value can be shown on the display, instead of the temperature one. Similarly, for the serial monitor. [6]https://download.kamami.com/p564008-p564008-PMS7003%20series%20data%20manua_English_V2.5.pdf === Result === You should see the values of temperature, relative humidity and dust that are sampled and displayed every 1 second (1000 ms). === Start === There are no special steps to be performed. === Steps === == Step 1 == Include LCD driver library, DHT library and Software Serial library: #include #include #include "DHT.h" == Step 2 == Instantiate the software controller component for the LCD display. Then set up: - 1) the DHTPIN (which refers to the digital pin we use to get the signal); - 2) the DHT sensor type in use (DHT11 or DHT22, it depends on the node of the lab you are using). Set the RX and TX pin of Arduino in the object of the SoftwareSerial library. #define DHTPIN 8 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal lcd(7, 6, 5, 4, 13, 2); // Arduino Uno port RX, TX SoftwareSerial mySerial(9,10); === Step 3 == Initialize display. Initialize the DHT sensor. Sets the data rate in bits per second (baud) for serial data transmissions, both for debugging (115200) and for communication (9600). We suggest to do it in the setup() function: void setup() { lcd.begin(16, 2); // Print Temperature label to the LCD lcd.print("T"); lcd.setCursor(9,0); // Print the Humidity label to the LCD lcd.print("H"); dht.begin(); lcd.setCursor(0,1); lcd.print("PM10"); // for debugging Serial.begin(115200); // Use software serial mySerial.begin(9600); } == Step 4 == Implement loop() to sample and display the values of the temperature, relative humidity and dust on the LCD display, every 1 second: void loop() { float h = dht.readHumidity(); // read temperature in Celsius float t = dht.readTemperature(); // compute heat index in Celsius (isFahreheit = false); float hic = dht.computeHeatIndex(t, h, false); // in Fahrenheit //float f = dht.readTemperature(true); // float hif = dht.computeHeatIndex(f, h); // print Temperature value to the LCD lcd.setCursor(2, 0); lcd.print(t); lcd.setCursor(6,0); lcd.print("C"); // print the Humidity value to the LCD lcd.setCursor(11, 0); lcd.print(h); lcd.setCursor(15,0); lcd.print("%"); int chksum=0; byte pms[32]={0,}; if(mySerial.available()>=32){ for(int j=0; j<32 ; j++){ pms[j]=mySerial.read(); if(j<30) chksum+=pms[j]; } if(pms[30] != (byte)(chksum>>8) || pms[31] != (byte)(chksum) ){ } } delay(1000); lcd.setCursor(5, 1); lcd.print(pms[14]); lcd.print("."); lcd.print(pms[15]); lcd.print("ug/m3 "); } === Result validation === Observe the temperature, relative humidity and dust values shown in the LCD display. The display will refresh values every 1 second. === Platformio.ini === [env:uno] platform = atmelavr board = uno framework = arduino lib_ldf_mode=deep+ lib_compat_mode=strict lib_deps = DHT sensor library@1.3.0 Adafruit Unified Sensor@1.0.3 lib_deps_external = https://github.com/arduino-libraries/LiquidCrystal.git#1.0.7 === IU1.cpp === #include #include #include "DHT.h" #define DHTPIN 8 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal lcd(7, 6, 5, 4, 13, 2); SoftwareSerial mySerial(9,10); // Arduino Uno port RX, TX void setup() { lcd.begin(16, 2); // Print Temperature label to the LCD lcd.print("T"); lcd.setCursor(9,0); // Print the Humidity label to the LCD lcd.print("H"); dht.begin(); lcd.setCursor(0,1); lcd.print("PM10"); // for debuging Serial.begin(115200); // Use software serial mySerial.begin(9600); } void loop() { float h = dht.readHumidity(); // read temperature as Celsius float t = dht.readTemperature(); // read temperature as Fahrenheit // float f = dht.readTemperature(true); // compute heat index in Fahrenheit (the default) // float hif = dht.computeHeatIndex(f, h); // compute heat index in Celsius (isFahreheit = false); float hic = dht.computeHeatIndex(t, h, false); // print Temperature value to the LCD lcd.setCursor(2, 0); lcd.print(t); lcd.setCursor(6,0); lcd.print("C"); // print the Humidity value to the LCD lcd.setCursor(11, 0); lcd.print(h); lcd.setCursor(15,0); lcd.print("%"); int chksum=0; byte pms[32]={0,}; if(mySerial.available()>=32){ for(int j=0; j<32 ; j++){ pms[j]=mySerial.read(); if(j<30) chksum+=pms[j]; } if(pms[30] != (byte)(chksum>>8) || pms[31] != (byte)(chksum) ){ } } delay(1000); lcd.setCursor(5,1); lcd.print(pms[14]); lcd.print("."); lcd.print(pms[15]); lcd.print("ug/m3 "); }