> For the complete documentation index, see [llms.txt](https://monasheng.gitbook.io/zerotohero/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monasheng.gitbook.io/zerotohero/arduino-zero-to-hero/core-skills/input-output-pins/digital-pins/digitalwrite.md).

# digitalWrite()

{% embed url="<https://youtu.be/Hwji3e81oCo>" %}

MCU digital pins can be used to programmatically drive `HIGH` and `LOW` voltages in a circuit. Usually, these are used for *low-load* devices - e.g. status LEDs - or as signals to inform other pieces of electrical equipment - e.g. a motor controller might take a digital signal as input to inform direction.

## Syntax

### `pinMode()`

To drive a digital pin, you must first set it to `OUTPUT` mode.  This is typically achieved in the `setup()`  function, as it only needs to be done once per boot. The `pinMode()` function expects a pin number and mode as arguments:

```arduino
void setup() {
  // Set PIN 2 as an output.
  pinMode(2, OUTPUT);
}
```

{% embed url="<https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/>" %}

### `digitalWrite()`

After the mode is set, the pin can be driven to a `HIGH` or `LOW` state. The `digitalWrite()` function expects a pin number and state as arguments:

```arduino
void loop() {
  // Drive PIN 2 to a HIGH (5V) state
  digitalWrite(2, HIGH);
  // Wait for 1 second
  delay(1000);
  // Drive PIN 2 to a LOW (0V) state
  digitalWrite(2, LOW);
  // Wait for 1 second
  delay(1000);
}
```

You can `digitalWrite()` in the `setup()` function to enforce an initial/boot behaviour, but the state is usually changed programmatically in the `loop()` function - either as a function of time/on a cycle, or in response to some measured input.

{% embed url="<https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/>" %}

## Complete Sample Code

{% code overflow="wrap" %}

```arduino
void setup() {
  // Set PIN 2 as an output.
  pinMode(2, OUTPUT);
  // Start it in a HIGH (5V) state
  digitalWrite(2, HIGH);
}

void loop() {
  // Drive PIN 2 to a HIGH (5V) state
  digitalWrite(2, HIGH);
  // Wait for 1 second
  delay(1000);
  // Drive PIN 2 to a LOW (0V) state
  digitalWrite(2, LOW);
  // Wait for 1 second
  delay(1000);
}
```

{% endcode %}

<div data-full-width="false"><figure><img src="/files/C1ZVjCbkLA3AFkLaOy9I" alt=""><figcaption></figcaption></figure></div>

## Practical Considerations

### Output Voltage

Most MCUs have onboard regulators, and will reliably supply near-enough to nominal output voltage ($$V\_\text{nom}=5;\text{V}$$ on Arduino) on digitals pins provided:

1. The input voltage to the MCU is $$\geq V\_\text{nom}$$, and,
2. The load on the MCU - from digital pins and/or any fixed supply pins - is small.

Where the input voltage to the MCU drops (e.g. from a depleting battery), or the output load becomes significant (e.g. when using bright LEDs), changes in the digital pin output voltage may be seen. These are usually small - $$<0.5;\text{V}$$ - and can typically be ignored for all practical purposes.
