/* * Lab 1 - Task 5: Basic ADC with Potentiometer & I2C LCD
* Objective: Read analog values and display them on Serial and LCD.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
// If 0x27 doesn't work, try 0x3F
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define your new custom I2C pins
const int custom_SDA = 4;
const int custom_SCL = 5;
const int potPin = 18; // Potentiometer connected to GPIO 34 (ADC1_CH6)
void setup() {
Serial.begin(115200); // Initialize Hyperterminal / Serial Monitor
//Start the I2C bus with the custom pins
Wire.begin(custom_SDA, custom_SCL);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the LCD backlight
// Print a welcome message
lcd.setCursor(0, 0); // Column 0, Row 0
lcd.print("ESP32 ADC Lab");
lcd.setCursor(0, 1); // Column 0, Row 1
lcd.print("Initializing...");
Serial.println("Task 5 Ready: Reading Potentiometer...");
delay(2000); // Wait for 2 seconds before clearing
lcd.clear();
}
void loop() {
// 1. Read the raw ADC value (0 to 4095)
int adcValue = analogRead(potPin);
// 2. Convert the raw value to Voltage (0.0V to 3.3V)
// Formula: (ADC_Value * Max_Voltage) / Max_ADC_Resolution
float voltage = adcValue * (3.3 / 4095.0);
// 3. Display on Hyperterminal (Serial Monitor)
Serial.print("Raw ADC Value: ");
Serial.print(adcValue);
Serial.print("\t Voltage: ");
Serial.print(voltage);
Serial.println(" V");
// 4. Display on I2C LCD 16x2
// Format Row 0 -> "ADC: 4095 "
lcd.setCursor(0, 0);
lcd.print("ADC: ");
lcd.print(adcValue);
lcd.print(" "); // Extra spaces to clear trailing characters
// Format Row 1 -> "Volt: 3.30 V "
lcd.setCursor(0, 1);
lcd.print("Volt: ");
lcd.print(voltage);
lcd.print(" V ");
delay(500); // Update every half second
}