79. Simultaneous Blinking LEDs

Every embedded engineer's journey begins with blinking an LED, but professional applications demand much more sophistication. When you need multiple LEDs blinking at different precise intervals while your microcontroller handles critical tasks, you need robust solutions that don't compromise system performance.

Using Hardware Timer Interrupts

Why this Approach Works Best
After implementing dozens of industrial control systems, I've found that hardware timers provide the perfect balance of precision and efficiency. They work like precision Swiss watches inside your microcontroller, completely independent of your main program flow.

Single Timer Implementation (Most Resource-Efficient)

How it Works:

  • Configure one timer to generate regular interrupts (e.g., every 1ms)
  • Maintain separate counters for each LED in the interrupt service routine (ISR)
  • Toggle LEDs when their respective counters reach target values

Best For:

  • Resource-constrained devices
  • Systems with limited available timers
  • Applications where minor timing coupling is acceptable

Pro Tip: I always use a base interval that's the greatest common divisor of all required blink rates. For 100ms and 360ms, 20ms works perfectly.

Multiple Timer Method (Maximum Independence)

When to Choose This:

  • When absolute timing independence is critical
  • For safety-critical systems where failure isolation matters
  • When working with microcontrollers that have multiple timers

Implementation Insight:

  • Assign dedicated timers to each LED
  • Configure each timer's compare match for the exact required interval
  • Keep ISRs ultra-simple – just pin toggling

Comparative Analysis

FeatureSingle TimerMultiple Timers
Resource UsageLowHigher
Timing PrecisionExcellentPerfect
Implementation ComplexityModerateSimple
ScalabilityGoodLimited
Debugging EaseChallengingStraightforward

Calculating Timer Values

The golden formula I use:

Compare_Value = (Desired_Interval * Clock_Frequency) / Prescaler - 1

Handling Long Intervals

 For intervals exceeding timer maximums:

  • Use a prescaled 16-bit timer
  • Implement a secondary counter in the ISR
  • Consider using the timer overflow interrupt
     

So, by choosing any of the approaches and considering the above points, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!