I'm new to Arduino programming and am experiencing the following issue.
Whenever my code is printing its serial data from an NTC sensor (which works fine on serial print) on a 20x4 HITACHI lcd screen, the screen flickers. I would like some insight on what could be wrong with my code.
My hardware setup works as it is (other code with static text is displayed wthout flickering and such) but I have a different setup as i don't use resistors and a potentiometer for the lcd. The setup is as followed.
Using an Arduino Mega btw
VSS - 5V
VDD - GND
VO - Analog pin 3 (I replaced the potentiometer with an analogpin and use analogWrite to give the lcd contrast
RS - TX0 / Digital 1
RW - GND
E - analog 2
D4 - digital 4
D5 - digital 5
D6 - digital 6
D7 - digital 7
Anode - not connected
Cathode - GND (parallel with VDD)
my code is as followed
#include <LiquidCrystal.h>
int Contrast=20;
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int TempPin = A0;
int Vo;
void setup()
{
analogWrite(3,Contrast); //This code replaces the potentiometer by connecting Vo to an Analog pin
{
lcd.begin(20, 4);
}
}
void loop()
{
Vo = analogRead(TempPin);
lcd.setCursor(0,0);
lcd.print("Temp");
lcd.print(Vo);
}
I'm uncertain if the command analogWrite(3, contrast) is causing the flicker as it is present in void loop.
The analogwrite command is in setup and cannot possibly cause flicker. It is usual to put some sort of timer control in the loop, the simplest being an appropriate delay. As things are, the main thing controlling the loop is probably the time taken to write on the display.
The
lcd.setCursor(0,0);
lcd.print("Temp");
need only be printed once and is better in the setup, hence perhaps
Setting lcd.print("temp") in setup and delaying loop by 1000ms didn't help. It only made the flickering more apparant every 1000ms. I've attached a video to demonstrate the flickering.
If what I see is truly representative of what is going on, I wouldn't call that "flickering", I would call it "intermittent operation", and I doubt that it has anything to do with code. It could have quite a lot to do with power supply.
The gif was taken in the dark but when the backlight is off, it's still updating the data on the screen, when it updates the lcd backlight flickers on and off and sometimes just goes on for a bit
The GIF shows a flash about once a second, so I cannot see a connection with a 500 delay, but at least it appears to be regular. I don't see any text on it when it flashes. I would guess that no amount of coding will fix this, it is all to do with wiring and power. If you can see text with no backlight, I think that would prove I am right, and most of the wiring is kosher. If you can't see any text, it does not prove I'm wrong, and it doesn't prove your code is wrong either.
The problem could be in the sensor. You could test this with dummy values.
Please don't tell us you are running this off a 9v PP3 battery.
BTW, I see you have a LCD pin connected to hardware serial, and the whole line LiquidCrystal lcd(1, 2, 4, 5, 6, 7); is probably suss. While you are not actually using serial, using TX0 cannot possibly be a good idea. It might even be indicative of something.