arduino / arduino/ArduinoCore-megaavr

The pinMode(pin,OUTPUT) can't initialize the pin in HIGH state on a Nano Every

Open
#143 10 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
111
Forks
65
PR merge metrics
No merged PRs in 30d

Description

https://github.com/arduino/ArduinoCore-megaavr/blob/5e639ee40afa693354d3d056ba7fb795a8948c11/cores/arduino/wiring_digital.c#L33-L47

On an Uno, you can use these lines to switch directly from INPUT_PULLUP to HIGH:

```
digitalWrite(pin,HIGH); // enable pullup
pinMode(pin,OUTPUT); // switch to OUTPUT HIGH
```

On a Nano Every, those commands switch instead to an OUTPUT LOW

See the discussion at:

https://forum.arduino.cc/t/how-to-set-output-high-without-pulse/1364909

The documentation at https://docs.arduino.cc/learn/microcontrollers/digital-pins/ says it should switch directly to HIGH:

>Consequently, a pin that is configured to have pullup resistors turned on when the pin is an INPUT, will have the pin configured as HIGH if the pin is then switched to an OUTPUT with pinMode().

Per this chunk of code, it looks like half of the issue was considered:

https://github.com/arduino/ArduinoCore-megaavr/blob/5e639ee40afa693354d3d056ba7fb795a8948c11/cores/arduino/wiring_digital.c#L162-L192

But the pinMode code is where the problem happens. Instead of always assuming LOW, it should use the state on the pullup to set the level before enabling the OUTPUT direction in order to match the documentation and the behavior on the '328 and '2560.

See also #86

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in cores/arduino/wiring_digital.c, especially the pinMode code at lines 33-47 and the related logic at lines 162-192. Reproduce the INPUT_PULLUP-to-OUTPUT sequence on a Nano Every and compare it with the documented Uno behavior. Done means the pin becomes OUTPUT HIGH without first switching LOW, while preserving the behavior on the other mentioned boards.

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
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.