Loading...
Searching...
No Matches
Digital I/O

Pins as inputs and outputs: pinMode(), digitalWrite(), digitalRead() and the pin constants. More...

Functions

 pinMode (pin, mode)
 Sets a pin up as an input or an output.
 digitalWrite (pin, value)
 Sets an output pin high or low.
 digitalRead (pin)
 Reads a pin's level.

Variables

int HIGH = 1
 Pin level: high (3.3 V).
int LOW = 0
 Pin level: low (0 V).
int INPUT = 0
 pinMode(): a digital input with no pull resistor.
int OUTPUT = 1
 pinMode(): a digital output.
int INPUT_PULLUP = 2
 pinMode(): a digital input pulled up to 3.3 V.
int INPUT_PULLDOWN = 3
 pinMode(): a digital input pulled down to 0 V.
str LED_BUILTIN = "LED"
 The on-board green LED.

Detailed Description

Pins as inputs and outputs: pinMode(), digitalWrite(), digitalRead() and the pin constants.

Function Documentation

◆ pinMode()

pinMode ( pin,
mode )

Sets a pin up as an input or an output.

INPUT has no pull resistor, so an unconnected pin reads at random. For a button to 3.3 V use INPUT_PULLDOWN (pressed reads HIGH); for a button to GND use INPUT_PULLUP (pressed reads LOW). A0-A2 can be digital pins too. A pin on analogWrite() or tone() comes off PWM.

Parameters
pinPin number (2 is D2) or name ("D2", "A0", LED_BUILTIN).
modeINPUT, OUTPUT, INPUT_PULLUP or INPUT_PULLDOWN.
Exceptions
ValueErrorUnknown pin or mode, or an analogue-only pin (A3-A5).
Example
pinMode(2, INPUT_PULLUP) # D2 reads HIGH until a button pulls it to GND
pinMode(LED_BUILTIN, OUTPUT) # the LED's pin as an output

◆ digitalWrite()

digitalWrite ( pin,
value )

Sets an output pin high or low.

A pin that is not an output yet (or is on analogWrite()) becomes one, so pinMode() is optional here. HIGH is 3.3 V; any true value counts as HIGH.

Parameters
pinPin number (2 is D2) or name ("D2", "A0", LED_BUILTIN).
valueHIGH or LOW.
Exceptions
ValueErrorUnknown pin, or an analogue-only pin (A3-A5).

◆ digitalRead()

digitalRead ( pin)

Reads a pin's level.

A pin that has not been set up with pinMode() becomes an INPUT, with no pull resistor; call pinMode() first for a button. An OUTPUT pin reads the level it is set to.

Parameters
pinPin number (2 is D2) or name ("D2", "A0").
Returns
HIGH or LOW.
Exceptions
ValueErrorUnknown pin, or an analogue-only pin (A3-A5).