LED Brightness Control on Arduino Nano using PWM

Table of Contents
Abstract
In this article, we present a step-by-step guide to controlling the brightness of an LED using an Arduino Nano. 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 Nano board (or any Arduino with PWM-capable pins)
- USB cable to connect the Arduino Nano to your computer
- Arduino IDE (latest version recommended) installed on your system.
- Interfacing external LED to Arduino Nano. Learn More
Hardware Required
- Arduino Nano.
- LED.
- Resistors.
- BreadBoard.
- Mini USB Cable.
- Connecting wires.
- 5V DC power supply (Optional)
| Components | Purchase Link |
|---|---|
| Arduino Nano | 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.

fig-Connection Diagram
Code
How it works
- ledPin is set to 9, one of the PWM-capable pins on the Arduino Nano.
- In
setup(), we configure this pin as OUTPUT. - Inside
loop(), we gradually increase the brightness variable from 0 to 255, callinganalogWrite()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
- Timer Groups:
- Pins 5,6 β Timer 0 (affects delay(), millis())
- Pins 9,10 β Timer 1
- Pins 3,11 β Timer 2
- Side Effects: Changing Timer 0 affects delay(), millis(), and micros()
- Frequency Ranges: Each timer has limited frequency options based on prescalers
- 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 Nano 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 Nano, 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
- Arduino Nano Data Sheet
Modules / Libraries Used
NIL