กลับหน้าหลัก
views
How to Use Arduino PWM to Control LED Brightness with analogWrite()
Last updated on

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.

Diagram comparing Digital On/Off signal with PWM signals at different duty cycles: 0%, 50%, 100%

Arduino PWM Values

Arduino UNO outputs PWM with 8-bit resolution, meaning 256 levels (0-255).

PWM ValueAverage VoltageResult
00VLED fully off
127~2.5VLED medium brightness
2555VLED 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
Arduino UNO diagram highlighting PWM pins with colored circles for clarity

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 PinConnected To
Pin 3LED1 via 330Ω Resistor
Pin 5LED2 via 330Ω Resistor
Pin 6LED3 via 330Ω Resistor
Pin 9LED4 via 330Ω Resistor
Breadboard circuit diagram showing Arduino UNO connected to 4 LEDs with resistors

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:

  1. Pin variables - Define which Arduino pins connect to which LEDs
  2. Fade In loop - Gradually increase value from 0 to 255 and output to all PWM pins
  3. Fade Out loop - Gradually decrease value from 255 to 0 and output to all PWM pins
  4. 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

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...