ArduinoCore-megaavr icon indicating copy to clipboard operation
ArduinoCore-megaavr copied to clipboard

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

Open drf5n opened this issue 8 months ago • 10 comments
trafficstars

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

drf5n avatar Mar 19 '25 12:03 drf5n