Skip to content

Commit 9e0d06f

Browse files
vs11officialsoburi
authored andcommitted
analogWrite: add digitalWrite fallback for analogWrite without PWM
When analogWrite() is called for a pin that cannot be resolved to a PWM channel, or when the resolved PWM device is not ready, fall back to GPIO output instead of returning without changing the pin state. This matches the common Arduino behavior for pins without hardware PWM support: values above the midpoint drive the pin HIGH, while lower values drive it LOW. Existing hardware PWM behavior is unchanged. Co-Authored-by: Satvik <145106491+vs11official@users.noreply.github.com> Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 4069733 commit 9e0d06f

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

cores/arduino/zephyrCommon.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,14 @@ void analogWrite(pin_size_t pinNumber, int value) {
400400
size_t idx = pwm_pin_index(pinNumber);
401401

402402
if (idx >= ARRAY_SIZE(arduino_pwm)) {
403+
pinMode(pinNumber, OUTPUT);
404+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
403405
return;
404406
}
405407

406408
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
409+
pinMode(pinNumber, OUTPUT);
410+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
407411
return;
408412
}
409413

0 commit comments

Comments
 (0)