I can not manage to display my data from serial monitor to my lcd i2c

Good morning,

I am desperately trying to work out how to display my code on my LCD screen.
I have attached my written code, it displays to the serial monitor correctly but lcd remains blank.
any offers of help would be greatly accepted.
The project is a soil moisture sensor showing dry and wet soil from analog to a percentage on two lines of the LCD.

type or paste code here

regards
Kaylo

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x3F,16,2);
int sensorPin = A0;
int delayTime = 5000;
int sensorVal;
int min = 158; // soil is dry.
int max = 138; // soil is wet.
float V2 = 0;
int percentage;

void setup() {

pinMode(sensorPin, INPUT);
Serial.begin(9600);
lcd.begin(16,2);

}

void loop() {
delay(delayTime);
sensorVal = analogRead(sensorPin);
percentage = map(sensorVal, min, max, 0, 100);
//Serial.print(" Raw : ");
//Serial.print(sensorVal);

lcd.begin(16,2);
lcd.clear();

lcd.setCursor(0,0);
lcd.print(" Raw :");
lcd.print(sensorVal);

lcd.setCursor(0,1);
lcd.print(" percentage ");
lcd.print(percentage);
lcd.print("%");
delay(delayTime);
lcd.clear();

Serial.print(" Raw : ");
Serial.print(sensorVal);

Serial.print(" | percentage ");
Serial.print(percentage);
Serial.println("%");
delay(delayTime);

// put your main code here, to run repeatedly:

}

Welcome to the forum.

Is your LCD display at I2C address 0x3F ?

Yes, I ran the wire.h software for the correct address

Welcome to the forum

Thank you for trying to use code tags but you posted the code in the wrong place. Please edit your post and move your code to where it says "type or paste code here"

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x3F,16,2); 
int sensorPin = A0;
int delayTime = 5000;
int sensorVal;
int min = 158;    // soil is dry.
int max = 138;   // soil is wet.
float V2 = 0;
int percentage;




void setup() {
 
 pinMode(sensorPin, INPUT);
 Serial.begin(9600);
 lcd.begin(16,2);
 
  
}

void loop() {
  delay(delayTime);
  sensorVal = analogRead(sensorPin);
  percentage = map(sensorVal, min, max, 0, 100);
  //Serial.print(" Raw :   ");
  //Serial.print(sensorVal);

 
  lcd.begin(16,2);
  lcd.clear();
 
 
  lcd.setCursor(0,0);
  lcd.print(" Raw :");
  lcd.print(sensorVal);
  
  lcd.setCursor(0,1);
  lcd.print(" percentage ");
  lcd.print(percentage);
  lcd.print("%");
  delay(delayTime);
  lcd.clear();
  
  
  Serial.print(" Raw :   ");
  Serial.print(sensorVal);

  Serial.print(" | percentage ");
  Serial.print(percentage);
  Serial.println("%");
  delay(delayTime);


  // put your main code here, to run repeatedly:

}

Thank you

Have you tried adjusting the display contrast?

Does the display work with example code from the library? Does it display properly?

1 Like

you only need lcd.begin(16,2) in setup().

There is no need to have it in loop().

Add

lcd.init();
lcd.backlight();

then remove

lcd.begin(16,2);

in the setup ,this will make the script work.

// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------

#include <Wire.h>

// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire

void setup() {
  WIRE.begin();

  Serial.begin(9600);
  while (!Serial)
     delay(10);
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    WIRE.beginTransmission(address);
    error = WIRE.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

The above script will display a list of I2C devices connected to your arduino, making sure that the address is 0x3F. I use a 0x27 for my displays that uses the following:
LiquidCrystal_I2C lcd(0x27,20,4);

1 Like

There are 2 versions of the PCF8574. The PCF8574 and the PCF8574A. The PCF8574 has 0x27 as its derault address. The PCF8574A has 0x3f as its default address.

There are several libraries eith the name LiquidCrystal_I2C. They are not the same. Code written for one library may not work with another. Only trust examle code that comes with the library that you have installed.

Thank you script now works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.