Main Content

configurePin

Set Arduino pin mode

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

Description

example

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

example

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

Examples

collapse all

a = arduino('COM4','Uno');
configurePin(a,'A2')
ans = 
'Unset'
a = arduino('COM4','Uno');
configurePin(a,'A4','I2C');

Display current pin mode.

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

Display current pin mode.

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

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

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

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

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

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.

Note

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
A0D36
A3D39
A4D32
A5D33
A6D34
A7D35
A10D4
A12D2
A13D15
A14D13
A15D12
A16D14
A17D27
A18D25
A19D26

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