Home

How to Write to a Digital Output

Arduino Uno and Arduino Nano boards (and their aftermarket clones) both have fourteen Digital I/O (aka GPIO) pins, numbered 0 to 13. These GPIO pins are sometimes referred to as D0 to D13, but you cannot depend on these names to be predefined.

An Arduino Uno oriented with the USB socket to the left has the Digital I/O pins along the top edge of the board, numbered 0 to 13 from right to left. The Arduino Nano has a similar pinout, except with D0 and D1 reversed, RST and GND pins added between D0 and D2, and D13 moved to the other edge of board.

Arduino boards often have an LED connected to D13. The constant LED_BUILTIN evaluates to 13.

Use pinMode(n, OUTPUT); to configure a digital pin as an output pin.
Use digitalWrite(n, state); to set the state of a digital output pin.
Use digitalRead(n); to get the last state written to a digital output pin.

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  // The next three function calls are equivalent
  digitalWrite(LED_BUILTIN, HIGH);   // Turn LED on
  digitalWrite(LED_BUILTIN, 1);      // Turn LED on
  digitalWrite(LED_BUILTIN, true);   // Turn LED on
  
  // The next three function calls are equivalent
  digitalWrite(LED_BUILTIN, LOW);    // Turn LED off
  digitalWrite(LED_BUILTIN, 0);      // Turn LED off
  digitalWrite(LED_BUILTIN, false);  // Turn LED off
  delay(500);

  // Toggle a digital output
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

Home


Questions or comments to phil@munts.net