Controlling the output power of a pin (dimming external LED)

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Controlling the output power of a pin (dimming external

Postby fma » Wed Aug 13, 2014 5:46 am

100Hz is far too slow, and it is normal than you see such flickering when moving the LED...

If you move the led at say 1m/s,at such slow frequency, the led will be on/off every 1cm; if the duty cycle is 50%, this is 5mm on and 5mm off.
Frédéric

User avatar
dhylands
Posts: 2253
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: Controlling the output power of a pin (dimming external

Postby dhylands » Wed Aug 13, 2014 6:52 am

Sorry about the mis-information about freq=5.

It turns out that one of the factors influencing the frequency of the timer is determined by the value of the PSC register.

For timers, 2, 3, 4, 5, 6, 7, 12, 13, and 14 it works out to 84 MHz.
For timers 1, 8, 9, 10, 11 it works out to 168 MHz (I think I haven't confirmed this yet).

If you do:

pwm = PWM(pyb.Pin.board.X1)

then you'll get timer 2.

The PSC value of 83 corresponds to a prescaler of 84, so the timer clock is 84,000,000 / 84 = 1,000,000 or 1 MHz.

The ARR value is 9999 which means that the timer reloads every 10,000 timer clocks or 1,000,000 / 10,000 = 100 times per second.

So if you set PSC to 41, then your LED will get PWM with a frequency of 200 Hz.
If you set PSC to 20 then LED will get PWM with a frequeny of 400 Hz

In particular, I'm referring to line 110 which says:

Code: Select all

    stm.mem32[pwm_timer + stm.TIM_PSC]      =  83      #gives 10,000 steps between 0 and 3.3v


I was able to confirm this on my logic analyzer. I attached a screenshot of what the signal looks like with a PSC of 41 and pwm.duty(50)

Screenshot-2.png
Screen shot taken with PSC=41 and duty(50)
Screenshot-2.png (95.74 KiB) Viewed 1152 times


Over on the right it shows the frequency of about 200 Hz (199.92 Hz) and 50/255 = 19.6% duty cycle. 19.6% of 5.002 msec = 0.980 msec (The Width value over on the right of the screenshot)

drexxler
Posts: 7
Joined: Tue Aug 12, 2014 8:38 pm

Re: Controlling the output power of a pin (dimming external

Postby drexxler » Wed Aug 13, 2014 1:01 pm

Okay, so time for a few new questions!

The frequency is set back on 5 and I've adjusted the PSC down to 1 and there's absolutely no flicker, it's completely a ribbon. My question is, what are the drawbacks of this? Should I be expecting significantly increased power usage, or is the change unnoticeable? This is a pretty important part of my program, so I have my fingers crossed that it's perfectly okay to do this.

Thanks again for all your help! My LED is finally smoothly shifting through the color spectrum!

User avatar
dhylands
Posts: 2253
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: Controlling the output power of a pin (dimming external

Postby dhylands » Wed Aug 13, 2014 1:52 pm

There are led driver chips that use frequencies like 1.6MHz ans 2MHz so I'm going to assume its fine. The change in power usage is probably negligible, but you'd have to measure to be sure.

PinkInk
Posts: 61
Joined: Tue Mar 11, 2014 3:42 pm

Re: Controlling the output power of a pin (dimming external

Postby PinkInk » Fri Aug 15, 2014 4:10 am

The call to timer with a frequency in my module is just to init it, which I couldn't work out how to do directly.

As Dave says the frequency is changed later - the following code is a translation of what Damien does in C when you provide a frequency to init a timer, to calc values for prescaler and period (stm.TIM_PSC and stm.TIM_ARR respectively) ... I don't profess to understand the calc ...

Code: Select all

def calc(timer, freq):
  if timer in [1,8,9,10,11]:
    clk_speed = pyb.freq()[3]
  elif timer in [2,3,4,5,6,7,12,13,14]:
    clk_speed = pyb.freq()[2]
  period = max(1, int(2 * clk_speed / freq))
  prescalar = 1
  while period > 0xffff:
    period >>= 1
    prescalar <<= 1
  prescalar -= 1
  period -= 1
  return (prescalar, period)

aaronrodg
Posts: 3
Joined: Thu Sep 29, 2016 11:14 am
Location: United States
Contact:

Re: Controlling the output power of a pin (dimming external LED)

Postby aaronrodg » Fri Mar 10, 2017 5:36 am

Two popular methods for dimming LEDs in switched-mode driver circuits exist: Pulse-Width Modulation (PWM) dimming and analog dimming. Both methods control the time-averaged current through the LED or LED string, but there are diff erences between the two which become evident when examining the advantages and disadvantages of the two types of dimming circuits.

Analog dimming of LEDs is the adjustment of cycle-by-cycle LED current. More simply put, it is the adjustment of the constant LED current level. Analog dimming can be accomplished by an adjustment of the current sense resistor RSNS, or by driving an analog voltage on some DIM function pin of the IC.


Return to “The MicroPython pyboard”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest