Is there something wrong in code?

I have an ESP32 with an 1602 LCD with backpack (i2c). I alsp have connected an DHT22 temperature and humidity sensor, one relay (Relay is not connected yet), an LDR sensor and å led diode.

All contacts are soldered on to a PCB and all worked fine the same day i tested. LCD shows the temperature and humidity and turns on an off when light in the room changes.

My only issue is that LCD suddenly wan't show the humidity and temperature and shows strange characters and just squares. That happens specially if i disconnect the power and connect it again.

LCD worked fine for 5 hours ago when i turned the computer on and connected LCD, but suddenly it starts to show strange character and squares.

When i try to install an simple "hello world" scetch i get it to work after one push on the reset button after installing, så i suspect it is something in the code taht i can have missed?

EDIT:
One little thing i forgot to mention:

When LCD works and shows humidity and temperature i can see it turns on an off when i load the code. When i see the strange characters and squares on the display nothing happend when i load the code. The display stays on an does not react to anyting.

Her is my code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>;

int lcdColumns = 16;
int lcdRows = 2;


LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);


#define DHTPIN 19     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);


int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


int sensorPin = 34;
int sensorValue = 0;
int relay = 23;
int powerLed = 5;



char auth[] = "****";

char ssid[] = "*****";
char pass[] = "'*****";

void setup()
{
  // Debug console
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
// some code that activates and the same time turns LCD in to "sleep mode"
  lcd.init();
  
  dht.begin();
  pinMode(powerLed, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(sensorPin, INPUT);
  digitalWrite(relay, HIGH);
  digitalWrite(powerLed, HIGH);
 
}

void loop() {
  
// Some code that enables Blynk
  Blynk.run();
  
// some code for writing temperature and humidity values to serial monitor
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  Serial.print("Fuktighet: ");
  Serial.print(hum);
  Serial.print(" %, Temperatur: ");
  Serial.print(temp);
  Serial.println(" Celsius");
  
// code for showing temperature values on LCD display
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);

// code for showing the celcius symbol on LCD display
  lcd.write(0b11011111);
  lcd.setCursor(12, 0);
  lcd.print("C");

// code for showing humidity values on LCD display
  lcd.setCursor(1, 1);
  lcd.print("Hum: ");
  lcd.print(hum);
  lcd.print("%");
  
// some code that makes the value "sensorValue"
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the

// some statements that turns on and off the LCD display regards to light in the room
  if (sensorValue > 100) {
    lcd.noDisplay();
    lcd.noBacklight();
    digitalWrite(powerLed, LOW);
  }
  if(sensorValue < 100) {
    lcd.display();
    lcd.backlight();
    digitalWrite(powerLed, HIGH);
  }

// some code that sends virtual data to the Blynk app
  Blynk.virtualWrite(V5, temp);
  Blynk.virtualWrite(V6, hum);

// Some delay for not blowing the Blynk server up
  delay(3000);
}

Posting your schematic would help a lot here. Also do you read the correct values on the serial monitor?

Jocobes:
Posting your schematic would help a lot here. Also do you read the correct values on the serial monitor?

Yes, Serial Monitor prints right values.

I do not have any chematic for the moment, sorry.

is this the same code that worked? or has the code been changed since it worked? if changed, what changed?

won't be able to help you with a hardware failure, possibly a bad solder joint

Bjerknez:
I do not have any chematic for the moment, sorry.

Get pencil and paper and draw one. Then post a photo of the drawing. See this Simple Image Posting Guide

...R

Don't need to. i just knew it was something with the code and i choosed to follow my gut feeling. When an example code works, then it must be something else than wiring.

The solution was wery simple. I just put LCD settings last in setup and added an delay before LCD startup. I think LCD must been interruptet from spikes from relay startup. Now all works fine. I can plug it inn and out and it just works :slight_smile:

I think i just have to trust more på my own instincts sometimes :slight_smile:

Your analysis is probably correct: the problem IS the hardware and the wiring, and quite a common one at that. The pros call it "inadequate power supply decoupling".

You found a temporary software workaround.

A software workaround is more than good enough for my home projects. I do not want to rebuild an sirquit because i want to change couple of lines in the code.

This is not a recpirator machine for hospitals. It's a little tiny prjojects yhats have cost me 10 dollar in parts and probably å hole lot more in time.

Så this software repair is more than good enough for me, and it was that i was looking for.