I have seen quite a few tutorials on this project I'm working on. Just trying to dip my toes into the waters here. I've connected Uno Rev3 to a DHT22 temperature/humidity sensor and a Waveshare 1602 I2C LCD display.
I can't quite get it working. I have found a few examples of this project and tried working with all of that posted code and those libraries, but I think I am having trouble with the LCD I am using: it seems like it only works with the specific waveshare library, but I can't get anything working, and I wouldn't think that's the case anyway. I've seen data sent to the serial monitor so the sensor works, I can print to the LCD, but I can't get all the pieces to come together.
I've tried the code below (nothing sent to LCD, but the address is correct).
#include <LiquidCrystal_I2C.h>
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-humidity-sensor-lcd
*/
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
LiquidCrystal_I2C lcd(0x3E, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
}
void loop()
{
delay(2000); // wait a few seconds between measurements
float humi = dht.readHumidity(); // read humidity
float tempC = dht.readTemperature(); // read temperature
lcd.clear();
// check if any reads failed
if (isnan(humi) || isnan(tempC)) {
lcd.setCursor(0, 0);
lcd.print("Failed");
} else {
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temp: ");
lcd.print(tempC); // print the temperature
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Humi: ");
lcd.print(humi); // print the humidity
lcd.print("%");
}
}
Thanks for any help here, I've been working on this for days without success!
Thanks for the reply Ron. Changed the address to reflect 0x27. I changed to 0x3E as I ran a scanner and found a device at that address, thinking it was the LCD display. But I've changed it back to 0x27.
The startup code you posted is the same as I have in mine, unless I'm not seeing something.
post the code that successfully wrote to the LCD. Was it using address 0x27 or 0x3E? Did you have other things connected to the wire bus while running the scan?
Nothing else connected to the bus while running scan, just ran it again and came back 0x3E. I haven't been able to send text to the screen via the address... the code below is the way I've gotten text to screen.
#include <dht.h>
#include <Wire.h>
#include "Waveshare_LCD1602.h"
Waveshare_LCD1602 lcd(16,2); //16 characters and 2 lines of show
int r,g,b,t=0;
void setup() {
lcd.init();
lcd.setCursor(0,0);
lcd.send_string("Hello, hello");
lcd.setCursor(0,1);
lcd.send_string("test, test");
}
void loop() {
delay(150);
}
Here are the results I got running your original sketch with the address changed to 0x27 and the rows and columns changed to match the display. No other changes were made.
Waveshare makes it clear the correct address is 0x27 and cannot be changed. I gave you a suggestion earlier, if you choose not to try what I suggested then best of luck with your problem. The last code you posted does not even have an address.
I think there's something specific about the Waveshare LCD I have. I will try again when I get home later. This here is the display I am using.
Is it possible that I need to use the Waveshare library, which doesn't have all the functions I need? I've tried using the waveshare library but it won't let me print a float value. I hope this question even makes sense.
I tried your suggestion, still couldn't get it to work. I have no reason to not trust the advice I'm getting here. The code posted to write a test message to the display still sent text to the display without an address...
As I showed, your original sketch works just fine with a bog standard PCF8574 backpack.
Your display doesn't have that. It has a completely different display controller IC, one that has I2C built into it.
Why you would you expect an example written for a PCF8574 backpack to work with a completely different piece of hardware is a mystery to me.
And just because the library does things differently doesn't mean you can't make it do what you want. Investigate snprintf. Or use Waveshare's library as a basis for creating a fork of the LiquidCrystal_I2C library that does work with your controller.
I think this is the answer. I was previously operating under the assumption that all I2C were created equal. I will investigate snprintf and how creating a fork works for what I've got here. Thanks. Will post what I end up doing if I can figure it all out for any future generation to use this as a reference.
The LCD you have uses a AiP31068 chip.
This uses a very different communication interface over i2c than when a PCF8574 is being used to control a HD44780 chip.
You need a library that supports using the AiP31068 chip.
The hd44780 library includes a an i/o class that supports this interface.
It is the hd44780_I2Clcd i/o class.