Lab 2: PWM LED Dimming with Arduino
Lab 2: PWM LED Dimming with Arduino
animation
electronics
Lab 2: PWM LED Dimming with Arduino
PWM changes the LED on-time. As the duty setting rises, observe the light through the current-limiting resistor and compare perceived brightness with the setting.
Materials (or simulate in TinkerCAD / Wokwi): - Arduino Uno - 1x LED (any color) - 1x 220 ohm resistor - Jumper wires
Circuit:
- LED anode → 220 ohm resistor → Pin 9 (PWM)
- LED cathode → GND
Note
Arduino PWM dimming code
The loop raises and lowers the PWM duty setting in steps. With a suitable resistor and LED, observe how the apparent brightness changes as the on-time changes.
const int ledPin = 9; // PWM pin
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Sweep brightness up
for (int duty = 0; duty <= 255; duty += 5) {
analogWrite(ledPin, duty);
float dutyCyclePct = (duty / 255.0) * 100.0;
float avgVoltage = (duty / 255.0) * 5.0;
Serial.print("PWM: ");
Serial.print(duty);
Serial.print(" | Duty: ");
Serial.print(dutyCyclePct, 1);
Serial.print("% | V_avg: ");
Serial.print(avgVoltage, 2);
Serial.println("V");
delay(50);
}
// Sweep brightness down
for (int duty = 255; duty >= 0; duty -= 5) {
analogWrite(ledPin, duty);
delay(50);
}
}Expected Learning:
Predict how the on-time changes with each duty setting. Observe the LED through its current-limiting resistor; perceived brightness need not change linearly with duty cycle.