// Define the analog pin for the potentiometer
int potPin = A0;
// Define the PWM pin for the LED
int ledPin = 9;
// Voltage reference (in volts) for the Arduino, typically 5V or 3.3V
float voltageReference = 5.0;
void setup() {
// Begin Serial Communication
Serial.begin(9600);
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the potentiometer value (0 to 1023)
int sensorValue = analogRead(potPin);
// Map the potentiometer value to PWM range (0 to 255) for LED brightness
int ledBrightness = map(sensorValue, 0, 1023, 0, 255);
// Set the LED brightness
analogWrite(ledPin, ledBrightness);
// Convert the ADC reading to voltage
float voltage = sensorValue * (voltageReference / 1023.0);
// Print the values to the Serial Monitor
Serial.print("Potentiometer Reading: ");
Serial.print(sensorValue);
Serial.print(" -> Voltage: ");
Serial.print(voltage);
Serial.print(" V");
Serial.print(" -> LED Brightness: ");
Serial.println(ledBrightness);
// Wait a bit before the next reading
delay(500);
}