Go back

Fade an LED with PWM on Arduino – Beginner Tutorial

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 diagram of fade-led on the Velxio simulator canvas

Circuit Walkthrough

Let’s look at the components and wiring for this project.

Components

ComponentQuantityNotes
Arduino Uno1The brain of the operation
LED (any color)1We’ll use a blue LED
Resistor (220 Ω)1Current-limiting resistor (not shown in schematic but recommended)
Jumper wires2For connections

Wiring

  1. LED Anode (long leg) → Connect to Arduino pin 9 (a PWM-capable pin).
  2. 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:

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.

Source code for fade-led in the Velxio Monaco editor

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

Key Concepts

Common Pitfalls & Debugging Tips

  1. 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_PIN matches the actual pin used.
  2. 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, not digitalWrite().
  3. Fade is too fast or too slow?

    • Adjust the delay(30) value. Larger = slower fade.
    • Change fadeAmount to control step size (e.g., 1 for very smooth, 10 for faster).
  4. Simulator not responding?

    • Use the Reset button in the canvas toolbar.
    • Refresh the page if needed.

Velxio editor toolbar with compile and run controls

Suggested Extensions

Once you’ve mastered the basic fade, try these challenges:

  1. Multiple LEDs: Add two more LEDs on pins 10 and 11. Make them fade in sequence or at different speeds.
  2. Button control: Add a push button to start/stop the fade.
  3. Potentiometer speed control: Use a potentiometer to adjust the fade speed (replace delay(30) with a variable read from an analog input).
  4. RGB LED: Use an RGB LED and fade through different colors by controlling three PWM pins.
  5. 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:

👉 Launch the Fade LED example

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!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Serial Hello World: Your First Arduino Communication Project
Next Post
Button-Controlled LED: Your First Arduino Input Project