Main Content


Set Arduino pin mode

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.



pinMode = configurePin(a,pin) displays the mode the specified pin on the Arduino® hardware in connection a.


configurePin(a,pin,mode) sets the specified pin on the Arduino hardware in connection a to the specified mode.


collapse all

a = arduino('COM4','Uno');
ans = 
a = arduino('COM4','Uno');

Display current pin mode.

pinMode = configurePin(a,'A4')
pinMode = 
a = arduino('COM4','Uno');

Display current pin mode.

pinmode = configurePin(a,'D3')
pinmode = 

Display current pin mode and reset it to 'Unset'.

a = arduino('COM4','Uno');
pinMode = configurePin(a,'D3')
pinMode = 

Change mode to 'Unset' and display the current mode.

pinMode = configurePin(a,'D3')
pinMode = 

Input Arguments

collapse all

Arduino hardware connection created using arduino, specified as an object.

Example: a = arduino;

Pin number on the physical hardware, specified as a character vector.

Example: D3 or A1.


If you connect to ESP32 board and the pin's mode is set to AnalogInput, the pin number must always correspond to a digital pin. For details, see Pin-mapping for ESP32 Boards.

Pin mode specified as a character vector. Valid pin modes are:

  • AnalogInput: acquire analog signals from pin

  • DigitalInput: acquire digital signals from pin

  • DigitalOutput: generate digital signals from pin

  • I2C: specify a pin to use with I2C protocol

  • Interrupt: specify pin in interrupt mode to be used in rotaryEncoder

  • Pullup: specify pin to use a pullup switch

  • PWM: specify pin to use a pulse width modulator

  • CAN: specify pin to use a Controller Area Network bus

  • Servo: specify pin to use a servo

  • SPI: specify a pin to use with SPI protocol

  • Tone: specify pin to use for playTone

  • Ultrasonic: specify pin to use with ultrasonic sensor

  • CAN: specify pin to use with CAN shield

  • Unset: clears pin designation. The pin is no longer reserved and can be automatically set at the next operation.

Pins are configured on first usage. You can reset the pin mode to change the mode. If you want to use a pullup, you must set the mode to 'pullup'.

Output Arguments

collapse all

Current mode of pin, returned as a character vector.

More About

collapse all

Pin-mapping for ESP32 Boards

If you connect to an ESP32 board, use the following table to map the analog pin number to the corresponding digital pin number, which must be entered as the pin's value.

Analog pin on ESP32-DevKitV1 and ESP32-DevKitCDigital pin number to be useda

a If you connect to ESP32 board over WiFi from the MATLAB PC, transferring data through the following pins may not work properly: D2, D12, D13, D14, D15, D25, D26, and D27. For communication over WiFi, it is recommended that you use the other pins listed in this table.

Code Generation Using MATLAB Function Block

  • Use configurePin in a MATLAB® Function block with the Simulink® Support Package for Arduino Hardware to generate code that can be deployed on Arduino Hardware.

  • Configure the Arduino peripherals using configurePin before using it in the MATLAB Function block.

  • The function does not return any output during code generation.

  • The following modes are currently supported for code generation : AnalogInput, DigitalInput, DigitalOutput, Pullup, PWM, and Unset.

Version History

Introduced in R2015b