Skip to content

Commit 4ac5092

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 1316b52 commit 4ac5092

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
@@ -436,10 +436,14 @@ void analogWrite(pin_size_t pinNumber, int value) {
436436
size_t idx = pwm_pin_index(pinNumber);
437437

438438
if (idx >= ARRAY_SIZE(arduino_pwm)) {
439+
pinMode(pinNumber, OUTPUT);
440+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
439441
return;
440442
}
441443

442444
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
445+
pinMode(pinNumber, OUTPUT);
446+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
443447
return;
444448
}
445449

0 commit comments

Comments
 (0)