// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>     // Include LiquidCrystal_I2C library 

/* https://simple-circuit.com/arduino-i2c-lcd-pcf8574/ */
/* Configure LiquidCrystal_I2C library with 0x27 address, 16 columns and 2 rows */
LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  Serial.println("\nI2C Scanner");

  lcd.init();                        // Initialize I2C LCD module
  lcd.backlight();                   // Turn backlight ON
}

void loop()
{
  byte error, address;
  int nDevices = 0;
  Serial.println("Scanning...");

  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)
    {
      lcd.setCursor(0, 0);               // Go to column 0, row 0
      lcd.print("Found I2C device");
      lcd.setCursor(0, 1);               // Go to column 0, row 1
      lcd.print("at address:");
      lcd.setCursor(13, 1);
      lcd.print(address, HEX);

      Serial.print("I2C device found at address 0x");
      Serial.print(address, HEX);
      Serial.println("  !");
      nDevices++;
    }

    else if (error == 4)
    {
      Serial.print("Unknown error at address 0x");
      Serial.println(address, HEX);
    }
  } // end of for loop

  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

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