arduino / arduino/ArduinoCore-samd

analogWrite() uses a threshold when writing to a digital port

Open
#706 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
502
Forks
740
PR merge metrics
No merged PRs in 30d

Description

When analogWrite() is called to write to a pin that is in digital mode, it maps the analog output value to 8 bits and then applies a threshold of 128, writing LOW if < 128 else HIGH. To me this is plain wrong, as one expects in C and C++ that only a value of 0 will result in LOW.

I suggest the code be changed as follows:

```
311,316c311
< value = mapResolution(value, _writeResolution, 8);
< if (value < 128) {
< digitalWrite(pin, LOW);
< } else {
< digitalWrite(pin, HIGH);
< }
---
> digitalWrite(pin, value == 0 ? LOW : HIGH);
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the analogWrite() implementation and the digital-mode branch described in the issue. Verify how values are mapped before the LOW/HIGH decision; done means zero produces LOW while every nonzero value produces HIGH, with the behavior covered by the relevant tests if available.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.