#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 and dimensions to 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(9, OUTPUT);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Pot: ");
lcd.setCursor(0, 1);
lcd.print("Brightness: ");
}
void loop() {
int pot = analogRead(A0); // 0 to 1023
int brightness = map(pot, 0, 1023, 0, 255);
int brightnessPercent = map(brightness, 0, 255, 0, 100);
analogWrite(9, brightness);
// Display potentiometer value
lcd.setCursor(5, 0);
lcd.print(" "); // clear old value
lcd.setCursor(5, 0);
lcd.print(pot);
// Display brightness percentage (always 3 digits with %)
lcd.setCursor(12, 1);
if (brightnessPercent < 10) lcd.print(" "); // 1 digit
else if (brightnessPercent < 100) lcd.print(" "); // 2 digits
lcd.print(brightnessPercent);
lcd.print("%");
delay(200); // Refresh quickly
}