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, seehttps://www.raspberrypi.org/technical-help-and-resource-documents.
Create a connection to the Raspberry Pi hardware usingraspi
. TheAvailableDigitalPins
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 theAvailableDigitalPins
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, useshowPins
.
showPins(mypi)
To configure a pin as a digital input, pass anDigitalInput
value toconfigurePin
.
configurePin(mypi,4,'DigitalInput')
This example configures pin 4 as an input.
To read the value of a digital pin, usereadDigitalPin
.
readDigitalPin(mypi,4)
ans = 1
This example shows that a wire connected to pin 4 has an elevated voltage, which produces a logical value of1
(true). If the wire has no voltage, the logical value of pin 4 is0
(false).
To configure a pin as a digital output, pass an outputvalue
toconfigurePin
.
configurePin(mypi,7,'DigitalOutput')
To write a logical value to a digital pin, usewriteDigitalPin
.
writeDigitalPin(mypi,7,1)
This example writes a logical value of1
销7。