How long should digitalWrite() take?

My first program & hardware project consists of 2 7-segment displays connected to
Arduino digital pins 0-3.

The program just counts from 0-15, and displays the count 0-F on the displays.

The displays have a blanking input, which I've connected to digital pin 4.
However, it seems to take almost one second for the digital pin 4 to change from low to high.
This doesn't make sense, have I overlooked something about the Arduino architecture?
When Flash() is called, it seems like it's taking way too long to execute

Here is the code

int outD = 3;                // LED connected to digital pin 3  -> 7 SEGMENT DIPSLAY PIN #12 (D)
int outC = 2;                // LED connected to digital pin 2  -> 7 SEGMENT DISPLAY PIN #13(C)
int outB = 1;                // LED connected to digital pin 1  -> 7 SEGMENT DISPLAY PIN #2 (B)
int outA = 0;                // LED connected to digital pin 0  -> 7 SEGMENT DISPLAY PIN #3 (A)
int blankLEDS = 4;           // setting this to 0 will enable the displays, 1 will turn it off, PWM will vary intensity

void setup()                    // run once, when the sketch starts
{
  pinMode(outD, OUTPUT);      // sets the digital pin as output
  pinMode(outC, OUTPUT);      // sets the digital pin as output
  pinMode(outB, OUTPUT);      // sets the digital pin as output
  pinMode(outA, OUTPUT);      // sets the digital pin as output
  pinMode(blankLEDS, OUTPUT);
  digitalWrite(blankLEDS, LOW);
  
  
}

void loop()                     // run over and over again
{
  Test2();
}

void Test2()
{
  int x;
  for(x=0; x< 16; x++)
  {
    digitalWrite(outD, x & 8 );   // sets the LED on
    digitalWrite(outC, x & 4 );   // sets the LED on
    digitalWrite(outB, x & 2 );   // sets the LED on
    digitalWrite(outA, x & 1);   // sets the LED on
  
    delay(200);                  
  }

  Flash(10);
}

void Flash(int numTimes)
{
  int x;
  for(x=0; x < numTimes; x++)
  {
    digitalWrite(blankLEDS, LOW);
    delay(10);
    digitalWrite(blankLEDS, HIGH);
  }
}

It takes around 2 microseconds for digitalWrite to change the state of a pin.

Pins 0 and 1 are used for the serial port so should be avoided for general IO on a standard arduino board.

Your description says pin 4 is an input but your code sets the pinMode as output.

It takes around 2 microseconds for digitalWrite to change the state of a pin.

Pins 0 and 1 are used for the serial port so should be avoided for general IO on a standard arduino board.

Your description says pin 4 is an input but your code sets the pinMode as output.

2 microseconds isn't what I'm seeing.

Executing Flash() takes almost a minute.

Pins 0 and 1 are used for the serial port so should be avoided for general IO on a standard arduino board.

Why should they be avoided for digital I/O, if they are labeled as digital pins?
The compiler should flag this as an error....

2 microseconds isn't what I'm seeing.

Executing Flash() takes almost a minute.

I'm willing to bet it does not take a minute for Flash() to execute. The problem is probably with the way you are interfacing to your displays. If you really want to figure out what is going on, troubleshoot your program. For example, turn on the LED when you enter flash and turn it off when you exit. I expect you'll see the LED turn on for around 0.1 s. If it turns on for a minute, there is something seriously wrong. You could also consider using serial to help you debug your program. Basically, all you know right now is that things aren't working as expected, but you have no idea what is at fault. Start decreasing the variables so you can systematically get to the source.

Why should they be avoided for digital I/O, if they are labeled as digital pins?
The compiler should flag this as an error....

The compiler isn't an "Arduino" compiler, it is a GCC compiler for AVRs. You shouldn't expect Arduino-specific warnings from a general AVR compiler. Furthermore, it's perfectly fine to use pins 0 and 1 as general digital I/O lines, but you can expect such usage to interfere with integrated hardware that expects those pins to be used in a different capacity. This may or may not cause problems (it would depend on the details of the integrated hardware).

  • Ben

2 microseconds isn't what I'm seeing.

Executing Flash() takes almost a minute.

I'm willing to bet it does not take a minute for Flash() to execute. The problem is probably with the way you are interfacing to your displays. If you really want to figure out what is going on, troubleshoot your program. For example, turn on the LED when you enter flash and turn it off when you exit. I expect you'll see the LED turn on for around 0.1 s. If it turns on for a minute, there is something seriously wrong. You could also consider using serial to help you debug your program. Basically, all you know right now is that things aren't working as expected, but you have no idea what is at fault. Start decreasing the variables so you can systematically get to the source.

Sorry for the confusion, the LEDS are not on for a minute.
Flash() takes much longer to execute than 20ms + 20 ms * 10 iterations (plus 2 calls to digitalWrite() * 10 iterations )

much longer -> approximately ~40 seconds- 1 minute

The for loop in Test2 runs as fast as I would expect, and I can easily see the 200ms delay between each iteration of the for loop

Flash() takes much longer to execute than 20ms + 20 ms * 10 iterations (plus 2 calls to digitalWrite() * 10 iterations )

much longer -> approximately ~40 seconds- 1 minute

This is the part I don't believe. That's why I suggested you should use the LED to determine the amount of time you spend in Flash().

Also, your problem might be arising from the fact Flash() is only pulsing the output high for maybe 100 - 200 nanoseconds each time. You should probably add a delay after setting the pin high.

  • Ben

This is the part I don't believe. That's why I suggested you should use the LED to determine the amount of time you spend in Flash().

Also, your problem might be arising from the fact Flash() is only pulsing the output high for maybe 100 - 200 nanoseconds each time. You should probably add a delay after setting the pin high.

  • Ben

Actually, I do have a delay after setting the pin high.
I had deleted some text after I pasted my code in into that post.

Here is the program in its entirety

int outD = 3;                // LED connected to digital pin 3  -> 7 SEGMENT DIPSLAY PIN #12 (D)
int outC = 2;                // LED connected to digital pin 2  -> 7 SEGMENT DISPLAY PIN #13(C)
int outB = 1;                // LED connected to digital pin 1  -> 7 SEGMENT DISPLAY PIN #2 (B)
int outA = 0;                // LED connected to digital pin 0  -> 7 SEGMENT DISPLAY PIN #3 (A)
int blankLEDS = 9;           // setting this to 0 will enable the displays, 1 will turn it off, PWM will vary intensity
int monitorLED = 13;

void setup()                    // run once, when the sketch starts
{
  pinMode(outD, OUTPUT);      // sets the digital pin as output
  pinMode(outC, OUTPUT);      // sets the digital pin as output
  pinMode(outB, OUTPUT);      // sets the digital pin as output
  pinMode(outA, OUTPUT);      // sets the digital pin as output
  pinMode(blankLEDS, OUTPUT);

  pinMode(monitorLED, OUTPUT); // used to monitor when the loop is entered/exited in Flash()

  
}

void loop()                     // run over and over again
{
  Test2();
}

void Test2()
{
  int x;
  for(x=0; x< 16; x++)
  {
    digitalWrite(outD, x & 8 );   // sets the LED on
    digitalWrite(outC, x & 4 );   // sets the LED on
    digitalWrite(outB, x & 2 );   // sets the LED on
    digitalWrite(outA, x & 1);   // sets the LED on
  
    delay(100);                  // waits for a second
  }

  Flash(10);
}

void Flash(unsigned char numTimes)
{
  int x;
  
  digitalWrite(monitorLED, LOW);
  
  for(x=0; x < numTimes; x++)
  {
    digitalWrite(blankLEDS, LOW);
    delay(100);
    digitalWrite(blankLEDS, HIGH);
    delay(100);
  }
  digitalWrite(monitorLED, HIGH);
}

I decided to use an separate LED to monitor Flash(), since the program was already writing to the other displays.
Now the program seems to be working normally, not sure why I had the anomaly before, but I see the "monitor LED"
go on & off about every 3 seconds.