0

Will the Arduino IDE interpret digitalWrite(13,x), where x>0, as digitalWrite(13,HIGH)? And will it interpret digitalWrite(13,x), where x<=0, as digitalWrite(13,LOW)?

  • 2
    0 is low, everything else is HIGH – Juraj Jun 30 '18 at 13:27
  • but some joker could make an arduino core where LOW is defined as 2. the AVR code for digitalWrite and digitalRead would work. it doesn't assume that LOW is 0. so your code shouldn't do that either. (but in my sketches I often assume that LOW is 0). – Juraj Jul 1 '18 at 10:32
  • the esp8266 core digitalRead and digitalWrite functions ignore LOW and HIGH and use if (val) and return the state of the register bit – Juraj Jul 1 '18 at 10:38
2

Every other value than 0 or LOW will result in setting the pin to HIGH. In the following I explain why:

The function digitalWrite() can be seen in the file hardware/arduino/avr/cores/arduino/wiring_digital.c. The important lines here are:

if (val == LOW) {
    *out &= ~bit;
} else {
    *out |= bit;
}

As you can see, it checks, if the given value (from the parameter) is equal to LOW and if so will set the corresponding pin to 0. In every other case it will set the pin to 1. The definition of LOW can be found in arduino/hardware/arduino/avr/cores/arduino/Arduino.h:

#define HIGH 0x1
#define LOW  0x0

So the code checks, if the value is equal to 0 and sets the pin also to 0. Every other value will result in a 1 (meaning HIGH). Also have a look at the definition of digitalWrite():

void digitalWrite(uint8_t pin, uint8_t val)

The value is an unsigned type, so every signed type will be casted to an unsigned type. A negative number would result in a positive number inside the function.

| improve this answer | |
  • Just nitpicking: Also False and anything other, which evaluates as 0, works here :) (And it is common to use boolean variables/expression as the val) – gilhad Jun 30 '18 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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