# Is digitalWrite always guaranteed to work?

**URL:** <https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639>\
**Category:** Programming\
**Created:** [August 20, 2015, 11:23am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639 "2015-08-20T11:23:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGrovesY](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@TheGrovesY](https://forum.arduino.cc/u/TheGrovesY)\
**Post date:** [August 20, 2015, 11:23am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/1 "2015-08-20T11:23:48Z")

</div>

Hi all,

I'm not sure if this is really a coding or electronics question but...

is calling the digitalWrite function guaranteed to always write to the pin correctly?

The reason i ask is the following:

I have a test rig project which toggles a digital output pin (13) of the arduino using the digitalWrite function. It toggles it between high and low with a 500ms delay between each.

The output gets fed into a temperature controls input (which reads a value between 0-5v). The output of the temp controller switches based on the input it receives, when its input is less than 2.5v its output goes low, and it goes high when the input goes above this value.

To ensure that the temp controller is reacting as expected the temp controllers output then gets fed back into my arduino and gets read using the analogRead function. and reports if there is a problem.

On two occations now the arduino has reported that the temp controller has failed, however when I measure the voltage of the arduino when it should have been high the voltage is zero (or there abouts). Is it possible that the digitalWrite can fail?

Code attached.

Thanks,

[TempSwtichTest.ino](https://forum.arduino.cc/uploads/short-url/q4ROr28OuFB6z0aBbpLVk4RJ1f8.ino) (2.96 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 20, 2015, 11:26am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/2 "2015-08-20T11:26:38Z")

</div>

Why can't you just POST the code? \>:(

```cpp
int OutputPin = 13;
int RelayPin = 2;
int ADCinPin = A5;
int val = 0;          // variable to store the value read
unsigned int ErrorCount = 0;
unsigned long timer = 0; // Time to see how long its been running and time errors
unsigned long errorTime = 0; // How long before error recovered

char errorString[40];

const char compile_date[] = __DATE__" " __TIME__ ; // Macro for date and time of compile

#define UnitOffTime 2 // Seconds to turn unit off
#define UnitOnTime  7 // Turn unit on and wait for specified number of seconds

void setup() {
  timer = millis();
  // put your setup code here, to run once:
  pinMode(OutputPin,  OUTPUT);
  pinMode(RelayPin,  OUTPUT);
  
  Serial.begin(9600);  //  setup serial

  powerCycleUnit(); // Start unit up
}

void loop() {
  
  //##### Set the output HIGH, wait a second then read to confirm
  digitalWrite(OutputPin, HIGH);  // set the output ON
  delay(500);
  val = analogRead(ADCinPin);      // read the input pin
  if (val < 500)
  {
    errorTime = millis(); // get the current up time so we can see how long it takes to recover
    // Failed to go high as expected
    errorFound(true);
  }
  
  //##### Set the output LOW, wait a second then read to confirm
  digitalWrite(OutputPin, LOW);    // sets the output off
  delay(500);
  val = analogRead(ADCinPin);      // read the input pin
  if (val > 500)
  {
    errorTime = millis(); // get the current up time so we can see how long it takes to recover
    // Failed to go low as expected
    errorFound(false);
  }
}

void errorFound(bool highError)
{
  bool error = true;  // Used to jump out of the retry loop
  unsigned long recoverTime = 0;
  ErrorCount += 1;    // Increment error count
  
  if (highError)
    sprintf(errorString, "High Error. Up time = %lu (%d)", errorTime, ErrorCount);
  else
    sprintf(errorString, "Low  Error. Up time = %lu (%d)", errorTime, ErrorCount);

  Serial.println(errorString);
  
  // Sit in a loop until output is correct
  while (error)
  {
    delay(500); // Wait before retrying
    val = analogRead(ADCinPin);      // read the input pin
    if (((highError == true) && (val > 500))
    || ((highError == false) && (val < 500)))
    {
      recoverTime = millis() - errorTime;
      // now correct so display recovery time and continue as normal
      sprintf(errorString, "Fixed. Recover time = %lu (%d)", recoverTime, recoverTime/500);
      Serial.println(errorString);
      error = false;
    }
  }
}

void powerCycleUnit()
{
  // Turn unit off for 2 seconds
  Turn_Unit_Off();
  delaySeconds(UnitOffTime);
  
  //Turn unit on and wait for 10 seconds to ensure units has enough time to start
  Turn_Unit_On();
  delaySeconds(UnitOnTime);
}

// Turn unit on via relay
void Turn_Unit_On()
{
  digitalWrite(RelayPin, LOW);  // LOW will turn the unit on
}

// Turn unit off via relay
void Turn_Unit_Off()
{
  digitalWrite(RelayPin, HIGH);  // HIGH will turn the unit off
}

// Delay for specified number of seconds
void delaySeconds(int seconds)
{
  delay(1000 * seconds);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 20, 2015, 11:27am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/3 "2015-08-20T11:27:50Z")

</div>

```cpp
errorString

```

Is that big enough?

---

<div class="post-metadata">

**Author:** ![holmes4](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holmes4/32/215302_2.png) [@holmes4](https://forum.arduino.cc/u/holmes4)\
**Post date:** [August 20, 2015, 11:28am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/4 "2015-08-20T11:28:21Z")

</div>

> is calling the digitalWrite function guaranteed to always write to the pin correctly?

Yes

Mark

---

<div class="post-metadata">

**Author:** ![C-F-K](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@C-F-K](https://forum.arduino.cc/u/C-F-K)\
**Post date:** [August 20, 2015, 11:40am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/5 "2015-08-20T11:40:22Z")

</div>

To my understanding, the digitalWrite() can't fail. The firmware actually toggles the pin to the selected value, then read the active value to see if it's correct.

But follow up question: What arduino board are you using? The uno has a LED attached to pin 13, so you can see the state of pin 13.  
Also, are your grounds connected? Do you have a schematic of your set-up?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 20, 2015, 11:44am UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/6 "2015-08-20T11:44:21Z")

</div>

The pin is not guaranteed to write to the relay correctly. 🙂 Show us your circuit, because relay connections are notorious for causing interference to the microprocessor.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:26pm UTC](https://forum.arduino.cc/t/is-digitalwrite-always-guaranteed-to-work/330639/7 "2021-05-05T21:26:39Z")

</div>


