Skip to content

LED Brightness Control on Raspberry Pi Pico with MicroPython & PWM

brightness led

Table of Contents

Abstract

In this article, we present a step-by-step guide to controlling the brightness of an LED using an Arduino Uno. By leveraging Pulse Width Modulation (PWM), you can smoothly vary the LED intensity β€” from dim glow to full brightness β€” rather than simply turning it on or off. This technique is a fundamental building block for more advanced projects such as LED dimmers, ambient lighting, and more.

🧭 Pre-Request

  • Arduino Uno board (or any Arduino with PWM-capable pins)
  • USB cable to connect the Arduino Uno to your computer
  • Arduino IDE (latest version recommended) installed on your system.
  • Interfacing external LED to Arduino Uno. Learn More

Hardware Required

  • Arduino Uno.
  • LED.
  • Resistors.
  • BreadBoard.
  • Mini USB Cable.
  • Connecting wires.
  • 5V DC power supply (Optional)
Components Purchase Link
Arduino Uno link
LED link
BreadBoard large : small
Connecting Wires link
Mini USB Cable link
5V DC Adaptor link

Don't own a hardware 😒

No worries,

Still you can learn using simulation. check out simulation part πŸ˜ƒ.

Connection Table

Particular GPIO Remarks
LED 9 LED Anode to GPIO 9 : High Logic

Note

Using a current-limiting resistor (220 Ξ© to 330 Ξ©) is important to protect the LED and Arduino pin.

Circuit Diagram

fig-Connection Diagram

πŸ“‚ Code

#define ledPin 9         // PWM capable pin
int brightness = 0;      // current LED brightness
int fadeAmount = 3;      // how many points to fade the LED by

void setup() {
  // Set ledPin as OutPut Pin.
  pinMode(ledPin, OUTPUT);

}

void loop() {
  // Set the brightness of ledPin (GPIO 9)
  analogWrite(ledPin, brightness);

  // Change the brightness for the next loop
  brightness = brightness + fadeAmount;

  // Reverse the direction of the fading at the ends
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }

  // Wait to see the dimming effect
  delay(10);

}

How it works

  • ledPin is set to 9, one of the PWM-capable pins on the Arduino Uno.
  • In setup(), we configure this pin as OUTPUT.
  • Inside loop(), we gradually increase the brightness variable from 0 to 255, calling analogWrite() on each step. This raises the duty cycle of the PWM signal, making the LED brighter.
  • After reaching full brightness, the code reverses β€” decreasing brightness from 255 back to 0, causing the LED to dim.
  • The delay(10) controls how smooth and slow the fade will be. You can adjust the delay value for faster or slower fades.

Important Note

  1. Timer Groups:
    • Pins 5,6 β†’ Timer 0 (affects delay(), millis())
    • Pins 9,10 β†’ Timer 1
    • Pins 3,11 β†’ Timer 2
  2. Side Effects: Changing Timer 0 affects delay(), millis(), and micros()
  3. Frequency Ranges: Each timer has limited frequency options based on prescalers
  4. Resolution: Lower frequencies typically allow for better resolution

Code Explanation

  • analogWrite(pin, value) β€” On Arduino, this function outputs a PWM signal on the given pin. The value can range from 0 (always off) to 255 (always on).

  • Duty cycle β†’ Brightness: PWM works by rapidly toggling the pin between HIGH and LOW. The fraction of time it stays HIGH (the duty cycle) determines the brightness perceived by the human eye. A higher duty cycle means more β€œon” time per cycle β†’ brighter LED.

  • Fade effect: By slowly increasing/decreasing the PWM duty cycle in small steps, the LED appears to fade in/out smoothly.

Try It

  • Try connecting RGB LED with PWM.
  • DIY Arduino Uno Christmas Light Controller

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Extras

Why PWM for brightness?

Many microcontrollers, including Arduino, do not provide a true analog voltage output. PWM offers a clever workaround: by switching the output pin on and off very quickly (faster than the eye can detect), and controlling how long it stays ON vs OFF, you can simulate a varying voltage. This allows for analog-like control (brightness, motor speed, audio volume) using purely digital pins. Halvorsen Blog +1

Choosing the right PWM pin

On Arduino Uno, not all pins support PWM. Common PWM pins are D3, D5, D6, D9, D10, D11. Always connect your LED (or other load) to a PWM-capable pin when using analogWrite().

Components details

Modules / Libraries Used

NIL