-
Notifications
You must be signed in to change notification settings - Fork 5
Pin Modes
luni64 edited this page Apr 4, 2020
·
1 revision
To setup digital pins for one of the various input or output modes you can use the pinMode(pin, mode)
function which takes the pin number (as given in the pinout card) as first and the mode as second parameter. The following modes are available on all Teensy models:
Mode | Description |
---|---|
OUTPUT | Low impedance output mode. Pin can be set or cleared using digitalWriteFast |
OUTPUT_OPENDRAIN | Operate the pin in open drain mode. Please note this Teensy specific quirk of open-drain mode. |
INPUT | High impedance input mode. Pin can be read with digitalReadFast. This mode is set per default. Reading an open pin gives undefined values. |
INPUT_PULLUP | Adds a pull up resistor (~10k?) to the pin. Open pin reads HIGH |
INPUT_PULLDOWN | Adds a pull down resistor (~10k?) to the pin. Open pin reads LOW |
INPUT_DISABLE | Disables the pin |
Here an example demonstrating how to read and write digital pins. It also shows the effect of PULLUP and PULLDOWN input modes on open pins. (Examples can be downloaded here)
constexpr int inputPin = 0;
void setup()
{
while (!Serial && millis() < 1000) {}
pinMode(LED_BUILTIN, OUTPUT);
pinMode(inputPin, INPUT_PULLUP);
Serial.println("Using INPUT_PULLUP:");
printDigitalVal(inputPin, digitalReadFast(inputPin));
pinMode(inputPin, INPUT_PULLDOWN);
Serial.println("\nUsing INPUT_PULLDOWN:");
printDigitalVal(inputPin, digitalReadFast(inputPin));
}
void loop()
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); // toggle pin
delay(150);
}
// Helpers ----------------------------------------------------------
void printDigitalVal(int pin, int val)
{
Serial.printf("Pin %d value: %s\n", pin, val == 0 ? "LOW" : "HIGH");
}
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.