Pins as inputs and outputs: pinMode(), digitalWrite(), digitalRead() and the pin constants.
More...
|
| | 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.
|
|
|
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.
|
Pins as inputs and outputs: pinMode(), digitalWrite(), digitalRead() and the pin constants.
◆ pinMode()
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
-
| pin | Pin number (2 is D2) or name ("D2", "A0", LED_BUILTIN). |
| mode | INPUT, OUTPUT, INPUT_PULLUP or INPUT_PULLDOWN. |
- Exceptions
-
| ValueError | Unknown pin or mode, or an analogue-only pin (A3-A5). |
- Example
pinMode(2, INPUT_PULLUP)
pinMode(LED_BUILTIN, 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
-
| pin | Pin number (2 is D2) or name ("D2", "A0", LED_BUILTIN). |
| value | HIGH or LOW. |
- Exceptions
-
| ValueError | Unknown pin, or an analogue-only pin (A3-A5). |
◆ digitalRead()
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
-
| pin | Pin number (2 is D2) or name ("D2", "A0"). |
- Returns
- HIGH or LOW.
- Exceptions
-
| ValueError | Unknown pin, or an analogue-only pin (A3-A5). |