Introduction
Welcome to your first PWM (Pulse-Width Modulation) project! In this tutorial, we’ll build a simple circuit that smoothly fades an LED from off to bright and back again. This is a classic Arduino beginner project that teaches you how to use analogWrite() to control brightness, motor speed, or any device that responds to a varying average voltage.
We’ll be using Velxio, an in-browser circuit simulator with SPICE-accurate analog support. No hardware needed – just your browser! You can follow along, edit the code, and see the results instantly.

Circuit Walkthrough
Let’s look at the components and wiring for this project.
Components
| Component | Quantity | Notes |
|---|---|---|
| Arduino Uno | 1 | The brain of the operation |
| LED (any color) | 1 | We’ll use a blue LED |
| Resistor (220 Ω) | 1 | Current-limiting resistor (not shown in schematic but recommended) |
| Jumper wires | 2 | For connections |
Wiring
- LED Anode (long leg) → Connect to Arduino pin 9 (a PWM-capable pin).
- LED Cathode (short leg) → Connect to GND on the Arduino.
Important: Always use a current-limiting resistor (typically 220 Ω to 1 kΩ) in series with the LED to prevent burning it out. In the Velxio simulator, the LED model includes internal resistance, but in real life you must add one.
Here’s the wiring in detail:
- Wire 1 (blue): From Arduino pin 9 to LED anode (A).
- Wire 2 (black): From LED cathode (C) to Arduino GND.
That’s it! Only two connections. The Arduino’s pin 9 can output a PWM signal, which we’ll use to vary the LED’s brightness.

Code Walkthrough
Open the code editor in Velxio. You’ll see the following Arduino sketch:
// Fade LED with PWM
// Smoothly fade LED brightness
const int LED_PIN = 9; // PWM pin
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, brightness);
brightness += fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
How It Works
LED_PIN = 9: We use pin 9 because it supports PWM (marked with a~on the Arduino Uno).brightness: This variable holds the current PWM value (0 to 255). 0 = off, 255 = full brightness.fadeAmount: How much the brightness changes each loop. Positive = getting brighter, negative = getting dimmer.setup(): Sets the LED pin as an output.loop():analogWrite(LED_PIN, brightness)outputs the PWM signal.brightness += fadeAmountincreases or decreases the brightness.- The
ifstatement checks if brightness has reached 0 or 255. If so, it reverses the direction by negatingfadeAmount. delay(30)slows down the fade so you can see it.
Key Concepts
- PWM (Pulse-Width Modulation): A technique where the digital pin rapidly switches on and off. The average voltage (and thus LED brightness) depends on the duty cycle (on-time vs. off-time).
analogWrite()sets the duty cycle. analogWrite(): Accepts values from 0 (always off) to 255 (always on).pinMode(): Configures the pin as output (or input).delay(): Pauses the program for milliseconds. Without it, the fade would be too fast to see.
Common Pitfalls & Debugging Tips
-
LED not lighting?
- Check that the LED is connected correctly (anode to pin 9, cathode to GND).
- Ensure the code is uploaded (click the Run button in the toolbar).
- Verify that
LED_PINmatches the actual pin used.
-
LED stays on full brightness?
- Make sure you’re using a PWM-capable pin (3, 5, 6, 9, 10, 11 on Uno). Pin 13 is not PWM.
- Check that
analogWrite()is called, notdigitalWrite().
-
Fade is too fast or too slow?
- Adjust the
delay(30)value. Larger = slower fade. - Change
fadeAmountto control step size (e.g., 1 for very smooth, 10 for faster).
- Adjust the
-
Simulator not responding?
- Use the Reset button in the canvas toolbar.
- Refresh the page if needed.

Suggested Extensions
Once you’ve mastered the basic fade, try these challenges:
- Multiple LEDs: Add two more LEDs on pins 10 and 11. Make them fade in sequence or at different speeds.
- Button control: Add a push button to start/stop the fade.
- Potentiometer speed control: Use a potentiometer to adjust the fade speed (replace
delay(30)with a variable read from an analog input). - RGB LED: Use an RGB LED and fade through different colors by controlling three PWM pins.
- Serial monitor: Print the brightness value to the Serial Monitor for debugging.
Try It Yourself!
Ready to see it in action? Open the live example in Velxio:
You can edit the code, change the fade speed, or even add new components. The simulator runs entirely in your browser – no downloads, no setup.
Happy fading!