PWM frequency on photon, 10KHz MAX?

Hi,

I’m trying to get the Photon’s PWM hardware working at 25Khz to avoid an annoying audible noise that a power mosfet in my design does.

I have been reading the particle docs about analogWrite and the related functions, as well as other recipes on internet but neither of that methods seems to work. (https://docs.particle.io/reference/firmware/photon/#analogwrite-pwm-)

I’ve been able to change the resolution of the PWM from 8bits to 12bits with no problem, but when I try to do analogWrite with the frequency parameter I can’t get it working above 10Khz (9999.8Hz actually)

My mosfet is connected to A4 PIN, but I have been trying with A5 and WKP with the same result, pins D2 and D3 (wich share PWM with A4 and A5 are set to digital output (LOW level) and unused in the test scenario.

I’m using firmware version 0.6.1 and Particle Dev.

Thanks for your help.

Have you tried setting a lower resolution and then reading back what the max frequency is for a particlular pin via analogWriteMaxFrequency()
Maybe you need to choose a different pin since not all timers allow for the full range of frequencies.

I did, analogWriteMaxFrequency gave me around 234KHz as maximun allowed frequency but ofcourse it is not working that fast…

void setup(void) {
  pinMode(A4,OUTPUT);	
  analogWriteResolution(A4, 12);
  int maxFreqA4 = analogWriteMaxFrequency(A4);
  Particle.publish("A4",String(maxFreqA4));
}

void loop(void) { }

In the console it publishes the max PWM frequency with 12bit resolution as 14648

For reference D0, D1, D2, D3, A4 and A5 have a max frequency
8bit 234375
10bit 58593
12bit 14648
14bit+ 3662

Testing with this it works:

#include <math.h>

int i = 8;
int maxFreqA4 = 0;
int dutycycle = 0;

void setup(void) {
  pinMode(A4,OUTPUT);	
}

void loop(void) {
   
  if(i < 18) {
    analogWriteResolution(A4, i);
    maxFreqA4 = analogWriteMaxFrequency(A4);
    dutycycle = pow(2,i) / 2; // 50%
    i += 2;
  }
    
  analogWrite(A4, dutycycle, maxFreqA4);
  delay(20000);  // excessive measurement and cloud connecting time
}
1 Like

@makerken thanks for your answer, I have been trying to get it working, and at the end after a lot of tests (and with the help of an oscilloscope) I’ve been able to realize what was going on.

The Photon is not the problem, it works very nice and the PWM freq and resolution can be changed easy and as expected. The problem I’m facing is in the interface hardware that i’m using to achieve a power PWM, looks like I’m losing the switching capabilities at some point… I know this is not the best place to continue debugging this problem, but I’m going to explain my problem just in case it can help anyone (or anyone can help me :wink:

Let’s take a look, I have a circuit connected to A4 as shown in figure 1, A4 PWM output is connected to a 2n2222 transistor that interfaces a power MOSFET PSMN4R8-100BSE
to drive a PTC heating element.

I took some screenshots with the oscilloscope in different points of the circuit while A4 was outputting a PWM freq of 25Khz.

The wave in the first point is correct a pwm output at 25Khz

In point 2, after the resistor the wave is still square.

At point 3, after the transistor the wave looks like a saw shape, and from here i don’t understand what is going on or what to do to fix it (I’m not an electronics engineer but a software developer…) I did the calculations to characterize the switching max freq of the components and it looks enough.

Nothing… at least nothing until the duty cycle is almost maximun where some wave shows up may be for the last 1% of the duty cycle.

Does anyone have any advice or can point me in the right direction to fix the problem?

My quick answer is try reducing R1 to 1k, with 10k the current is limited to 1.2mA, at 1k it still gives you a small 12mA. Try that first. The gate of a MOSFET has capacitance that needs to be drained for it to switch. Voltage is fine, so my instinct is to look at current.

I take it back about the LED, my brain was on current not voltage, you could still do it, but with a voltage divider. You need to blow a few LEDs and create a certain amount of ‘black smoke’ from components or you don’t have any rep. It would be like never experiencing an off by one error.

3 Likes

Are you prototyping with surface mount components? I ask because I doubt it, but the datasheets you helpfully linked to were for surface mount components. I buy stuff from DigiKey but also from AliExpress, the DigiKey parts I expect to be spec but knockoff I can’t assume anything. So extremely basic testing is valuable.

I think you would do well to follow @makerken advice above about lowering R1. The datasheet for the MOSFET shows a maximum input capacitance of 0.0144uF which when coupled with a 10k resistor yields a low-pass filter with a cutoff frequency of around 1 kHz. Changing that resistor to 1k will bring that frequency up to around 11 kHz which is still low but will work much better.

If you have a 220 ohm resistor , I would try that. The current climbs to 55mA but the cutoff frequency will be around 50 kHz.

4 Likes

Nice @bko it was a sawtooth not a RC (resistor-capicator) curve so I knew that the resistor wasn’t the root of the problem, but the filter explains it.

@bko and @makerken thank you very much for your advice, I’ll try tomorrow and get back with the results.

@makerken in this case, because of rush out of the scope of this post xD, I’m prototyping with a PCB with surface mount components, and the components are coming from well-known seller (farnell I think)

Gutsy, they call it a protoboard for a reason, no test server or test cases for this guy :smirk:

Watch the 2N2222 power disappation, I’d just want to run it for 8h and take the temperature before you commercialize. The higher the frequency it switches at the more switching losses, 25kHz is fine, overkill isn’t better in this case.

So… no more SMD mass productions PCBs for testing and check the power dissipation of the transistor, understood xDDDD

Thanks for the advice, but if at some point this product is going to see the market, someone with proper electronic knowledge will design the board for me.

I’ve been testing the PWM with the changes you suggest me and it’s working much better know, I’ve been trying with 220Ohm resistor and 820Ohm, the results as follow.

In both screenshots the CH1 (yellow) is the test point 4 (the MOSFET’s output) and the CH2 (green) is the point 3 (the MOSFET’s gate ), the PWM resolution is 10bits/25Khz with a duty cycle of 50%.

220 Ohm resistor

With this configuration the resistor gets hot, the 2n2222 looks ok.

820 Ohm resistor

As one can see from the shape of the waves in channel two the change in the resistor makes it square and as you said the smaller the resistor the better.

CH1 is other thing, it still has a strange shape, not square… feels like something loading in the rise time, is there something I can do to fix that and get a square output PWM?

Thanks!

Hi @mdps

Yes, I should have reminded you that 12V / 220 ohm is going to dissipate 655mW so a 1W resistor would be a good choice.

As to Ch1 waveform: what kind of load is the MOSFET connected to? It looks to me like the load has a large capacitive component of some kind.

3 Likes

my bad, I had no load before and because of that the output looks ugly, now i’ve connected 2x 50W PTC heaters and every thing is working fine at the end!!! and more important in silence, before, with the 500Hz PWM, the noise was to annoying.

Finally I’m using a 470Ohm resistor, not as perfect pulse, but much cooler.

Thank you very much guys for your help!

3 Likes

Perfect @mdps, you’ve got this.

V ÷ R = I
12V ÷ 470Ω = 25.5mA
P = V x I
12V x 25.5mA = 306mW

1206 components are usually rated for 250mW of power. Testing with a square wave now you are only using it 1/2 the time, so it’s cooling 1/2 the time and fine. But if the user maxes the heat output that resistor will be dealing with the full power the whole time. A 1210 resistor is the same length but a little wider (little or no board redesign) but is usually rated for 500mW. You could parallel two resistors, or in a prototype pinch stack two of the same on top of each other. Even if it’s fine after 8h on max, how many things in use have the air slots to passively cool components clogged, resistors are cheap, overrate by as much as you can. Warranty claims will thank you, actually good design is never noticed- but you’ll know.