AnalogWrite won't work except @255

Hi everyone,

Me again with yet another programming question, so I really tried and looked at everything but I can’t seem to find why my arduino won’t give any PWM on my pin 45.

To be sure, I tried with a simple Fade sketch and everything works perfectly, so PWM or Arduino hardware is not to be suspected.

Now in my sketch between 6-25seconds, I have a PID calculating a PWM value that I’m then putting in an analogWrite(pin45,xxx).
I even tried to put an intermediate int value (since Output is a Double) but it didn’t change anything…

I can monitor the PWM on the small LED of my mosfet module and it does light up only when the PWM value reaches 255.

I searched in vain for any line that would put this value to 0 but nothing… Also, I’m monitoring th Output of the PID on an LCD and it’s rising slowly as expected…

My sketch is of course attached.

Thanks for your help!

Marc

SketchStart2_76.ino (14.3 KB)

MEGA?

Yes Sir! Sorry, I should have mentioned that...

In between I actually found the problem. I really had been stuck on this one for days but it's like the doctor's appointment, as soon as you call, you're already feeling better!

Turns out I'm using the servo library and of course it inhibits PWM on certain pins (which one remains a mystery to me), so I tried pin 12 and it works just fine!

Thanks anyway!

from - http://arduino.cc/en/Main/arduinoBoardMega -

PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.

marc426:
Yes Sir! Sorry, I should have mentioned that…

In between I actually found the problem. I really had been stuck on this one for days but it’s like the doctor’s appointment, as soon as you call, you’re already feeling better!

Turns out I’m using the servo library and of course it inhibits PWM on certain pins (which one remains a mystery to me), so I tried pin 12 and it works just fine!

Thanks anyway!

Under variants/mega/ in the distribution you’ll find the version of pins_arduino.h
for the Mega, which includes this array:

const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {

And the relevant entries are:

	NOT_ON_TIMER	, // PL 6 ** 43 ** D43	
	TIMER5C	, // PL 5 ** 44 ** D44	
	TIMER5B	, // PL 4 ** 45 ** D45	
	TIMER5A	, // PL 3 ** 46 ** D46	
	NOT_ON_TIMER	, // PL 2 ** 47 ** D47

Showing that timer5 is connected to those pins. The Servo library
starts using 16 bit timers (it needs one timer per 12 pins driven) from
the highest number backwards, so on the Mega, timer 5, 4, 3, 1 in that
order. See the source of the Servo library to see this.