LCD randomly stops showing text but backlight is on

So I created a program that waters a plant whenever it gets dry.

This is the code:

#include <LiquidCrystal.h>

int motor=6;
int sensor=A0;
int s;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() 
{
  lcd.begin(16,2);
  pinMode(motor, OUTPUT);
  pinMode(sensor, INPUT);  
  
  lcd.setCursor(0,0);
  lcd.print("Moisture: ");
}

void loop() 
{
  s=analogRead(sensor);
  lcd.setCursor(11,0);
  lcd.print(s);
  delay(1000);
  
  if (171<=s) 
  {
    digitalWrite(motor, LOW);
    lcd.setCursor(0,2);
    lcd.print("motor off");
  }
  else if (s<=170) {
    digitalWrite(motor, HIGH);
    lcd.setCursor(0,2);
    lcd.print("motor on");

  }  
}

The code functions properly in all aspects except the LCD. When executing the code, everything works by code and then I would have the LCD turn off randomly (the back-light is on, only the “wording & numbers” disappear).

I don’t know what may cause the issue but when the words and letters disappear from the LCD the code still function properly the way it should.

Any help would help!

~Yann

[wag absent a circuit diagram]
The motor is powered off the Arduino i/o pin and every now and then the Arduino voltage dies; it might even be resetting.
[/wag]

If you are powering off the i/o pin, don't. The i/o pin should control the base or gate of a suitable bjt or mosfet, which in turn will switch a suitable power source. And there should be a flyback diode.

So what are you using as a 5 V power supply for the Arduino (and the motor)?

I have a 24v ac water pump which is controlled by a 5v relay. I'm still kinda new to electronics and arduino, so thanks for the help.

Hello arduin_ologist,

Whats a fly-back diode, what is it used for, and where would I include it in my system?

Just to let you, I don't really understand what diodes, transistors, (The i/o pin should control the base or gate of a suitable bjt or mosfet) are.

If you could briefly explain what you mean, than I would be very grateful.

~Yann

So what are you using as a 5 V power supply for the Arduino and the relay?


And how are you switching the relay? What relay is it? Web link to the relay you are using?

The arduino is powered from a 5V power supply and the relay gets powered thru it (GND to GND - signal to port #). The motor is connected to NO and the 24V AC power supply is connected to the Vcc.

Here's the relay I am using:

arduinoproject1234:
The arduino is powered from a 5V power supply and the relay gets powered thru it (GND to GND - signal to port #). The motor is connected to NO and the 24V AC power supply is connected to the Vcc.

I doubt the 24V AC power supply is connected to any "Vcc". That would be bad.

So you do not know what an I/O pin on a microprocessor is? Oh well! You need a transistor to switch the current to the relay. Generally easiest to get a relay module which has the necessary components all assembled for you.

arduinoproject1234:
Here's the relay I am using:
Mini 5V DC Power Relay SRD-5VDC-SL-C 5 Pin PCB Type Sale - Banggood USA

Link corrected.

Yes, you need one of these (or similar):

I figured out the problem, it was a loose potentiometer that was hooked up to the LCD.

~Yann