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

How to Use PWM on Arduino to Control LED Brightness


How to Use PWM on Arduino to Control LED Brightness

PWM stands for Pulse Width Modulation. It is a technique that switches a digital output between HIGH (1) and LOW (0) at a fixed frequency. By varying the proportion of time the signal stays HIGH versus LOW — called the duty cycle — you get a different average voltage on the output. This lets you dim an LED or control a motor speed using ordinary digital pins.

How Arduino PWM Works

Arduino UNO has PWM-capable pins at 3, 5, 6, 9, 10, and 11. The PWM module provides 8-bit resolution, which means 255 distinct levels from 0 to 255. These map directly to 0V through 5V:

PWM ValueAverage Voltage
00V
511V
1282.5V
2555V

Formula: PWM value = (desired voltage × 255) ÷ 5V. Example: 1V → 1.0 × 255 ÷ 5 = 51

Wiring the Arduino PWM Circuit

Connect the components as follows:

  • Potentiometer (10kΩ, 3 terminals): connect to A0, 5V, and GND
  • LED (long leg = anode): connect through a 220–330Ω resistor to pin 9, short leg to GND
Arduino PWM circuit diagram showing Potentiometer wired to A0, 5V, GND, and LED with resistor on pin 9. Clear labels for each connection.
Pinout diagram of Arduino UNO highlighting PWM-capable pins 3, 5, 6, 9, 10, 11 in red, with pin 9 circled for this lesson.

[image: PWM signal waveform illustration showing three duty cycles — 25%, 50%, 75% — with labels linking duty cycle percentage to average voltage output.]

Arduino Code: Read Potentiometer and Output PWM

// Analog input pin connected to the potentiometer
const int sensorPin = A0;
// PWM output pin connected to the LED
const int ledPin = 9;

// Variables to store readings
int sensorValue = 0;
int ledValue = 0;

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    // Read the potentiometer (range: 0-1023)
    sensorValue = analogRead(sensorPin);

    // Remap 0-1023 to PWM range 0-255
    ledValue = map(sensorValue, 0, 1023, 0, 255);

    // Print PWM value to Serial Monitor
    Serial.println(ledValue);

    // Send PWM signal to the LED
    analogWrite(ledPin, ledValue);

    delay(100);
}

Code Breakdown

  1. analogRead(sensorPin) — Reads the voltage from the potentiometer, returns 0–1023
  2. map(sensorValue, 0, 1023, 0, 255) — Converts the 0–1023 range to 0–255, the range accepted by PWM
  3. analogWrite(ledPin, ledValue) — Outputs a PWM signal on pin 9. The LED brightness changes proportionally

Turn the potentiometer toward 5V → input approaches 1023 → LED reaches full brightness. Turn it toward GND → LED dims to off.

Testing the Setup

  1. Upload the sketch to your Arduino board
  2. Open the Serial Monitor and set baud rate to 9600
  3. Turn the potentiometer knob and watch the PWM value change from 0 to 255, while the LED at pin 9 brightens or dims accordingly

[image: Serial Monitor screenshot showing PWM values scrolling from 0 to 255 as the potentiometer is adjusted]

Key Takeaways

PWM lets you generate variable analog voltages using only digital pins. Arduino’s PWM works at 8-bit resolution (0–255). Using map() with analogRead() and analogWrite() gives you smooth, responsive control over LED brightness or motor speed without needing extra hardware.

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

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

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

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