0
\$\begingroup\$

I need to light a LED during a sequence with a microcontroller. The sequence is as follows :

  • I need to turn on/off the LED with a given frequency (PWM 1). It will be between 1 and 200 Hz.
  • When the LED is on, I have to control its brightness with a second PWM (the duty cycle will set the brightness, it must go from 1 to 100 %).

I have never done this kind of thing : is it possible ? If yes what is the best approach to do it ?

I have not chosen any particular microcontroller yet. I might chose one from STM32 family. The thing is that I need to control 4 different LED, and those LED are high-output LED.

Thank you in advance. :)

\$\endgroup\$
3
  • \$\begingroup\$ I wouldn't call that double PWM; you want to modulate the PWM, in this case to an extreme. This is fairly standard, but you need to give more info if you want a good answer. Like, what family of microcontrollers are you considering? \$\endgroup\$
    – Mattman944
    Nov 30 '19 at 13:14
  • \$\begingroup\$ I will leave more info as an edit, thanks \$\endgroup\$ Nov 30 '19 at 13:33
  • \$\begingroup\$ If you shared a bit more about your application you might get better answers. \$\endgroup\$
    – Transistor
    Nov 30 '19 at 14:00
2
\$\begingroup\$

If you really want to do it with two PWMs, you can use two timers and an AND gate to combine the output.

If you want to blink the LED as you explain in your question, it is simpler to:

  • Control LED brightness with a times in PWM mode

  • Using another timer, or even the same timer, setup an interrupt that updates PWM registers to turn the LED ON/OFF

Say you want to PWM it at 1 kHz to control brightness. So you got a timer which cycles at 1kHz and outputs a PWM signal. Now, enable the interrupt for this timer, and the interrupt handler will run 1000 times per second. In the code, increment a variable, you got a millisecond tick counter. You can use that to turn the LED on and off.

Depending on your micro, your software framework might already have a timer interrupt that already runs every millisecond, so you just need to add your code to it.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your answer, I did not think about that. I just need to find a microcontroller which can handle 4 different LED sequences at the same time. :) \$\endgroup\$ Nov 30 '19 at 13:38
  • \$\begingroup\$ If you need 4 different LED intensities you need 4 PWM outputs (but not necessarily 4 timers, some timers can have several outputs) ... there are also I2C chips which can drive several LEDs with independent PWM \$\endgroup\$
    – bobflux
    Nov 30 '19 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.