// Pin definitions
const int potPin = A0; // Pin connected to the potentiometer wiper (middle pin)
const int ledPin = 9; // Pin connected to the LED
// Variables for potentiometer value
int potValue = 0; // Variable to store potentiometer value (0 to 1023)
int ledBrightness = 0; // Variable to store PWM value for LED brightness (0 to 255)
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Set LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the potentiometer value (0-1023)
potValue = analogRead(potPin);
// Map the potentiometer value to a range of 0 to 255 for PWM (LED brightness)
ledBrightness = map(potValue, 0, 1023, 0, 255);
// Set the LED brightness using PWM
analogWrite(ledPin, ledBrightness);
// Print the potentiometer value and mapped brightness to the Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" -> LED Brightness: ");
Serial.println(ledBrightness);
// Small delay to stabilize the reading
delay(50);
}