analogWrite on Atmel SAMD behaves differently than documented

I’m working with Atmel SAMD microcontroller on Adafruit Feather M0 board. Looking at the documentation of analogWrite() - Arduino Reference, I read that PWM will be stopped or changed by next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin. In my tests, only analogWrite() affects the PWM on given pin, e.g.:

analogWrite(1, 128);
analogWrite(1, 0);

will leave pin #1 in low state, but

analogWrite(1, 128);
digitalWrite(1, 0);

will leave it producing 50% duty cycle PWM. Is this behavior normal?

Yes, this is expected for the Adafruit SAMD core.

The Arduino documentation can be pretty worthless when working with other core implementations. Every Arduino core implementation is technically free to do whatever it wants and does not implement every caveat in the documentation of the official Arduino documentation. The code of the core implementation has to the final say.

You will e.g. see that with the “exemplary” AVR arduino core implementation, digitalWrite() does…

…turn off the PWM, aka the timer associated with the pin. That stops the wave.

However, with the Adafruit SAMD core implementation of digitalWrite(), you will see no such thing – just setting the GPIO registers and not bothering to turn off any eventually running PWM wave.

You can open an issue at Issues · adafruit/ArduinoCore-samd · GitHub for their violation of the official spec for this to get them to change it.

1 Like

Thank you. I opened an issue: analogWrite on Atmel SAMD behaves differently than described by official Arduino documentation · Issue #304 · adafruit/ArduinoCore-samd · GitHub.