Skip to content

Analysis comment: Arduino GPIO Read on output pins returns stale Low#2551

Draft
Copilot wants to merge 1 commit into
mainfrom
copilot/arduino-gpio-read-fix
Draft

Analysis comment: Arduino GPIO Read on output pins returns stale Low#2551
Copilot wants to merge 1 commit into
mainfrom
copilot/arduino-gpio-read-fix

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 2, 2026

Reported on Arduino Uno R3 + ConfigurableFirmata 3.3: after pin.Toggle() the LED turns on, but pin.Read() still returns Low.

No code changes — per the agent instruction, this PR records an analysis of whether Toggle should work on this board.

Findings (src/devices/Arduino/ArduinoGpioControllerDriver.cs)

  • Toggle works, even without a prior Write. OpenPin seeds _pinValues[pin] = Low, and Toggle is implemented as Write(pin, !_pinValues[pin]), so the first toggle correctly drives the pin High.
  • Read on an output pin is broken. Read always delegates to _device.ReadDigitalPin, which returns the value sampled from the digital input port. ConfigurableFirmata only auto-reports DIGITAL_MESSAGE for ports that have at least one input-configured pin; on a pure-output port the cached port stays 0, so Read() returns Low regardless of what was last written.
  • This violates the documented GpioPin.Read() contract: "If the pin is configured as an output, this value is the last value written to the pin."

Suggested fix (not applied here)

Short-circuit Read for output pins to return the cached written value:

protected override PinValue Read(int pinNumber)
{
    if (_pinModes.TryGetValue(pinNumber, out var mode) && mode == PinMode.Output)
    {
        return _pinValues[pinNumber];
    }

    _pinValues[pinNumber] = _device.ReadDigitalPin(pinNumber);
    return _pinValues[pinNumber];
}

Happy to follow up with a separate PR implementing this if reviewers agree.

Copilot AI linked an issue Jun 2, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix GPIO read issue in ConfigurableFirmata on Arduino Uno Analysis comment: Arduino GPIO Read on output pins returns stale Low Jun 2, 2026
Copilot AI requested a review from raffaeler June 2, 2026 07:31
@raffaeler
Copy link
Copy Markdown
Contributor

@pgrawehr you may like this or not, decision is yours ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Arduino Gpio Read Not Work

2 participants