Why does the lcd show the correct printed values only when i open serial monitor

Guys i am making a gas detector alarm with arduino, i have interfaced a 16X2 lcd module and a gas sensor to the arduino. On connecting the components according to circuit diagram, the lcd just shows some random special symbols like “%@??&**” , later on opening serial monitor it then shows proper characters as given in code and functions normally.Why is this happening is it possible to correct this error, is it because coding error or any settings which has to be modified. I want a solution to this problem urgently any valuable suggestions would help guys… I have uploaded my code below…
.I have to shrinkify this project to an atmega328p and power it separately via 9v battery, i am worried that even on powering arduino separately it might not display… PlZZ help guys…
:confused: :confused:
Thank you,

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 5, 4, 3, 2);

int potPin = A4;
int ledred= 9;
int ledgreen= 10;

int potValue = 0;

int buzzer = 6;

void setup() {

lcd.begin(16, 2); // lcd rows and columns

lcd.print(“GAS_SENSOR”);

pinMode(6, OUTPUT);

}

void loop() {

potValue = analogRead(potPin);

digitalWrite(10, HIGH);

lcd.setCursor(0, 1);

lcd.print("Value = ");

lcd.print(potValue);

delay(1000);

lcd.print(" ");

delay(1);

if (potValue>15)

{

digitalWrite(6,HIGH);

delay(1000);
digitalWrite(10,LOW);
digitalWrite(9,HIGH);

}

}

atmega328pgas.ino (620 Bytes)

No idea what goes wrong. Try a short delay at the end of setup().

Further

Your code is missing pinMode statements for the leds.

Why do you declare pins in the beginning of your code but don't use them in the code (e.g. ledred).

PS
Opening serial monitor resets the Arduino.

These lines should be in setup(), because you never change them.
lcd.setCursor(0, 1);
lcd.print("Value = ");

Then write the actual pot value in loop() to the position after those words.
lcd.setCursor(8, 1);
lcd.print(potValue);

You might have to write four spaces first to that position, to overwrite the previous reading.

lcd.print(" ");
delay(1);
Not sure what they are doing there.
Try a single delay(250); // update four times per second

Try adding an lcd.clear(); right after lcd.begin(16, 2);
Leo..

Thnak you so much i have completed y project succesfully, ur suggestions turned out to be really helpful and gave me an insight into better code for the final uploaded code.. :slight_smile: :slight_smile: