My question is simple. Does Arduino set the digitalwrite() the moment it is called or at the end of the while loop. E.G.

int ledPin = 13;               
void setup()
  pinMode(ledPin, OUTPUT);      

void loop()
{for(int i=0; i<10; i++){
  digitalWrite(ledPin, HIGH);   
  digitalWrite(ledPin, LOW);    
  delay(1000);      }           

3 Answers 3


A key point of the Arduino 'language' is every statement is completed before the next one can begin.

A simple statement has a ';' at the end of it.
Or followed by ')' instead of ';' in for (...; ...; statement) loop.

So digitalWrite() will set the pin, HIGH or LOW, before it completes and allows the next statement, delay(...) to begin.

Side note:
Actually digitalWrite() takes a small amount of time to do its work, so it is not exactly 'the moment' it is called. digitalWrite() is pretty quick, a few millionths of a second, which is usually near enough to be practically 'the moment' for most applications.

  • Arduino 'language' a.k.a. C++ (would add it in brackets to the answer but too short for edit)
    – user2973
    Aug 24, 2014 at 18:35

Arduino sets the pin at the moment digitalWrite() is called.

  • 3
    To be extremely pedantic, it sets the IO pin state a short time into the processing time of the function call, which can be important if you're trying to run things very fast (digitalWrite() is actually quite slow, in comparison to writing pin control registers directly). It still sets the pin within the execution of the digitalWrite() function call, in any event. Aug 24, 2014 at 0:50

The condition


will be checked immediately upon entering the "for" loop, and again on every iteration of that loop. It will be the first code executed on each iteration. One form or another of the digitalWrite() will be called next, followed by the delay().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.