This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

configurePin

Configure GPIO pin as digital input, digital output, or PWM output

Syntax

configurePin(mypi,pinNumber,mode)
mode = configurePin(mypi,pinNumber)

Description

example

configurePin(mypi,pinNumber,mode) configures the GPIO pinNumber pin of a Raspberry Pi™ device mypi as a digital input, digital output, or PWM output, depending on the specified mode.

mode = configurePin(mypi,pinNumber) returns the current configuration mode of the pin.

Input Arguments

collapse all

Connection to a specific Raspberry Pi hardware board, specified as a raspi object.

GPIO pin number, specified as a scalar. This argument does not accept vectors because the hardware cannot access multiple pins simultaneously.

To get a list of valid pin numbers, enter mypi.AvailableDigitalPins.

Example: 12

Data Types: double

Mode used to configure the pin, specified as 'DigitalInput', 'DigitalOutput', or 'PWM'.

Data Types: char

Output Arguments

collapse all

Current configuration mode of pin, returned as 'DigitalInput', 'DigitalOutput', or 'PWM'.

Examples

collapse all

Configure a GPIO pin as digital input and read its logical value.

Create a connection from MATLAB® to the Raspberry Pi board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18725
               BoardName: 'Raspberry
        Pi Model B Rev 2'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
    AvailableSPIChannels: {}
      AvailableI2CBuses: {'i2c-0'  'i2c-1'}
             I2CBusSpeed: 100000

The AvailableDigitalPins property shows the list of available digital GPIO pins.

Show the location of all the GPIO pins on your device.

showPins(mypi)

Display the AvailableDigitalPins.

mypi.AvailableDigitalPins   
ans =

  Columns 1 through 13

     4  7  8  9  10  11  14  15  17  18  22  23  24

  Columns 14 through 17

    25  27  30  31

Connect your digital device to the first GPIO pin available, for example GPIO 4.

Configure pin GPIO 4 as a digital input.

configurePin(mypi,4,'DigitalInput')

Read the value from pin GPIO 4.

readDigitalPin(mypi,4)
ans =

  1

The logical value of 1 indicates a positive voltage signal on the pin GPIO 4.

Configure a GPIO pin as a digital output and write its logical value.

Create a connection from the MATLAB software to the Raspberry Pi board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18725
               BoardName: 'Raspberry
        Pi Model B Rev 2'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]
    AvailableSPIChannels: {}
      AvailableI2CBuses: {'i2c-0'  'i2c-1'}
             I2CBusSpeed: 100000

The AvailableDigitalPins property shows the list of available digital GPIO pins.

Show the location of all the GPIO pins on your device.

showPins(mypi)

Display the AvailableDigitalPins.

mypi.AvailableDigitalPins   
ans =

  Columns 1 through 13

     4  7  8  9  10  11  14  15  17  18  22  23  24

  Columns 14 through 17

    25  27  30  31

Connect your digital device to the first GPIO pin available, for example GPIO 4.

Configure pin GPIO 4 as a digital output.

configurePin(mypi,4,'DigitalOutput')   

Write a logical value of 1 to pin GPIO 4.

writeDigitalPin(mypi,4,1)

The logical value of 1 sets the signal voltage on pin GPIO 4 to high.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.