LCD prints gibberish When Using Analog Read

I am using an arduino UNO and the parts that come with the starter kit. I am using a USB connection to code from my PC. I also have a capacitive soil moisture sensor that I would like to have read the moisture and display the value as a percentage on the LCD. The sensor uses an analog input that seems to be where the LCD stops working. I can use the Serial Monitor to see all the values I want displayed on the LCD, but when I print to the lcd it fails.

I have included a diagram of my circuit as an attachment.

My code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int dryVal0 = 675;                                            //dry value variable to sensor and climate
const int wetVal0 = 397;                                            //wet value same sensativities  
int soilMoistureVal0 = 0;                                           //analog value from sensor  
int soilMoistureP0 = 0;                                            //calculate moisture percentage 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);                                               //initialize serial monitor
  lcd.begin(16, 2);                                                 //initialize lcd at 16 x 2
}

void loop() {
  // put your main code here, to run repeatedly:
  soilMoistureVal0 = analogRead(A0);                                //read soil moisture senosr 0
  Serial.println(soilMoistureVal0);                                 //print to serial monitor
  soilMoistureP0 = map(soilMoistureVal0, dryVal0, wetVal0, 0, 100);  //map moisuture value between 0, 100%
  
  if(soilMoistureP0 > 100) {                                        //edge case: over 100%
    Serial.println("100 %");
    lcd.setCursor(0, 0);
    lcd.print("Soil Moisture");
    lcd.setCursor(0, 1);
    lcd.print("100 %");
    delay(250);
    lcd.clear();                                                    //print 100% and clear
  }

  else if(soilMoistureP0 < 0) {                                //edge case: below 0%
    Serial.println("0 %");
    lcd.setCursor(0, 0);
    lcd.print("Soil Moisture");
    lcd.setCursor(0, 1);
    lcd.print("0 %");
    delay(250);
    lcd.clear();                                                    //print 0% and clear
  }

  else if(soilMoistureP0 >= 0 && soilMoistureP0 <= 100) {             //correct percentage
    Serial.print(soilMoistureP0);
    Serial.println("%");
    lcd.setCursor(0, 0);
    lcd.print("Soil Moisture");
    lcd.setCursor(0, 1);
    lcd.print(soilMoistureP0);
    lcd.print(" %");
    delay(250);
    lcd.clear();
  }
}

You are clearing your LCD about 1/4 second after you write the information to it.

Don

I have changed the delay to 3 seconds. As I said, I am able to print to the LCD itself without any problem. However, once I begin the serial monitor or read from the moisture sensor, any attempt to print that data to the LCD results in ASCII gibberish.

Annotation 2020-06-21 185135.png

Sorry, I haven't figured out your problem, and this is not critical but just a little hint to make things easier to set the best contrast.

Remove the red wire from the potentiometer to +5 V, that is a mistake propagated amongst the amateur designs using the display. :astonished: If it is a 10k potentiometer connect both ends of the potentiometer to ground.

I believe you are trying to display an integer value that you have obtained from your sensor.

When you send the data to the serial monitor the 'serial.print' routine detects that you are sending it an integer and it converts that integer into its equivalent ASCII code. It then sends those ASCII codes to the display.

The lcd.print routine does not do any such conversion. It takes what you give it, assumes that it consists of ASCII codes, and sends it directly to the display.

You are going to have to convert the data before you try to display it on the LCD.

Don

Disclaimer- If I really messed this up Bill (bperrybap) will be along to correct me.