arduino / arduino/Arduino

Erronious behavior of analogWrite when used for PWM in inverted mode

Open
#9,801 0 comments 1 reaction 0 assignees View on GitHub
Component: Core Type: Bug
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

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.

Contributor guide

Open the contributing guide

Research direction

Start by locating the analogWrite implementation and its special handling for val==0 and val==255. Inspect how inverted PWM is represented through TCCR2A, then compare normal and inverted behavior using the workaround described in the issue's linked videos. Done means the boundary values preserve the intended inverted PWM behavior without breaking ordinary analogWrite use.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.