Do Mega2560 PWM pins using analogWrite() interfere with each other?

Thread: What should be a simple dim LED display - Project Guidance - Arduino Forum

It seems that Mega2560 PWM on pins 12 and 13 interfere with each other?

Wawa did a test and I trust him to get it right.

void setup() {
  analogWrite(13, 15);
  analogWrite(12, 100);
}

void loop() {
}

I can't get it to work no matter what I do.
Only if I write 0 or 255 or the same value to the second PWM pin.
The attached sketch flashes the build-in LED.
Leo..

I've run out of guesses....

It really ought to work!

Print out the TCCnx registers for the relevant times, and relevant OCR registers before any of the analogWrites, after the first, and after the second.

Likely this will give some insight into what's going on.

I was hoping to vector some help onto that thread.

Ugh, fine, if you insist.

In any event - it works for me!

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
}

void loop() {
  delay(2000);
  Serial.println();
  Serial.println("Start");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  analogWrite(12,127);
  Serial.println("Wrote 12 127");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  digitalWrite(12,0);
  Serial.println("Wrote 12 LOW");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  analogWrite(13,40);
  Serial.println("Wrote 13 40");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  digitalWrite(13,0);
  Serial.println("Wrote 13 LOW");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  analogWrite(12,127);
  analogWrite(13,40);
  Serial.println("Wrote 12 127,13 40");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);
  digitalWrite(12,0);
  digitalWrite(13,0);
  Serial.println("Wrote 12, 13 LOW");
  Serial.println("Timer 0");
  Serial.println(TCCR0A,HEX);
  Serial.println(TCCR0B,HEX);
  Serial.println(OCR0A,HEX);
  Serial.println(OCR0B,HEX);
  Serial.println("Timer 1");
  Serial.println(TCCR1A,HEX);
  Serial.println(TCCR1B,HEX);
  Serial.println(OCR1A,HEX);
  Serial.println(OCR1B,HEX);
  Serial.println(OCR1C,HEX);
  delay(2000);

}

Output:

Start
Timer 0
3
3
28
0
Timer 1
1
3
0
7F
0
Wrote 12 127
Timer 0
3
3
28
0
Timer 1
21
3
0
7F
0
Wrote 12 LOW
Timer 0
3
3
28
0
Timer 1
1
3
0
7F
0
Wrote 13 40
Timer 0
83
3
28
0
Timer 1
1
3
0
7F
0
Wrote 13 LOW
Timer 0
3
3
28
0
Timer 1
1
3
0
7F
0
Wrote 12 127,13 40
Timer 0
83
3
28
0
Timer 1
21
3
0
7F
0
Wrote 12, 13 LOW
Timer 0
3
3
28
0
Timer 1
1
3
0
7F
0

Output on the pins looked fine on the scope. Pin 12 was at 50%, pin 13 was shorter, looked right to me?

Perhaps adding Serial and the loop() code changes something and the original effect is some compiler weirdness?

I just updated Raspbian and can't get Arduino to install cleanly. I haven't tried everything though.
I have to swap OS cards to run Arduino though. PITA to test. Maybe I do need a real PC.

Hello Everybody,

It seems my MEGA 2560 can not analogwrite simulatnously with two different pins and two different outputs.

Here is the code:

void setup() {
 
}
void loop() {
       analogWrite(8,128);
       analogWrite(13,128);
       
}

This outputs a signal at pin 8 of about 500Hz, peak amplitud 1ms, and at pin 13 of 1000Hz , peak amplitud of 0.5ms.

BUT,

when I try this code

void setup() {
 
}
void loop() {
       analogWrite(8,100);
       analogWrite(13,128);
       
}

At pin 8 nothing comes, is 0mv and at pin 13, there is not a regular output, but about every second, there is 1 signal of 200ms, then down for 50ms then up again for about 250ms and then down until next cycle. I have an Osciloscope connected, i could take images if necessary.

If I test another pin rather than 13, but with output different I obtain nothing at both pins, but If I use the same OUTPUT it seems to be owrking good.

Thanks for your help.

Can it be a problem with the MEGA I bought at Amazon...made from China?

Read this page carefully analogWrite()

Could the difference in PWM frequency of the two pins be causing the effect that you see ?

There maybe something peculiar going on with some Megas. See these two threads discussing anomalies when using two different duty cycle values.

https://forum.arduino.cc/index.php?topic=664943.new;topicseen#new

https://forum.arduino.cc/index.php?topic=665369.0

Hi Guys,

I am having the same problem an not only in the pins 12 and 13, I have the problem when I use simulatnously more than two PWM pin with different output value.

The test gave me this result:

Start
Timer 0
3
3
0
0
Timer 1
81
3
7F
0
0
Wrote 12 127
Timer 0
3
3
0
0
Timer 1
81
3
7F
0
0
Wrote 12 LOW
Timer 0
3
3
0
0
Timer 1
81
3
7F
0
0

Start
Timer 0
3
3
0
0
Timer 1
81
3
7F
0
0
Wrote 12 127
Timer 0
3
3
0
0
Timer 1
81
3
7F
0
0
Wrote 12 LOW
Timer 0
3
3
0
0
Timer 1

I have tested in another mega (I bought at the same place) and the problem is still there.

I have tested in another PC and problem is still there.

Shall I buy another MEGA?

Thanks for your support.

Thanks for your answers. If I used the same duty cycles (pins 4 and 13) or the others ones it happen the same, when I am using pin 13, the other pin has no signal an pin 13 the behavior is strnage as a I described earlier, but when I use another pins for example 7 and 8, at pin 8 the behavior is as expected and at pin 7 there is nothing. I will continue dicussion in this topic "Do Mega2560 PWM pins using analogWrite() interfere with each other? - Microcontrollers - Arduino Forum" as was already created and I did not find it yesterday.

Thanks.

Perhaps https://github.com/arduino/Arduino/issues/9653
It's looking like a compiler bug (the first since Arduino started build a custom version of the compiler?)

Duplicate topics merged

Potul:
Thanks for your answers.

Maybe wait more than 6 minutes since the last post?

GoForSmoke that was on the other Topic I opened yesterday an UKHeliBob merge it .. It was not ironical, I am based in Catalonia and I am used to wait several hours or days for any contribution.

I am happy problem seems to be a Compiler issue as I was getting crazzy, although at Githib somebody said it is working with version 1.8.9, I have tested from 1.8.7 to 1.8.12 and problem is still there ....

You guys who are having problems - what results do you get running the code I posted above? Do you see the same behavior there?

Asking because it not only generates PWM, but also shows the exact values of all the relevant registers, so you could get some idea of what might be going on under the hood if the problem reproduces there (you could also add the same register prints to your code to get that info)

That would be wacky behavior if it's a compiler bug though... like, maybe a problem with the iom2560.h - but why would they have been modifying that anyway?

Hi Guys. I have tested IDE version 1.8.5 with AVR core 1.8.2 and it failed also, then I tested IDE 1.8.5. with AVR core 1.6.12 and It work wounderfull. Then I tested latest IDE version 1.8.12 with AVR core 1.6.23 and it did work! So bug is on AVR Boards core from version 1.8.1, as there are no downloable version beween 1.6.23 and 1.8.1.

Potul:
Hi Guys. I have tested IDE version 1.8.5 with AVR core 1.8.2 and it failed also, then I tested IDE 1.8.5. with AVR core 1.6.12 and It work wounderfull. Then I tested latest IDE version 1.8.12 with AVR core 1.6.23 and it did work! So bug is on AVR Boards core from version 1.8.1, as there are no downloable version beween 1.6.23 and 1.8.1.

What results do you get running the sketch I posted?