arduino / arduino/ArduinoCore-renesas

Calls to digitalWrite() don't work after a previous analogWrite()

Open
#58 8 comments 1 reaction 0 assignees View on GitHub
topic: code type: imperfection
Dominant language
C
Stars
193
Forks
112
PR merge metrics
No merged PRs in 30d

Description

Once you've generated PWM on a pin with `analogWrite()`, a subsequent `digitalWrite()` doesn't work.

Here is a sequence of calls and the resulting voltage on the pin (on core 0.8.7), compared to the same sequence on an original Arduino Uno R3:

| Command | Arduino R4 | Arduino R3 |
| --------------- | ---------------- | ----------------- |
| `pinMode(3, OUTPUT)` | 0V | 0V |
| `digitalWrite(3, HIGH)` | 4.7V | 5V |
| `analogWrite(3, 200)` | 3.68V | 3.9V |
| `digitalWrite(3, LOW)` | 3.68V | 0V |
| `digitalWrite(3, HIGH)` | 3.68V | 5V |

The R3 behaviour is what I'd expect.

Here's a visual demonstration of the problem. This program first blinks the builtin LED, and then pulsates it smoothly on and off:

```cpp
void blink () {
for (int i=0; i<5; i++) {
digitalWrite(13, HIGH); delay(1000);
digitalWrite(13, LOW); delay(1000);
}
}

void pulse () {
for (int i=0; i<5; i++) {
for (int x=0; x<=255; x++) { analogWrite(13, x); delay(5); }
for (int x=255; x>=0; x--) { analogWrite(13, x); delay(5); }
}
}

void setup() {
pinMode(13, OUTPUT);
}

void loop() {
blink();
delay(1000);
pulse();
delay(1000);
}
```
The call to `blink()` only works the first time around `loop()`.

The same problem occurs on the Uno R4 WiFi.

### Additional context

The bug was originally reported via the private Early Access Program category on Arduino Forum:

https://forum.arduino.cc/t/calls-to-digitalwrite-dont-work-after-a-previous-analogwrite/1141523

#### Additional reports

- https://forum.arduino.cc/t/digitalwrite-does-not-work-on-uno-r4-wifi/1394091

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the issue with the setup(), loop(), digitalWrite(), and analogWrite() sequence shown in the report on an Uno R4. Trace the digitalWrite() and analogWrite() entry points in the Arduino core and compare the pin state transitions with the expected R3 behavior. Done means digitalWrite() correctly changes the pin after analogWrite() has been used.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.