I am using an Elegoo Nano with a DHT11 Sensor. I want to connect a LGDehome IIC/I2C/TWI LCD 1602 16x2 Serial Interface Adapter to it. On the display, I connected the black to ground, red to 5 volts, SCL to A5 on the Nano, SDA to A4. As you will see in the code, I also am running the DHT11 to a serial port on a monitor. I can get temperature and humidity readings on the monitor, but nothing shows on the lcd except for the 16 blocks when I have the pot turned up. Below is my code:
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include <Wire.h>
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
delay(1000 );//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
void loop() {
//Start of Program
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("Temperature = ");
// Serial.print(DHT.temperature);
Serial.print((int)round(1.8 * DHT.temperature + 32));
Serial.println(" F ");
delay(500);//Wait 5 seconds before accessing sensor again.
// int chk = DHT.read11(dht_apin);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
delay(1000);
//Fastest should be once every two seconds.
}// end loop()
run an IDC scan to ensure your LCD is found and @ 0x27 (it's the typical default value but can be changed between 0x20 and 0x27 depending on A0,A1,A2 wiring)
This is a very common problem. There are many different I2C LCD displays. one difference is that all of them do not have the same I2C address. Another thing is that not all have the same pin map in terms of the I2C backpack to LCD wiring. You must know and enter into the LiquidCrystal library LCD constructor the right address and pin mapping. It can be a challenge to figure those parameters out.
The hd44780 library, available from the IDE library manager, takes care of finding the address and pin mapping for you. It is the best LCD library available right now.
Thanks for all the replies. I looked online where I purchased the LCD and found a library that worked. Now the problem is that the data on the LCD doesn't have the same readings as my monitor. The temp shows 2.00 and the humidity shows 3%. The monitor shows 36%humidity and the temp is 72 degrees.
Can anyone point out my error? Thanks
code #include <LiquidCrystal_I2C_PCF8574.h> #include <dht.h> #include <Wire.h> #define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
//LiquidCrystal_i2c lcd(12, 11, 5, 4, 3, 2);
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C_PCF8574 lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Serial.begin(9600);
delay(1000 );//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
lcd.init();
delay(500);//Wait 5 seconds before accessing sensor again.
int chk = DHT.read11(dht_apin);
lcd.setCursor(0,0);
lcd.writeStr("Temp: ");
lcd.print(DHT.temperature);
//lcd.writeStr((char)223); //lcd.writeStr("C");
lcd.setCursor(0,1);
lcd.writeStr("Humidity: ");
lcd.print(DHT.humidity);
lcd.writeStr("%");
delay(1000);
//Fastest should be once every two seconds.
I have cleaned up the code and used code tags. You were right in that the temperature code for the lcd was not like the temperature code for the serial monitor. There was a line of code for the lcd that showed the Celsius temperature, not Fahrenheit. As you can see, I have modified the code so the serial monitor and lcd are in sync.
I have tried to include photos showing both the serial monitor and the lcd which the difference indicates that the monitor shows humidity and temperature accurately, but the lcd shows what appears to be just the first digit.
What am I doing wrong to chop the last digits from the lcd readings. Thanks
#include <LiquidCrystal_I2C_PCF8574.h>
#include <dht.h>
#include <Wire.h>
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
//The next two lines are commented out for a prior library that didn't work
//LiquidCrystal_i2c lcd(12, 11, 5, 4, 3, 2);
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C_PCF8574 lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Serial.begin(9600);
delay(1000);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
lcd.init();
}//end "setup()"
void loop() {
//Start of Program
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("Temperature = ");
Serial.print((float)round(1.8 * DHT.temperature + 32));
Serial.println(" F ");
delay(1000);//Wait 1 second before accessing sensor again.
// int chk = DHT.read11(dht_apin); int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.writeStr("Temp: ");
//lcd.print(DHT.temperature);
lcd.print((float)round(1.8 * DHT.temperature + 32));
lcd.print((char)223);
lcd.writeStr("F");
lcd.setCursor(0,1);
lcd.writeStr("Humidity: ");
lcd.print(DHT.humidity);
lcd.writeStr("%");
delay(1000);
}// end loop()