Exercise 31 - Viewing sensor data
1. INTRODUCTION
LCD screen can be used as serial monitor to monitoring sensor measures and general data in this exercise we are going to connect a force resistor CONNECTING_A_FORCE_SENSITIVE_RESISTOR and a Humidity and temperature sensor CONNECTING_A_TEMPERATURE_AND_RELATIVE_HUMIDITY_SENSOR.
You must remember to install libraries for LCD_I2C screen and DHT11 sensor.
2. HOW TO CONNECT?
Materials:
· Arduino UNO board
· Protoboard
· Some wire Jumpers
· LCD (I2C) screen
· Force sensitive resistor
· Temperature and relative humidity sensor
· 10K ohms resistor
Connections in this exercise are identical to CONNECTING_A_LCD_(I2C)_SCREEN, CONNECTING_A_FORCE_SENSITIVE_RESISTOR and CONNECTING_A_TEMPERATURE_AND_RELATIVE_HUMIDITY_SENSOR. You can go to each one to look it in more detail. You can find also the schematics on these exercises.
EXERCISE_A (LCD_Force) |
![]()
|
EXERCISE_B (LCD_Temperature_Humidity) |
![]()
|
3. PROGRAMMING
EXERCISE_A (LCD_Force)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int sensor = 0;
int value = 0;
void setup()
{
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("FORCE SENSOR");
delay(1000);
}
You can change in this code in loop. It is very similar to CONNECTING_A_FORCE_SENSITIVE_RESISTOR but now, the value of force sensor is read and then is displayed on LCD display.
void loop()
{
value = analogRead(sensor);
lcd.setCursor(0, 1);
lcd.print(" ");//Print 16 spaces to Erase row
lcd.setCursor(0, 1);
lcd.print("FORCE: ");
lcd.print(value);
delay(1000);
}
EXERCISE_B (LCD_Temperature_Humidity)
In this exercise DHT library is added to code to use humidity and temperature sensor. The setup of the program, it is very similar to and CONNECTING_A_TEMPERATURE_AND_RELATIVE_HUMIDITY_SENSOR but in this exercise, the LCD display is written in its two rows.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 4 // Arduino pin4, connected to pin2 in sensor
#define DHTTYPE DHT11 // DHT 11
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
int sensor = 0;
int value = 0;
void setup()
{
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("WEATHER SENSOR");
dht.begin();
delay(1000);
}
void loop()
{
// Wait to do meassure
delay(2000);
// Each read of sensor spends 250 milliseconds
// Read the humidity value
float h = dht.readHumidity();
// Read the temperature value in celsius ( if you want Fahrenheit put TRUE into brackets)
float t = dht.readTemperature();
// Validate if the sensor is sending meassures
if (isnan(h) || isnan(t)) {
Serial.println("Sensor read failed!");
return;
}
Up to this point, the exercise equals to the previous exercise. Now it is necessary to write in the two rows of LCD display because this, it is important to set the cursor at the start of the row in which you want to write.
To update the text written you should write first 16 spaces (one per column) and then, you can write the text that you want to appear on the display.
lcd.setCursor(0, 0);
lcd.print(" ");//Print 16 spaces to Erase row
lcd.setCursor(0, 0);
lcd.print("TEMPERAT: ");
lcd.print(t,0);
lcd.print("*C");
lcd.setCursor(0, 1);
lcd.print(" ");//Print 16 spaces to Erase row
lcd.setCursor(0, 1);
lcd.print("HUMIDITY.: ");
lcd.print(h,0);
lcd.print("%");
delay(1000);
}