UPDATE: The LCD will light up only when the all my components are located in the bottom right of the breadboard. Why am I not able to use the entire breadboard for my circuit?
I am trying to display data from a temperature and humidity sensor on the serial monitor and the liquid crystal display.
I am using :
- Arduino Uno R3
- I2C 16x2 LCD Module
- 3 pin DHT11 sensor
Here is my code :
#include <dht.h> // initialize the digital temperature and humidity sensor library ("DHTlib")
#include <LiquidCrystal_I2C.h> // initialize the liquid crystal display library ("LiquidCrystal I2C")
#include <Wire.h> // intialize the wire library
/* the first parameter is the I2C address
* the second parameter is how many columns are on your screen
* the third parameter is how many rows are on your screen
*/
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT; // create a handle to call functions from the sensor library
#define DHT11_PIN 7 // assign a digital pin number to the sensor
float TempC; // create a variable to store temperature in celsius
float TempF; // create a variable to store temperature in fahrenheit
float Humi; // create a variable to store humidity
void setup()
{
lcd.init(); // initialize liquid crystal display
lcd.backlight(); // turn on the backlight of the lcd
pinMode(DHT11_PIN,INPUT); // configure the pin for the sensor as an input for reading the data
Serial.begin(9600); // set data rate in bits per second for serial data transmission
}
void loop()
{
// collect sensor data
int chk = DHT.read11(DHT11_PIN); // store sensor data in a variable
TempC = DHT.temperature; // store temperature in Celsius
TempF = 9 / 5 * TempC + 32; // store temperature in Fahrenheit
Humi = DHT.humidity; // store relative humidity as a percentage
// display sensor data on LCD
lcd.setCursor(0,0); //tell the screen to write on the top row
lcd.print("T: ");
lcd.print(TempC); // print the temperature in Celsius
// lcd.print(TempF); // print the temperature in Fahrenheit
lcd.print((char)223); // print the degree symbol
lcd.print(" C");
// lcd.print(" F");
lcd.setCursor(0,1); // tell the screen to write on the bottom row
lcd.print("H: ");
lcd.print(Humi); // print the humidity
lcd.print(" %");
// display sensor data on the serial monitor
Serial.print("Temperature = ");
Serial.print(TempC); // print the temperature in Celsius
// Serial.print(TempF); // print the temperature in Fahrenheit
Serial.print("\xc2\xb0"); // print the degree symbol
Serial.println(" C");
// Serial.println(" F");
Serial.print("Humidity = ");
Serial.print(Humi); // print the humidity
Serial.println(" %");
delay(2000); //delay fetching the next sensor values for 2 seconds
}
My jumper wires are connected as follows:
- DHT plus → red plus row
- DHT middle → digital pin 7
- DHT minus → blue minus row
- LCD GND → blue minus row
- LCD VCC → red plus row
- LCD SDA → analog pin A4
- LCD SCL → analog pin A5
- Red plus row → 5V pin
- Blue minus row → GND pin