0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Dim LED Using PWM in Embedded C: Simple Guide

To dim an LED using PWM in Embedded C, configure a timer to generate a PWM signal and adjust the duty cycle to control brightness. A higher duty cycle means brighter LED, while a lower duty cycle dims it. Use timer registers to set frequency and duty cycle in your microcontroller code.
📐

Syntax

To dim an LED using PWM, you typically:

  • Initialize the timer peripheral for PWM mode.
  • Set the PWM frequency by configuring the timer's period.
  • Adjust the duty cycle register to control LED brightness.
  • Start the timer to output the PWM signal on the LED pin.

The duty cycle value controls how long the LED is ON versus OFF in each cycle.

c
void PWM_Init(void) {
    // Configure timer for PWM mode
    TIMER_CONTROL_REG = PWM_MODE;
    TIMER_PERIOD_REG = 255; // Set PWM frequency
}

void PWM_SetDutyCycle(uint8_t duty) {
    // Set duty cycle (0-255)
    TIMER_COMPARE_REG = duty;
}

void PWM_Start(void) {
    // Enable timer
    TIMER_CONTROL_REG |= TIMER_ENABLE;
}
💻

Example

This example shows how to initialize PWM and dim an LED by changing the duty cycle from 0 (off) to 255 (full brightness) gradually.

c
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>

void PWM_Init(void) {
    // Set PB1/OC1A as output
    DDRB |= (1 << PB1);

    // Configure Timer1 for 8-bit Fast PWM mode, non-inverting
    TCCR1A = (1 << COM1A1) | (1 << WGM10);
    TCCR1B = (1 << WGM12) | (1 << CS11); // Prescaler 8
}

void PWM_SetDutyCycle(uint8_t duty) {
    OCR1A = duty; // Set duty cycle
}

int main(void) {
    PWM_Init();

    while (1) {
        // Increase brightness
        for (uint8_t i = 0; i < 255; i++) {
            PWM_SetDutyCycle(i);
            _delay_ms(10);
        }
        // Decrease brightness
        for (uint8_t i = 255; i > 0; i--) {
            PWM_SetDutyCycle(i);
            _delay_ms(10);
        }
    }
    return 0;
}
Output
LED brightness smoothly increases from off to full brightness and then dims back to off repeatedly.
⚠️

Common Pitfalls

  • Not setting the timer in PWM mode causes no PWM output.
  • Using wrong pin direction (must be output).
  • Setting duty cycle outside valid range (0-255 for 8-bit PWM).
  • Forgetting to start the timer or enable the output compare pin.
  • Using a very low PWM frequency causing visible flicker.

Always check your microcontroller datasheet for correct register names and PWM setup.

c
/* Wrong: Pin not set as output, no LED light */
// DDRB &= ~(1 << PB1); // Pin input by mistake

/* Right: Set pin as output */
// DDRB |= (1 << PB1);
📊

Quick Reference

PWM Dimming Cheat Sheet:

  • Initialize PWM: Set timer to PWM mode and configure output pin.
  • Set Frequency: Adjust timer period register.
  • Set Brightness: Change duty cycle register (0 = off, max = full brightness).
  • Start PWM: Enable timer and output compare.

Key Takeaways

Configure your timer peripheral in PWM mode to generate the PWM signal.
Control LED brightness by adjusting the PWM duty cycle value.
Set the LED pin as output and enable the timer to start PWM.
Use a suitable PWM frequency to avoid visible flicker.
Always verify register names and settings from your microcontroller datasheet.