# Using 16*2 display

**16×2 LCD with I²C backpack (PCF8574)** on Arduino/ESP boards.

---

# ✅ **Hardware Connections**

Since it uses I²C, only **4 wires** are required:

| LCD Pin | Connect to |
| --- | --- |
| VCC | 5V |
| GND | GND |
| SDA | ESP8266 → D2 (GPIO4) / ESP32 → GPIO21 / Arduino Uno → A4 |
| SCL | ESP8266 → D1 (GPIO5) / ESP32 → GPIO22 / Arduino Uno → A5 |

---

# ✅ **Install Required Library**

Install:

### **LiquidCrystal\_I2C**

by Frank de Brabander  
(Arduino Library Manager)

---

# ✅ **Basic Example Code (Works on Arduino, ESP8266, ESP32)**

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

// Most common I2C LCD address is 0x27 or 0x3F
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Wire.begin();          
  lcd.init();            
  lcd.backlight();       

  lcd.setCursor(0, 0);
  lcd.print("Hello World!");

  lcd.setCursor(0, 1);
  lcd.print("I2C LCD 16x2");
}

void loop() {
}
```

---

# 🔍 **If your LCD does not show anything**

Try these:

### 1️⃣ **Check I2C address using scanner**

Upload this sketch:

```cpp
#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Serial.println("Scanning...");
}

void loop() {
  byte error, address;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if(error == 0) {
      Serial.print("I2C device found: 0x");
      Serial.println(address, HEX);
    }
  }
  delay(2000);
}
```

Use the detected address in:

```cpp
LiquidCrystal_I2C lcd(0x27, 16, 2);
```

---

# 🛠 **Adjust contrast**

Turn the **blue potentiometer** on the I2C backpack until text becomes visible.

---

```cpp
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

// Change these:
#define DHTPIN 2        // DHT data pin (D4 on ESP8266)
#define DHTTYPE DHT11   // or DHT22

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);

  dht.begin();
  lcd.init();
  lcd.backlight();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // °C
  float f = dht.readTemperature(true); // °F

  if (isnan(h) || isnan(t)) {
    lcd.clear();
    lcd.print("DHT Error!");
    delay(1000);
    return;
  }

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print(" C");

  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");

  delay(1500);
}
```
