digitalWrite()
Description
Write a
HIGH
or a LOW
value to a digital pin.If the pin has been configured as an
OUTPUT
with pinMode()
, its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH
and 0V (ground) for LOW
.If the pin is configured as an
INPUT
, digitalWrite()
will enable (HIGH
) or disable (LOW
) the internal pull-up on the input pin. It is recommended to set the pinMode()
to INPUT_PULLUP
to enable the internal pull-up resistor. See the Digital Pins tutorial for more information.If you do not set the pin as an
OUTPUT
, and connect an LED to it, when calling digitalWrite(pin, HIGH)
, the LED may appear dim. Without explicitly setting pinMode()
, digitalWrite()
will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.Syntax
Use the following function to write a digital value to a pin:
digitalWrite(pin, value)
Parameters
The function admits the following parameters:
: the Arduino pin number to be controlled.pin
:value
orHIGH
LOW
Returns
The function returns nothing.
Example Code
Set the Arduino digital pin 13 (built-in LED) as an
OUTPUT
and toggles it by alternating between HIGH
and LOW
at one second pace.1void setup() {2 pinMode(13, OUTPUT); // sets the digital pin 13 as output3}4
5void loop() {6 digitalWrite(13, HIGH); // sets the digital pin 13 on7 delay(1000); // waits for a second8 digitalWrite(13, LOW); // sets the digital pin 13 off9 delay(1000); // waits for a second10}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs.
See also
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.