Description
The function analogWrite deals with val==0 and val==255 as special cases and disables the PWM mode in favour of a simple digitalWrite. This causes an issue if the PWM has been inverted using a register (e.g.: TCCR2A != 0x30;)
This was identified back in 2017, the youtuber Julian Ilett identified the issue in the following video: https://youtu.be/fEz-NDCDhT4 The issue is somewhat important because it complicates implementing a synchronous buck converter using an Arduino. Further explanation, and a work-around of the issue (it is arguably a bug in analogWrite) was shown in: https://youtu.be/IMm0-gGkRi0
I checked the source of analogWrite and the issue appears to still be there so I thought I'd open an issue.
Solution could be to check if the PWM signal has been inverted and adjust accordingly.