Skip to content

Erronious behavior of analogWrite when used for PWM in inverted mode #9801

Open
@davidosterberg

Description

@davidosterberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CoreRelated to the code for the standard Arduino APIType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions