Skip to content

Commit 509439d

Browse files
vs11officialsoburi
authored andcommitted
fix: analogWrite() silently fails when PWM pin lookup fails on Uno Q
On Arduino Uno Q, calling analogWrite() on any pin silently returns without doing anything in two cases: 1. When pwm_pin_index() cannot find the pin in the arduino_pwm_pins[] lookup table (idx >= ARRAY_SIZE(arduino_pwm)) 2. When pwm_is_ready_dt() returns false for the PWM device This causes analogWrite() to appear completely broken to users - no output, no error, no warning. The function just silently does nothing. The pwm-pin-gpios mapping in the Uno Q device tree overlay is missing some pins (D0, D1, D4 are absent or commented out). This means pwm_pin_index() returns (size_t)-1 for those pins, which is always >= ARRAY_SIZE(arduino_pwm), causing an immediate silent return. Added a digitalWrite() fallback in both early-return paths: - If PWM lookup fails → fall back to digitalWrite HIGH/LOW - If PWM device not ready → fall back to digitalWrite HIGH/LOW This ensures analogWrite() always produces some output even when hardware PWM is unavailable, which is consistent with how other Arduino cores handle non-PWM pins. Tested on Arduino Uno Q with: - Motor driver (SmartElex L298N integrated board) - Confirmed analogWrite() was completely silent before fix - Confirmed digitalWrite() fallback works correctly after fix - Pins tested: 3, 4, 5, 6, 7, 8, 9, 10 - No breaking changes - Existing PWM functionality unchanged - Non-PWM pins now behave like standard Arduino (HIGH if value > 127) - Fixes user confusion when analogWrite() silently does nothing Co-Authored-by: Satvik <145106491+vs11official@users.noreply.github.com> Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 7dde3d9 commit 509439d

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

483483
if (idx >= ARRAY_SIZE(arduino_pwm)) {
484+
pinMode(pinNumber, OUTPUT);
485+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
484486
return;
485487
}
486488

487489
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
490+
pinMode(pinNumber, OUTPUT);
491+
digitalWrite(pinNumber, value > 127 ? HIGH : LOW);
488492
return;
489493
}
490494

0 commit comments

Comments
 (0)