cancel
Showing results for 
Search instead for 
Did you mean: 

Control LED light

Ala
Senior

hey there

I have a project where I need to control light of a LED and dim it. usually we use TIMER PWM GENERATION. for example in CubeMX I set a pin to TIMx CHy and by choosing "PWM Generating CHy" I can easily control LED light using PWM signal.

but in my case this LED pin has no TIM Channel connected to it. what should I do to dim the LED?

10 REPLIES 10
Ahmet Yasin CİVAN
Associate III

You can use GPIO with delay loops.

Ala
Senior

sure! but this occupies CPU and also is not efficient.

is there a way to generate an internal PWM signal and control LED with it?

Nikita91
Lead II

Set a timer as timebase at 1000Hz, and in the ISR toggle the LED GPIO.

This will generate your "PWM"

You should be able to manage about 16 light levels

Javier1
Principal

So you need to implement a softpwm.

There is three ways:(that i can think of now)

  • software bitbang (toglegpio;delay;togglegpio),Process intensive, not scalable, huge jitters.
  • interrupt based bitbang (as @Nikita91​ )said , less process intensive, jitter depends of the interrupt priority.
  • DMA based GPIO bitbang, lesser processor intensive, very scalable, a bit more complex and memory hungry.

If your system can spare those interrution times i would choose the second option.

I had to implement a bunch of pins at the same time and my system couldnt spare lots of processing time so i had to do the third option

https://www.hackster.io/javier-munoz-saez/all-pins-as-pwm-at-the-same-time-baremetal-stm32-1df86f

we dont need to firmware by ourselves, lets talk

You could also use the DAC if that function is available in your pin (overkill)

we dont need to firmware by ourselves, lets talk

this one sounds great! but it affects the entire process, as ISR happens fast, though I might end up doing such...

You can set the timer to 500Hz, and the ISR is quite short.

I actually ended up doing something similar to that... and yeap! it worked! thanks many times @Javier Muñoz​ 

yes but also I can detect LED being ON/OFF