Soil moisture sensor reading - last digital often warped/messed up?

I’m working on a smart greenhouse. Temp/humidity are good, but for some reason my soil moisture sensor displaying on my OLED looks funky - about half the time the last digit is warped and the other half it looks fine. Sometimes there is a 1 number difference from the serial print of the sensor, but mostly it’s spot on.

I have moved it around my display and it’s always just the last number (so it’s not like the display is going bad). I threw in some delays here and there to see if maybe it was updating the OLED too quickly, but no love. Attached are pics of it warped and normal and below is my code…

Any ideas how to fix that?

Soil moisture sensor is a typical cheapy YL-69 sensor and a LM393 attached to it. I have it connected to 5V pin and GND and A0.

#include <U8glib.h>  // U8glib library
#include <dht.h>     // DHT library

#define dht_dpin 2  // Analog pin to which the sensor is connected
dht DHT;
#define pwm 6
const int soilSensorPin=0;

U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // DIN=13, CLK=11, CS=10, DC=9, Reset=8
//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9, 8); // DIN=13, CLK=11, CS=10, DC=9, Reset=8
//U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9, 8); // DIN=13, CLK=11, CS=10, DC=9, Reset=8


void draw(void) 
{
   int sensorValue = analogRead(soilSensorPin);

   u8g.setFont(u8g_font_fub11r);   // select font
   u8g.drawStr(29, 17, "Temp: ");   // put string of display at position X, Y
   u8g.drawStr(29, 40, "Hum: ");
   u8g.drawStr(29, 61, "Soil: ");   // put string of display at position X, Y
   u8g.setPrintPos(85, 17);        // set position
   u8g.print((DHT.temperature * 9) / 5 + 32, 0);  // display temperature from DHT11 in Fahrenheit
   u8g.println("F"); 
   u8g.setPrintPos(85, 40);        // set position
   u8g.print(DHT.humidity, 0);     // display humidity from DHT11
   u8g.println("%");
   u8g.setPrintPos(85, 61);        // set position
   u8g.print(sensorValue);           // display moisture level
  // u8g.println(".");
}

void setup(void) 
{
analogWrite(pwm, 255);
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
delay(2000);
}


void loop(void)
{
   DHT.read11(dht_dpin);  // Read apin on DHT11
   int humi=DHT.humidity;
   int sensorValue = analogRead(soilSensorPin);
   Serial.println(sensorValue);
   delay(500);
   
   u8g.firstPage();  
   do 
{
   draw();
}  while( u8g.nextPage() );

 if(humi <45 )
    { 
      analogWrite(pwm, 0);
      delay(100);
    }
     else if(humi>45)
    {
      analogWrite(pwm, 255);
      delay(100);
    } 
  
  if(sensorValue < 600)
  {
    digitalWrite(3,HIGH);
    delay(1000);
  }
  else 
  {
    digitalWrite(3,LOW);
    delay(1000);
  }
  
 delay(3000);
}

Sometimes there is a 1 number difference from the serial print of the sensor, but mostly it's spot on.

Some jitter in the sensor reading is normal. If you want the serial output and the LCD data to be the same, don't read the sensor twice.

   u8g.print(DHT.humidity, 0);     // display humidity from DHT11
   u8g.println("%");

Does the device properly support printing carriage returns and line feeds? I have my doubts about that. So, sending carriage returns and line feeds to it seems pointless, when that is NOT how you control text position.

I'm not sure if it supports it to be honest. I didn't code the display myself, but it does put the % right after the reading on the same line though.

The moisture one I took out the println though and that is the only one messed up!

I can try changing my code to read it only once a loop... change my line: int sensorValue = analogRead(soilSensorPin); in void draw to to: int sensorValue;

That maybe will fix it...I thought I had tried it and it didn't display anything, but I may be wrong on that!

I can try changing my code to read it only once a loop... change my line: int sensorValue = analogRead(soilSensorPin); in void draw to to: int sensorValue;

No, you need to create a global variable called sensorValue (although that is a terrible name), and value it in loop() and reference it in draw().

The moisture one I took out the println though

After posting the code above?

PaulS: No, you need to create a global variable called sensorValue (although that is a terrible name), and value it in loop() and reference it in draw(). After posting the code above?

Ahh, gotcha. I will try that tonight! Agreed on the terrible name lol. I will clean it up/make it more relevant to my purposes today. I usually try to make sure things are bug-free before I make it my own. I should probably start doing that sooner hah.

It's been excluded like in the above code. I tried it with the . as well, but i don't want any unit after it so I will remove the whole line since I won't be using it.

Thank you for all the help! I will try this tonight when I'm home from work.

Hmm, no love. I am swapping sensors tonight to a capacitive one, so maybe that will fix it.