This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:remotelab:ume:smartme:b2 [2019/10/28 17:17] – tomykalm | en:iot-open:remotelab:ume:smartme:b2 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== IB2: Presenting temperature and humidity values on the LCD ==== | ||
| + | |||
| + | === Target group === | ||
| + | |||
| + | This hands-on lab guide is intended for the Beginners 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: | ||
| + | |||
| + | === DHT sensors === | ||
| + | The nodes of SmartME Network Laboratory are equipped with different versions of DHT sensors: DHT11 and DHT22. | ||
| + | The sensors look a bit similar and have the same pinout, but have different characteristics. | ||
| + | Below you can find the DHT technical specifications: | ||
| + | |||
| + | == DHT11 == | ||
| + | * Ultra-low-cost | ||
| + | * 3 to 5V power and I/O | ||
| + | * 2.5mA max current use during conversion (while requesting data) | ||
| + | * Good for 20-80% humidity readings with 5% accuracy | ||
| + | * Good for 0-50°C temperature readings ±2°C accuracy | ||
| + | * No more than 1 Hz sampling rate (once every second) | ||
| + | * Body size 15.5mm x 12mm x 5.5mm | ||
| + | * 4 pins with 0.1" spacing | ||
| + | |||
| + | == DHT22 == | ||
| + | |||
| + | * Low cost | ||
| + | * 3 to 5V power and I/O | ||
| + | * 2.5mA max current use during conversion (while requesting data) | ||
| + | * Good for 0-100% humidity readings with 2-5% accuracy | ||
| + | * Good for -40 to 80°C temperature readings ±0.5°C accuracy | ||
| + | * No more than 0.5 Hz sampling rate (once every 2 seconds) | ||
| + | * Body size 15.1mm x 25mm x 7.7mm | ||
| + | * 4 pins with 0.1" spacing | ||
| + | |||
| + | DHT sensors are able to detect temperature and relative humidity. | ||
| + | Relative humidity consists of the amount of water vapour in air vs. the saturation point of water vapour in the air. | ||
| + | At the saturation point, water vapour starts to condense and accumulate on surfaces forming dew. The saturation point changes with air temperature. | ||
| + | Cold air can hold less water vapour before it becomes saturated, and hot air can hold more water vapour before it becomes saturated. | ||
| + | |||
| + | Temperature and relative humidity can be used to determine the apparent temperature or the human perceived equivalent temperature, | ||
| + | |||
| + | === Scenario === | ||
| + | |||
| + | First, initialize LCD screen with the labels of temperature and relative humidity. | ||
| + | Then, after the sensor detection, next to the labels, the sensor values will be displayed, one per line, every 3 seconds. Note, the function readTemperature() reads temperature as Celsius (the default). If you want to read the temperature as Farenight, you have to set the true value as a parameter of the signature in the readTemperature(true) function. The relative humidity is expressed as a percentage. That means that at 100% RH the condensation occurs, while at 0% RH the air is completely dry. | ||
| + | More, but not used in the proposed exercise, it is possible to compute the heat index in Fahrenheit (the default) by using the function computeHeatIndex(temp_value_farenight, | ||
| + | Result | ||
| + | You should see the values of temperature and relative humidity, that is sampled and displayed every 3 seconds (3000 ms). | ||
| + | Start | ||
| + | There are no special steps to be performed. | ||
| + | |||
| + | === Steps === | ||
| + | |||
| + | == Step 1 == | ||
| + | Include LCD driver library and DHT library: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include " | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Step 2 == | ||
| + | Instantiate the software controller component for the LCD display. Then set up: | ||
| + | - the DHTPIN (which refers to the digital pin we use to get the signal); | ||
| + | - the DHT sensor type in use (DHT11 or DHT22, it depends on the node of the lab you are using). | ||
| + | Then initialize the DHT sensor. | ||
| + | |||
| + | <code c> | ||
| + | // initialize the library with the numbers of the interface pins | ||
| + | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| + | #define DHTPIN 8 // what pin we are connected to | ||
| + | // Uncomment whatever type you're using! | ||
| + | #define DHTTYPE DHT11 | ||
| + | //#define DHTTYPE DHT22 | ||
| + | // Initialize DHT sensor | ||
| + | DHT dht(DHTPIN, DHTTYPE); | ||
| + | </ | ||
| + | |||
| + | == Step 3 == | ||
| + | |||
| + | |||
| + | Initialize display and Initialize the DHT sensor - we suggest to do it in the setup() function: | ||
| + | |||
| + | <code c> | ||
| + | void setup() { | ||
| + | // set up the LCD's number of columns and rows: | ||
| + | lcd.begin(16, | ||
| + | // Print Temperature label to the LCD. | ||
| + | lcd.print(" | ||
| + | lcd.setCursor(0, | ||
| + | // Print the Humidity label to the LCD. | ||
| + | lcd.print(" | ||
| + | dht.begin(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Step 4 == | ||
| + | |||
| + | Implement loop() to sample and display the values of temperature and relative humidity on the LCD display, every 3 seconds: | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | void loop() { | ||
| + | // Wait three seconds between measurements. | ||
| + | delay(3000); | ||
| + | |||
| + | // read relative humidity | ||
| + | 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, | ||
| + | // compute heat index in Celsius (isFahreheit = false) | ||
| + | float hic = dht.computeHeatIndex(t, | ||
| + | // print Temperature value to the LCD | ||
| + | lcd.setCursor(14, | ||
| + | lcd.print(t); | ||
| + | // print the Humidity value to the LCD | ||
| + | lcd.setCursor(14, | ||
| + | lcd.print(h); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Result validation === | ||
| + | Observe the temperature and relative humidity values shown in the LCD display. The display will refresh values every 3 seconds. | ||
| + | |||
| + | === 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 | ||
| + | | ||
| + | |||
| + | lib_deps_external = | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === IB2.cpp === | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | // initialize the library with the numbers of the interface pins | ||
| + | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| + | |||
| + | #define DHTPIN 8 // what pin we are connected to | ||
| + | // uncomment whatever type you're using | ||
| + | #define DHTTYPE DHT22 | ||
| + | //#define DHTTYPE DHT22 | ||
| + | |||
| + | // initialize DHT sensor | ||
| + | DHT dht(DHTPIN, DHTTYPE); | ||
| + | |||
| + | void setup() { | ||
| + | // set up the LCD's number of columns and rows | ||
| + | | ||
| + | // Print Temperature label to the LCD | ||
| + | | ||
| + | | ||
| + | // Print the Humidity label to the LCD | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // wait three seconds between measurements | ||
| + | | ||
| + | // read humidity | ||
| + | float h = dht.readHumidity(); | ||
| + | // read temperature as Celsius | ||
| + | float t = dht.readTemperature(); | ||
| + | // read temperature as Fahrenheit | ||
| + | // | ||
| + | |||
| + | // compute heat index in Fahrenheit (the default) | ||
| + | // | ||
| + | // compute heat index in Celsius (isFahreheit = false); | ||
| + | float hic = dht.computeHeatIndex(t, | ||
| + | |||
| + | // print Temperature value to the LCD | ||
| + | | ||
| + | | ||
| + | // print the Humidity value to the LCD | ||
| + | | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||