How to Use Arduino PWM to Control LED Brightness with analogWrite()
How to Use Arduino PWM to Control LED Brightness with analogWrite()
Regular digital signals from Arduino can only be ON (5V) or OFF (0V). But many projects need intermediate control, like dimming an LED smoothly instead of just switching it on and off. This article teaches how to use Analog Output via PWM on Arduino.
What is PWM?
PWM (Pulse Width Modulation) is a technique that outputs rapid pulses and varies the ratio of time the signal is HIGH versus LOW. When averaged, this creates an intermediate voltage level.
Arduino PWM Values
Arduino UNO outputs PWM with 8-bit resolution, meaning 256 levels (0-255).
| PWM Value | Average Voltage | Result |
|---|---|---|
| 0 | 0V | LED fully off |
| 127 | ~2.5V | LED medium brightness |
| 255 | 5V | LED fully on |
PWM Pins on Arduino UNO
Not every pin on Arduino can output PWM. PWM-capable pins have a ~ symbol next to the number:
- Pins 3, 5, 6, 9, 10, 11
Required Components
- Arduino UNO R3 with USB cable
- 5mm Red LED x4
- Breadboard 830 Point
- 330 Ohm Resistor x4
- Jumper wires M-M
Wiring Diagram
Wire the circuit as follows using 4 PWM pins:
| Arduino Pin | Connected To |
|---|---|
| Pin 3 | LED1 via 330Ω Resistor |
| Pin 5 | LED2 via 330Ω Resistor |
| Pin 6 | LED3 via 330Ω Resistor |
| Pin 9 | LED4 via 330Ω Resistor |
Note: Connect the positive leg (longer lead) of each LED to the Arduino pin through the resistor. Connect the negative leg (shorter lead) to GND on the breadboard.
The analogWrite() Command
Syntax:
analogWrite(pin, value)
Parameters:
pin- The pin number to output the signal (must be a PWM pin)value- Signal level from 0 to 255
Example Code: LED Fade In and Fade Out
This code gradually increases brightness to maximum, then fades out smoothly, repeating in a loop.
const int ledPin3 = 3;
const int ledPin5 = 5;
const int ledPin6 = 6;
const int ledPin9 = 9;
void setup() {
// No need to set pinMode as OUTPUT for analogWrite
Serial.begin(9600);
}
void loop() {
// Gradually increase brightness (fade in)
for (int x = 0; x <= 255; x++) {
analogWrite(ledPin3, x);
analogWrite(ledPin5, x);
analogWrite(ledPin6, x);
analogWrite(ledPin9, x);
delay(10);
Serial.println(x);
}
// Gradually decrease brightness (fade out)
for (int x = 255; x >= 0; x--) {
analogWrite(ledPin3, x);
analogWrite(ledPin5, x);
analogWrite(ledPin6, x);
analogWrite(ledPin9, x);
delay(10);
Serial.println(x);
}
}
Code breakdown:
- Pin variables - Define which Arduino pins connect to which LEDs
- Fade In loop - Gradually increase value from 0 to 255 and output to all PWM pins
- Fade Out loop - Gradually decrease value from 255 to 0 and output to all PWM pins
- delay(10) - Add 10ms delay between each value change, making the light transition smooth
Practical Applications
After understanding the basics, you can apply PWM in various projects:
- Control DC motor speed
- Adjust LED strip brightness
- Create light effects and animations
- Control Buzzer volume (in some cases)
Important Notes
- Always connect a resistor, otherwise the LED will burn out quickly
- Verify the pin has a ~ symbol, regular Digital pins cannot output PWM
- Arduino UNO PWM frequency is approximately 490Hz, which may not be suitable for some applications like directly controlling servo motors
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย