Input/Output
pinMode()
pinMode()
configures the specified pin to behave either as an input (with or without an internal weak pull-up or pull-down resistor), or an output.
// PROTOTYPE
void pinMode(uint16_t pin, PinMode mode)
// EXAMPLE USAGE
int button = D0; // button is connected to D0
int LED = D1; // LED is connected to D1
void setup()
{
pinMode(LED, OUTPUT); // sets pin as output
pinMode(button, INPUT_PULLDOWN); // sets pin as input
}
void loop()
{
// blink the LED as long as the button is pressed
while(digitalRead(button) == HIGH) {
digitalWrite(LED, HIGH); // sets the LED on
delay(200); // waits for 200mS
digitalWrite(LED, LOW); // sets the LED off
delay(200); // waits for 200mS
}
}
pinMode()
takes two arguments:
pin
: the pin you want to set the mode of (A0, A1, D0, D1, TX, RX, etc.). The typepin_t
can be used instead ofuint16_t
to make it more obvious that the code accepts a pin number in your code.mode
: the mode to set to pin to:INPUT
digital input (the default at power-up)INPUT_PULLUP
digital input with a pull-up resistor to 3V3INPUT_PULLDOWN
digital input with a pull-down to GNDOUTPUT
an output (push-pull)OUTPUT_OPEN_DRAIN
an open-drain or open-collector output. HIGH (1) leaves the output in high impedance state, LOW (0) pulls the output low. Typically used with an external pull-up resistor to allow any of multiple devices to set the value low safely.
You do not need to set the pinMode()
to read an analog value using analogRead
as the pin will automatically be set to the correct mode when analogRead is called.
When porting code from Arudino, pin numbers are numbered (0, 1, 2, ...) in Arduino code. Pin D0 has a value of 0, but it's best to use Particle pin names like D0 instead of just 0. This is especially true as the numeric value of A0 varies depending on the device and how many digital pins it has. For example, on the Argon, A0 is 19 but on the Photon it's 10.
Gen 3 Devices (nRF52) (B-Series SoM, Tracker SoM, Tracker One, Boron, Argon, and E404X):
Make sure the signal does not exceed 3.3V. Gen 3 devices (Tracker SoM as well as Argon, Boron, Xenon, and the B-Series SoM) are not 5V tolerant in any mode (digital or analog).
INPUT_PULLUP
andINPUT_PULLDOWN
are approximately 13K on Gen 3 devices.
If you are using the Particle Ethernet FeatherWing you cannot use the pins for GPIO as they are used for the Ethernet interface:
Argon, Boron, Xenon | B-Series SoM | Ethernet FeatherWing Pin |
---|---|---|
MISO | MISO | SPI MISO |
MOSI | MOSI | SPI MOSI |
SCK | SCK | SPI SCK |
D3 | A7 | nRESET |
D4 | D22 | nINTERRUPT |
D5 | D8 | nCHIP SELECT |
When using the FeatherWing Gen 3 devices (Argon, Boron, Xenon), pins D3, D4, and D5 are reserved for Ethernet control pins (reset, interrupt, and chip select).
When using Ethernet with the Boron SoM, pins A7, D22, and D8 are reserved for the Ethernet control pins (reset, interrupt, and chip select).
By default, on the B-Series SoM, the Tinker application firmware enables the use of the bq24195 PMIC and MAX17043 fuel gauge. This in turn uses I2C (D0 and D1) and pin A6 (PM_INT). If you are not using the PMIC and fuel gauge and with to use these pins for other purposes, be sure to disable system power configuration. This setting is persistent, so you may want to disable it with your manufacturing firmware only.
void disable()
{
SystemPowerConfiguration conf;
conf.feature(SystemPowerFeature::DISABLE);
System.setPowerConfiguration(conf);
}
STARTUP(disable());
B-Series SoM | Power Manager Usage |
---|---|
D0 | I2C SDA |
D1 | I2C SCL |
A6 | PM_INT (power manager interrupt) |
P2 and Photon 2 Devices (RTL872x):
Make sure the signal does not exceed 3.3V. The P2 and Photon 2 are not 5V tolerant in any mode (digital or analog).
INPUT_PULLUP
andINPUT_PULLDOWN
vary by pin and can be approximately 2.1K, 22K, or 42K depending on the pin.- Pins A0, A1, D2, D3, D4, D5, D10, S0, S1, S2 are 2.1K
- Pins D0, D1, S4, S5, S6 are 22K
- Pins A2, A5, D6, TX, RX are 42K
On the P2, pins S4, S5, S6 do not support pull-up or pull-down in HIBERNATE sleep mode. Use an external pull resistor if this is required.
On the P2, make sure you do not hold down pins D8 or D7 at boot as this will cause the MCU to enter download or test mode and will not operate correctly.
On the P2, Pin RGBR (PA[30]) has a 10K hardware pull-up in the module because it's a trap pin that controls the behavior of the internal 1.1V regulator. This does not affect the RGB LED but could affect your design if you are repurposing this pin as GPIO. You must not hold this pin low at boot.
If you are using the Particle Ethernet FeatherWing you cannot use the pins for GPIO as they are used for the Ethernet interface:
Photon 2, P2 | Ethernet FeatherWing Pin |
---|---|
MISO | SPI MISO |
MOSI | SPI MOSI |
SCK | SPI SCK |
D3 | nRESET |
D4 | nINTERRUPT |
D5 | nCHIP SELECT |
When using the Ethernet FeatherWing with the Photon 2, pins D3, D4, and D5 are reserved for Ethernet control pins (reset, interrupt, and chip select).
Gen 2 Devices (STM32) (E-Series, Electron, Photon, and P2; does not include E404X):
- When using
INPUT_PULLUP
orINPUT_PULLDOWN
make sure a high level signal does not exceed 3.3V. INPUT_PULLUP
does not work as expected on TX on the P1, Electron, and E-Series and should not be used.INPUT_PULLDOWN
does not work as expected on D0 and D1 on the P1 because the P1 module has hardware pull-up resistors on these pins.INPUT_PULLUP
andINPUT_PULLDOWN
are approximately 40K on Gen 2 devices- On the P1, D0 and D1 have 2.1K hardware pull-up resistors to 3V3.
On Gen 2 devices (Photon, P1, Electron, and E-Series), GPIO pins are 5V tolerant if all of these conditions are met:
- Digital input mode (INPUT) (the ADC is not 5V tolerant)
- Not using INPUT_PULLDOWN or INPUT_PULLUP (internal pull is not 5V tolerant)
- Not using pins A3 or A6 (the DAC pins are not 5V tolerant, even in INPUT mode)
- Not using pins D0 and D1 on the P1 only as there is a pull-up to 3V3 on the P1 module only
Also beware when using pins D3, D5, D6, and D7 as OUTPUT controlling external devices on Gen 2 devices. After reset, these pins will be briefly taken over for JTAG/SWD, before being restored to the default high-impedance INPUT state during boot.
- D3, D5, and D7 are pulled high with a pull-up
- D6 is pulled low with a pull-down
- D4 is left floating
The brief change in state (especially when connected to a MOSFET that can be triggered by the pull-up or pull-down) may cause issues when using these pins in certain circuits. You can see this with the D7 blue LED which will blink dimly and briefly at boot.