Hello, how can I change this code with LCD 16x2 to LCD with I2C?

/ includes the LiquidCrystal Library
#include <LiquidCrystal.h>

// includes the LcdBarGraph Library
#include <LcdBarGraph.h>

// Maximum distance we want to ping for (in centimeters).
#define max_distance 200

// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

LcdBarGraph lbg(&lcd, 16, 0, 1); // Creates an LCD Bargraph object.

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
int distance1;
int speakerPin = 8;
void setup()
{
lcd.begin(16,2); // Initializes the interface to the LCD screen

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
// Write a pulse to the HC-SR04 Trigger Pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);

// Determine distance from duration
// Use 343 metres per second as speed of sound
distance= duration*0.034/2;
distance1= 180 - distance ;
// Prints "Distance: " on the first line of the LCD

if (distance1 > 100) {
tone (speakerPin, 1000);

} else {
noTone (speakerPin);

}
lcd.setCursor(0,0);
lcd.print("HEIGHT: ");
lcd.print(distance1);
lcd.print(" cm ");

// Draws bargraph on the second line of the LCD
lcd.setCursor(0,1);
lbg.drawValue(distance1, max_distance);
delay(500);
}

You will have to change the code and the wiring too, so the best place to start is with one of the examples in the LCD I2C library you choose.

LCD I2C tutorials can be found on line (Adafruit, for example).

Please use code tags when posting code.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your sketch code is using two libraries to control the LCD.

  • LiquidCrystal
  • LcdBarGraph

If you switch to an I2C LCD neither of those libraries can be used.
Switching from LiquidCrystal to a library that supports a hd44780 LCD with a PCF8574 backpack is easy. Install the library (I'd recommend using the hd44780 library and its hd44780_I2Cexp i/o class), change the header(s) includeded and then modify the constructor for the lcd object.
The hd44780 library hd44780_I2Cexp i/o class wiki has more on this:

The LcdBarGraph library is hard coded to only work with the LiquidCrytal library.
They could have used a C++ template so it could work with any hd44780 LCD library but they didn't so your choices are to modify their code to use the appropriate class type for their lcd object pointer or switch to another library.
But since all the lcd libraries that enhance LiquidCrystal type libraries, I've seen have used this hard coding approach, there may not be anything out there that can work unmodified.
The modifications to the LcdBarGraph library are quite trivial - just changing the LiquidCrystal class name to the name of the class used by the lcd library you decide to use. So it is probably best to take that approach.

--- bill