Hello,
I know there are some other treads on that topic but i went through all of them and couldn't find any answer to my problem, so I'm opening a new topic.
I'm trying to display the temperature (received from DHT22) into a LCD 16x2 display (I used Arduino Uno board).
The temperature sensor (DHT22) is working fine (I always get the right temperature in the serial console). So I don't think I have any issue here.
When I start my code, it displays as expected the initial screen on the LCD (as defined in setup() function) and then the temperature. But suddenly the LCD displays random characters, see attached, and somehow never stop displaying random characters until i restart the board.
I verified the cabling (to avoid Pins 0 and 1) as well as the code (I'm not using any lcd.println function that would add two additional characters).
Can someone please help me? I don't know how to fix that issue...
Code is below.
thanks and cheers,
Romain
// Librairies
#include <LiquidCrystal.h> // Library code LCD
#include <DHT.h> // Library code DHT22
// Initialize the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Initiliaze the Temp & Hum Sensor (DHT22)
#define dhtPin A0
#define dhtType DHT22 //DHT 22 (AM2302)
DHT dht(dhtPin, dhtType);
// Variables
int contrastPin = 6;
int contrast = 40;
int brightnessPin = 10;
int brightness = 123;
float t; // current temperature
float prev_t = 0; // variable to store previous temperature
void setup() {
Serial.begin(9600);
// set up LCD initial screen
lcd.begin(16, 2);
lcd.print("Good Morning!");
lcd.setCursor(0, 1);
lcd.print("Test Clock");
pinMode(contrastPin, OUTPUT);
pinMode(brightnessPin, OUTPUT);
analogWrite(brightnessPin, brightness);
analogWrite(contrastPin, contrast);
// set up temperature sensor
dht.begin();
}
void loop() {
// Read temperature as Celsius
t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print Temperature in serial Consol
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
delay(1000); // Wait a bit between measurements
if ( t != prev_t){ // if temperature changed
// display temperature on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print(t);
lcd.print(" C");
delay (2000);
}
prev_t = t; // store measured temperature into prev_t variable
delay(1000);
}