Use the Raspberry Pi GPIO Pins as Digital Inputs and Outputs

This example shows how to use the digital pins on the Raspberry Pi™ hardware as digital inputs and outputs.

Warning

Excessive voltage and current can damage the Raspberry Pi hardware. Observe the manufacturer’s precautions for handling the Raspberry Pi hardware and connecting it to other devices. For more information, see https://www.raspberrypi.org/technical-help-and-resource-documents.

Create a connection to the Raspberry Pi hardware using raspi. The AvailableDigitalPins property shows the list of digital pins that are available.

mypi = raspi
mypi = 

  raspi with properties:

           DeviceAddress: 'raspberrypi-computername'
                    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

Supported peripherals

The Raspberry Pi hardware shares some digital pins with the SPI and I2C interfaces. Enabling or disabling those interfaces changes the number of available pins.

To review the list of digital pins are available, use the AvailableDigitalPins property.

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

To show a pin diagram for the specific model of the Raspberry Pi hardware that you are using, use showPins.

showPins(mypi)

To configure a pin as a digital input, pass an input value to configureDigitalPin.

configureDigitalPin(mypi,4,'input')

This example configures pin 4 as an input.

To read the value of a digital pin, use readDigitalPin.

readDigitalPin(mypi,4)
ans =

  1

This example shows that a wire connected to pin 4 has an elevated voltage, which produces a logical value of 1 (true). If the wire has no voltage, the logical value of pin 4 is 0 (false).

To configure a pin as a digital output, pass an output value to configureDigitalPin.

configureDigitalPin(mypi,7,'output')   

To write a logical value to a digital pin, use writeDigitalPin.

writeDigitalPin(mypi,7,1)

This example writes a logical value of 1 to pin 7.

See Also

External Websites