LCD I2C showing gibberish while connected to arduino uno

#include <hd44780.h>

#include <EEPROM.h>

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

LiquidCrystal_I2C lcd(0x27, 16, 2);

long duration, inches;
int set_val,percentage;
bool state,pump;

void setup() {
Wire.setClock(1000);
lcd.begin();
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF MANUAL");

pinMode(2, OUTPUT);
pinMode(3, INPUT);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(13, OUTPUT);

set_val=EEPROM.read(0);
if(set_val>20)set_val=20 ;
}
void loop() {
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
duration = pulseIn(3, HIGH);
inches = microsecondsToInches(duration);

percentage=(set_val-inches)*110/set_val;

lcd.setCursor(12, 0);
if(percentage<0)percentage=0;
lcd.print(percentage);
lcd.print("% ");

if(percentage<30&digitalRead(11))pump=1;
if(percentage>85)pump=0;
digitalWrite(13,!pump);

lcd.setCursor(5, 1);
if(pump==1)lcd.print("ON ");
else if(pump==0) lcd.print("OFF");

lcd.setCursor(9, 1);
if(!digitalRead(11))lcd.print("MANUAL ");
lcd.print("AUTO ");

if(!digitalRead(10)&!state&digitalRead(11)){
  state=1;
  set_val=inches;
  EEPROM.write(0, set_val);
  }
  
 if(!digitalRead(10)&!state&!digitalRead(11)){
    state=1;
    pump=!pump;
 
  }
  
if(digitalRead(10))state=0;
  

delay(500);

}
long microsecondsToInches(long microseconds) {
return microseconds / 29 / 2;
}

hi @suryakiran00111 welcome to the fora.

Did you successfully run any example code, code you did not write, and get a simple display working on you LCD?

Also, you should post your code properly.One way is to use code tags.

  1. Use the Autoformat tool in the IDE to make your code pretty.

  2. Use the Copy for Forum tool in the IDE to copy your entire sketch.

  3. Open a new post on this thread and paste.

You will see your code is more likely to be examined when you do this.

a7

When you ran the LCD example code, did the LCD work correctly?

When posting code please use code tags?

Here is how to correctly use the hd44780 library for a 1602 LCD with I2C backpack.

The includes:

#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <hd44780.h>    // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>  // i2c expander i/o class header

The constructor:

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

The begin function:

 lcd.begin(16, 2);

Look at the hd44780 library examples for the I2C backpack enabled LCDs.

What are these 2 lines supposed to do? There are syntax mistakes here.

  if (percentage < 30 & digitalRead(11))pump = 1;
   if (percentage > 85)pump = 0;

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