// Task 3: PWM using Potentiometer and LED
int potPin = A0; // Potentiometer connected to analog pin A0
int ledPin = 6; // LED connected to PWM pin 6
int potValue = 0; // Variable to store analog value
int pwmValue = 0; // Variable to store mapped PWM value
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
pinMode(ledPin, OUTPUT);
}
void loop() {
// Step 1: Read analog value from potentiometer (0–1023)
potValue = analogRead(potPin);
// Step 2: Map potentiometer value to PWM range (0–255)
pwmValue = map(potValue, 0, 1023, 0, 255);
// Step 3: Write PWM value to LED
analogWrite(ledPin, pwmValue);
// Step 4: Calculate and print duty cycle percentage
float dutyCycle = (pwmValue / 255.0) * 100.0;
Serial.print("ADC Value: ");
Serial.print(potValue);
Serial.print(" | PWM Value: ");
Serial.print(pwmValue);
Serial.print(" | Duty Cycle: ");
Serial.print(dutyCycle, 1);
Serial.println("%");
delay(200); // Small delay for readability
}