Help with sensor value displayed on lcd shield.

Hi

New to arduino and this forum,
i would appreciate if i could get some help with having my analog sensor value displayed
to my “keypad lcd shield”

Values are from a soil moisture sensor, and i get the in serial com in computer,

but instead of running around with my plants back and forth to my computer i would like to read the value on my lcd shield
and power the arduino with a battery

so, after spending a couple of evenings of trying to copying others lcd codes and trying to mix them with the code i run
i have to give up before i go all nuts

here is the code i run, and if someone would create the rest of whatever it takes to have value displayed on my lcd i would be very happy

I fill in the first pins i use as i have the older lcd

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

  # Example code for the moisture sensor
  # Editor     : Lauren
  # Date       : 13.01.2012
  # Version    : 1.0
  # Connect the sensor to the A0(Analog 0) pin on the Arduino board
   
  # the sensor value description
  # 0  ~300     dry soil
  # 300~700     humid soil
  # 700~950     in water
*/
 
void setup(){
   
  Serial.begin(9600);
  Serial.print("soil moisture sensor");
  
  delay(2000);
  
   
}
 
void loop(){
   
  Serial.print("Moisture Sensor Value:");
  Serial.println(analogRead(1
  ));  
  delay(1000);
   
}

I might have solved the issue, to get the analog readings to my lcd ,

However a new problem appeared,

I get 1 extra digit on the lcd, when i hold sensor in dry air , it displays correct 1023, but when immerse in wet soil
the number go nuts, instead of printing 312 as the serial com , it prints 3124on lcd

Anyone know what the problem is ?

#include <LiquidCrystal.h>

int SensorPin = A1;
int SensorValue;


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  pinMode(SensorPin, INPUT);
  
  lcd.begin(16, 2);
 
  lcd.clear();
  
  Serial.begin(9600);
  Serial.print("soil moisture sensor");
  
}

void loop() {
  delay(500);
   SensorValue = analogRead(SensorPin);
   
   lcd.setCursor(0,0);
   
   lcd.print("moisture value");
   
   lcd.setCursor(0,1);
   lcd.print(SensorValue);
   
   Serial.print("Moisture Sensor Value:");
  Serial.println(analogRead(1
  ));  
}

Is the problem is that digits from previous readings remain on the display and corrupt current readings?

I mean, if the first reading is "1024" is it followed by a reading that should be "312" but the LCD shows as "3124"?

If so, you can clear the LCD early in the loop() or you can print a line of spaces before printing the reading

 lcd.setCursor(0,1);
lcd.print("       ");
   lcd.print(SensorValue);

``Hi and thanks for your answer,

You was right about the problem, it was so obvious, that i would never had figured it out :smiley:

But i did not get your code to work properly

However i figured that by dividing the 1023 by 10,3 to get the output full span reading between 0-99 (15-99 in reality) i avoided
to have a remaining digit on the screen

Anyone feel free to refine , its my first code i write , so i don’t doubt it could get better.

If anyone know how to reverse the values ,so that higher (ohm) values would mean more moisture ,it would be great,

#include <LiquidCrystal.h>


int SensorPin = A1;
int SensorValue;


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  pinMode(SensorPin, INPUT);
  
  lcd.begin(16, 2);
  

  lcd.print(" kristers soil  ");
  lcd.setCursor(0,1);
  lcd.print("moisture sensor   ");
  
  delay(5000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("High value = dry" );
  lcd.setCursor(0,1);
  lcd.print("Low  value = wet");
   delay(3000);
   lcd.clear();
   lcd.print(" optimum value");
   lcd.setCursor(0,1);
   lcd.print(" > 35------65 <");
   delay(3000);
  lcd.clear();
  
  Serial.begin(9600);
  Serial.print("soil moisture sensor");
  
}

void loop() {
  
   SensorValue = analogRead(SensorPin);
   
   lcd.setCursor(0,0);
   
   lcd.print(" moisture value ");
   
    lcd.setCursor(0,1);
lcd.print("");

lcd.print(SensorValue/10.3);
   
   delay(2000);
   lcd.setCursor(0,1);
   lcd.clear();
   
   lcd.setCursor(0,1);
       
    lcd.print(SensorValue/10.3);
    
    if(analogRead(1)>901 && analogRead(1)< 1023)
 
   {lcd.print(" Not in use");}
   
   if(analogRead(1)>701 && analogRead(1)< 900)
   {lcd.print(" : dead dry");} 
    
    if(analogRead(1)>(601) && analogRead(1)< 700)
   {lcd.print(" : too dry");}
   
   if(analogRead(1)>350 && analogRead(1)< 600)
   {lcd.print(" : perfect");}
   
   if(analogRead(1)>201 && analogRead(1)< 349)
   {lcd.print(" : too wet");}
   
   if(analogRead(1)<200)
   {lcd.print(" : drowning");}
   
   
   
   
   
   
   
   
    
}[/code

You're using delays. Get rid of those in your 'loop', learn to do without delays as soon as possible. You can find out how by playing around with the 'blink without delay' sketch, found in the examples that came with the IDE.

Do you see the display blinking if you take a real good look at it ? It actually does so because that is what you make it do at the moment. Instead of clearing the entire display, print spaces over the position where you like to print your values, then print the new value over that. If you first check that value for changes, and do not (needlessly) erase and rewrite if the value didn't change, you can do perfectly well without any delay (either way) at all and have a very stable display.

thanks for your input, ill look in to that

i can see the display blink, or like a quick flickering

thank you kristerh , I used your code for temp sensor lm35, it works great on my lcd thanks to : forum members

To get rid of any left over digit(s)
lcd.print(SensorValue);
lcd.print(" ");// add one or two spaces.