Go Down

Topic: Can't get PWM output with myMega 2560 (Read 254 times) previous topic - next topic

hary

Hi guys

I was just trying some measurement with  a brand new multimeter that is able to read PWM and frequencies and wanted a PWM signal on one of the pin of my Mega 2560

So I write a very simple code :
Code: [Select]
void loop(){
analogWrite (8, 127);
}


But impossible to get a signal.
I change the pin to another one then another one but still nothing !

Using the voltmeter, I tried different value for PWM analogWrite(pin, PWMvalue) and get 0V from 0 to 254 and 5V for 255 !

Then I tried the #13 pin as it has the embeded LED and there I got some very strange behavior :
analogWrite(13, 255), would light the LED continuously with 5V on its pin.
analogWrite(13, 0 to 254) would blink the led at low frequency about 1 or 2Hz with a MORSE signal like .._.._.._ with . for led ON and _ for led OFF. The shape of the blinking seems exactly the same for all PWM value in the range 0-254.

Would anybody have an explanation and a solution to this prolem ?

CrossRoads

Do you need to do
pinMode (8, OUTPUT);
with that?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

hary

Well, I've done it in the setup() but still.

Plus as stand the Reference at : https://www.arduino.cc/en/Reference/AnalogWrite
"You do not need to call pinMode() to set the pin as an output before calling analogWrite()"

But in any case, it doesn't make any change. I still got the problem !

Very very strange !


hary

I put exactly the same code in a NANO and it worked as attended.

But on my Mega, all the PWM pin behave only as nonPWM !

I wonder if my Mega is fucked ?

Any chance I could save it ?

manor_royal

If this was a Civil Engineering forum would there be posts like "I need to build a bridge. Someone send me drawings."

sterretje

#5
Jan 13, 2017, 07:08 am Last Edit: Jan 13, 2017, 07:12 am by sterretje
Are you compiling for the correct board? Pin 8 is not a PWM pin on a Nano and neither is pin 13.

If it's not a PWM pin, analogWrite() will use a digitalWrite(pin, LOW) for values < 128 and digitalWrite(pin, HIGH) for other values.

PS
not sure if it's your issue; I think that avrdude would complain and you did not mention that so I'm probably in the wrong track.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

hary

@sterretje
You're absolutely right about pin 8 and 13 of the nano !

But my problem is with PWM on my Mega2560 and yes I upload with checking the right board to upload to !

I've been talking about the nano only because I compared the same code on nano and Mega2560 to make some comparison.

What I should do is comparing wth another Mega 2560, but I have only one ! The supposedly broken one.

Go Up