How to set output HIGH without pulse?

This will also stop wanted "LOWs".

Also a work around.
digitalWrite should do what it sais it does: set the Output Register, not more, not less.

If you add an external pullup resistor, as I said, that output won't ever be pulled low unless you do it issuing a digitalWrite(pin, LOW) because as soon as the circuit is powered the pullup will do the job (Ohm's law rules).
This way you don't need to mess up with registers, so I still can't get it, sorry: what problem do you see in this method? To make sure, just use an oscilloscope connected to that Arduino pin and do your tests to see if it works, and let me know, I'm curious.

In setup() I want to transition the output from high impedance state to HIGH.
Later on in loop() I will set the output to LOW to start the other system.
I don't want the other system to start before the right time.

I understand, but I wonder why: if the pin is in high impedance the other device internal pullup resistor will prevent to go to a low state (I think it has been added on purpose, don't you think?). If you want to absolutely make sure the pin won't go LOW before you take control of it, just use a regular external pullup resistor (remember it'll be in parallel with the other device pullup, so a higher value is recommended).
I repeat, I don't understand what/where the problem is in using just an external pullup. Have you ever tried that before asking?

I still can't get it. With an external pullup resistor you'll be sure the signal will be HIGH when the pin is in high impedance, then in setup() you can write HIGH as first statement (or, better, second, after the pinMode) then in loop() you can put it to LOW whenever you need that.
Again: are you absolutely sure, using an external pullup and the above statements, the level will be brought to LOW even for a small time when powering up, because you have tested with an oscilloscope, or are you just imagining that scenario?

An external pull up resistor won't do what the pull up resistor in the other system doesn't already do. Unless the value is that low that the output of my board gets damaged. The other system is something altready existing, I can't change it.

I suspect that the problem is that with an external pull-up and switching the pin to output, the pin will go low till it's written high.

1 Like

@stitech, unless you don't want to do it in hardware, how about a small capacitor between pin and GND to keep the output high while switching the pin to output.

You think on another level than docdoc, please read post #8 and post #1 again.

Well, it's not what even TinkerCAD and Wokwy show (I can't currently test that in "real life" yet), using this code with a (virtual) multimeter and a pullup resistor:

void setup()
{
  Serial.begin(115200);
  Serial.println("START");
  delay(5000);
  Serial.println("write");
  digitalWrite(10, HIGH);
  delay(5000);
  Serial.println("mode");
  pinMode(10, OUTPUT);
  
}

void loop()
{

}

I constantly read "5.00 V" on any step.

Any solution you could think about, you still need an oscilloscope to test it and make sure you won't anytime get a low tension. But often the best solution is the easier one, so if I were you I'd give the simple "external pullup resistor" solution a try, and get some "real life" data, instead of racking your brains with more complex things. Just my two cents, now it's up to you. Cheers!

I have to correct myself: you don't have to do pinMode first and then digitalWrite but the opposite (like you can see in the previous example code).

Sorry, I was answering this:

Anyway, I've got the point that this is more an exercise in demonstrating an inconsistency in the IDE.

Just to get a concrete use case out of this which could be useful to illustrate any issue report: If your other system is say the gate of (logic level) PMOS being driven by PWM then you would need the external pullup to ensure that the PMOS is off during system start. However, during the operation, you would want either a strong LOW (= PMOS ON) or a strong HIGH (= PMOS OFF). In this or a similar case, it looks like you would, with the Nano Every behaviour, have to wait until you wanted to switch it on for the first time before issuing the pinMode().

Electronics: any capacitor between output and ground will initially draw a current that is limited by the resistance of the MOSFET. How small must the capacitor be not to exceed the current limit of the output while and after the MOSFET is going through the linear phase? Will it be enough to survive the time between pinMode(pin, OUTPUT) and digitalWrite(pin, HIGH)? I do know that executing a digitalWrite takes more than 4 μs.

If it works, it's still a work around for something that should be possible with a well written Arduino IDE.

You are suggesting another work around:

if (condition1) pinMode(pin, OUTPUT);  // switch system on
if (condition2) pinMode(pin, INPUT);   // switch system off
1 Like

This doesn't work for high frequency PWM because you want a strong HIGH to switch the PMOS off, not simply a gentle float towards HIGH via the external resistor. That is why I chose the example.

Once pinMode() has been set to output once, then you are of course free to write HIGH or LOW to the pin without it generating any spurious pulses.

1 Like

In another thread I was pointed at ArduinoCore-API at GitHub. I did some browsing and found the section 'issues'. This issue has been raised before, in 2020.

I added a comment, and hope it will get some attention now.

1 Like

Oops. I raised an issue myself:

Sample code:

// See https://docs.arduino.cc/learn/microcontrollers/digital-pins/
void setup() {
  const int pin = 2;
  digitalWrite(pin, HIGH); // enable pullup
  pinMode(pin, OUTPUT); // switch to OUTPUT
  Serial.begin(115200);
  Serial.println(digitalRead(pin) == HIGH ? "HIGH" : "LOW" );
  digitalWrite(pin, HIGH); 
  Serial.println(digitalRead(pin) == HIGH ? "HIGH" : "LOW" );
}
void loop() {}

There is a pull request which I think would fix the issue : change digitalWrite() by thorv · Pull Request #133 · arduino/ArduinoCore-megaavr · GitHub although it was raised for different but related issue.

1 Like

Currently on 4809, the digitalWrite(P, HIGH) turns on the input pullup if the pin is in input mode, but does NOT set the port output register. So when you change the output mode with pinMode(), the output register bit is still 0, and the pin will be set to zero as well.
While having the digitalWrite() switch to input pullup mode is questionable (and "only for backward compatibility"), I can't think of any arguments for it NOT setting the output port register.

1 Like