-->
Page 1 of 2

Problem with smooth transition between PWM range?

PostPosted: Sat Nov 26, 2016 8:27 pm
by BlitzSSS
Hi All

I'm driving a large solenoid via PWM and a NTD5867NLT4G Mosfet on GPIO14. My aim is to give a short burst of full power to initially open the valve, but then the valve can stay fully open with much less holding current so I am then scaling down the PWM range to save power.

Here's exactly what I'm doing:

analogWrite(solenoidPin, 1023);
delay(200);
analogWrite(solenoidPin, 235);

The problem im having is on first run only, the valve opens for the initial 200ms with PWM 1023 but then the ESP seems to have some kind of interruption and struggles to smoothly change the PWM range down to 235, instead the pin goes low, the valve shuts, then PWM starts at 235 but the valve is already shut.

This is only on the first time the ESP runs the sketch after a reboot, otherwise it runs this smoothly and corrects itself on the next loop even after if turn the valve off (analogWrite(solenoidPin, 0)) and then on again via the above code.

Can anyone make any suggestions of what is going on or how I can avoid this please?

Re: Problem with smooth transition between PWM range?

PostPosted: Sun Nov 27, 2016 3:56 am
by Barnabybear
Hi, I'm not sure why that would happen but the ESP does various house keeping at random times when it can. As a work round and just for general interest does it do the same if you use a digital write for the open and then an analog write for the hold?

Re: Problem with smooth transition between PWM range?

PostPosted: Sun Nov 27, 2016 7:48 am
by rudy
What happens is you do this,

analogWrite(solenoidPin, 1023);
delay(2);
analogWrite(solenoidPin, 1023);
delay(200);
analogWrite(solenoidPin, 235);

Re: Problem with smooth transition between PWM range?

PostPosted: Sun Nov 27, 2016 6:19 pm
by BlitzSSS
Barnabybear wrote:Hi, I'm not sure why that would happen but the ESP does various house keeping at random times when it can. As a work round and just for general interest does it do the same if you use a digital write for the open and then an analog write for the hold?


Thanks, ordinarily I'm running the ESP at the default 80Mhz, I did try 160Mhz in case it was the housekeeping tasks you mentioned but no change. I've also tried delays and yields before and after and in-between with no change.

Yes I've also tried digitalWrite(solenoidPin, HIGH) instead of analogWrite(solenoidPin, 1023) and same thing :(

rudy wrote:What happens is you do this,

analogWrite(solenoidPin, 1023);
delay(2);
analogWrite(solenoidPin, 1023);
delay(200);
analogWrite(solenoidPin, 235);


Hi, I definitely tried this too but with a 1ms delay between the two analogWrite(solenoidPin, 1023). I'll try again tonight.

Also might add I'm using Arduino IDE 1.6.12 and ESP8266 Core 2.3.0 (latest versions at the time of writing).

Thanks again for your suggestions.