Driving LED dim/glow using PWM pins and MOSFET

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
stanleyseow
Posts: 52
Joined: Sun Sep 27, 2015 3:26 am
Location: Kuala Lumpur, Malaysia
Contact:

Driving LED dim/glow using PWM pins and MOSFET

Post by stanleyseow » Fri Dec 01, 2017 6:03 am

Hi,

I calculated the red/blue pill have a total of 15 PWM pins and I would like to use ALL of them to drive a 12V LED using a logic level MOSFET...

Is there any restrictions on using ALL the PWM pins using analogWrite ?

Any conflict of timers ?

Thanks

Stanley
Stanley
9W2SVT / N5SVT

User avatar
RogerClark
Posts: 7540
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by RogerClark » Fri Dec 01, 2017 8:53 am

AFIK

As there are only 4 timers on he F103C, you will only be able to output 4 unique PWM values

If you want more than that, you're probably going to have to do it via an ISR and some code.

There is a good reference for the Maple mini that gives more information on PWM

http://docs.leaflabs.com/static.leaflab ... m.html#pwm

Specifically

http://docs.leaflabs.com/static.leaflab ... -conflicts

stevestrong
Posts: 1937
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by stevestrong » Fri Dec 01, 2017 2:28 pm

I think it is possible to use all channels of a timer but with the same basic frequency for all, different duty cycles are doable though.

stanleyseow
Posts: 52
Joined: Sun Sep 27, 2015 3:26 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by stanleyseow » Fri Dec 01, 2017 2:54 pm

I probably only need all the pins to be on 2 unique PWM values only ... so does it means I can use all the 15 PWM pins ?

Thanks
Stanley
9W2SVT / N5SVT

User avatar
RogerClark
Posts: 7540
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by RogerClark » Fri Dec 01, 2017 6:37 pm

stanleyseow wrote:
Fri Dec 01, 2017 2:54 pm
I probably only need all the pins to be on 2 unique PWM values only ... so does it means I can use all the 15 PWM pins ?

Thanks
Can you explain in more detail ...

victor_pv
Posts: 1788
Joined: Mon Apr 27, 2015 12:12 pm

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by victor_pv » Fri Dec 01, 2017 8:39 pm

There are 4 timers with 4 channels each.
Single frequency per Timer, but each channel can have a different duty cycle as Steve pointed, so you get up to 16 different PWM duty cycles, grouped 4 by 4 in the same frequency.

There is also code in another thread to do PWM by software in any port and any PIN. In that case the frequency is the same for all pins that you drive with the same code, but each can have a different duty cycle. You can use hardware PWM in some pins, and software PWM in others if you want.

If you want to drive multiple leds with just the same PWM frequency and duty, I think it would be a better idea to use a FET, that will allow a much higher load than the MCU. I see you were planning to use a FET already.

So your limitation is just that 4 timers, each can have a certain frequency. 4 channels per timer, each can have a different duty cycle.

User avatar
RogerClark
Posts: 7540
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by RogerClark » Fri Dec 01, 2017 8:50 pm

thanks guys.

I did not I notice the channel setting, but was not sure this would allow the duty cycle to be changed independently.

The leaflabs docs seem to be rather misleading , if it is possible to vary the duty cycle based on a combination of Timer and Channel

stanleyseow
Posts: 52
Joined: Sun Sep 27, 2015 3:26 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by stanleyseow » Sat Dec 02, 2017 8:17 am

Here are more details...

I plans to drive a n-channel logic level MOSFET using PWM pins ( either software or hardware ) ...

Usually to control the brightness of the LED , in 2 levels ... says 30% brightness in some pins and 90% brightness in other pins ...

I didn't knew about the software based PWM to drive any pins... let me search for that and test it out ...

Thanks
Stanley
9W2SVT / N5SVT

victor_pv
Posts: 1788
Joined: Mon Apr 27, 2015 12:12 pm

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by victor_pv » Sat Dec 02, 2017 2:29 pm

You can use all PWM pins for what you described, since you don't plan on having a bunch of different frequencies, only duty cycles.
Each of the 15 pins available can have it's own PWM duty cycle at any given time.
There are 16 pins with timer channels on them, I'm just guessing, without looking at the schematic, that 1 of them may not be usable because it has an input button or something like that, you doublecheck in case you still can use it.

stanleyseow
Posts: 52
Joined: Sun Sep 27, 2015 3:26 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Driving LED dim/glow using PWM pins and MOSFET

Post by stanleyseow » Tue Dec 05, 2017 9:45 am

Thank you ...
I tested it and it is working for the PWM 15 pins with different duty cycle ....

I m using the following MOSFET, IRLZ34N with the following specs for my PWM 12V LED dimm/glow
Vgs(th) - 1-2v,
Rds(on) 35mΩ

BUT when it is fully ON at 3.3V, analogWrite(xx, 255) or digitalWrite(xx,HIGH) the LED is still not at full brightness at 12V. This MOSFET was suppose to be fully open at 2V.

I did a comparison using an Arduino UNO 5V with similar PWM/digitalWrite(HIGH) and the LED is at full brightness at 12V

I tested with other MOSFET IRF530, Vgs(th) - 2-4v, the brightness was even worst ...

Any suggested MOSFET should I be using ??

Stanley
Stanley
9W2SVT / N5SVT

Post Reply