Main Content

readDigitalPin

Read logical value from GPIO input pin

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

Description

pinvalue = readDigitalPin(bbb,pin) returns the logical value of a digital pin.

If the pin is not configured, this function configures the pin as a GPIO input.

If the pin is a GPIO output, or if another interface (I2C, Serial, SPI) uses the pin, this function returns an error message.

example

Examples

Configure Pin as Input and Read Its Value

You can configure a GPIO pin as digital input and read its logical value.

Create a connection from the MATLAB® software to the BeagleBone® Black hardware.

bbb = beaglebone
bbb = 

  beaglebone with properties:

           DeviceAddress: '192.168.7.2'
               BoardName: 'BeagleBone Black Rev 00C0'
           AvailableLEDs: {'USR0'  'USR1'  'USR2'  'USR3'}
    AvailableDigitalPins: {1x29 cell}
     AvailableAnalogPins: {'AIN0'  'AIN1'  'AIN2'  'AIN3'  'AIN4'  'AIN5'  'AIN6'}
        AvailablePWMPins: {}
    AvailableSPIChannels: {}
       AvailableI2CBuses: {'i2c-1'}
    AvailableSerialPorts: {}
        AvailableWebcams: {} 

The AvailableDigitalPins property shows the list of available digital pins.

Redisplay AvailableDigitalPins.

bbb.AvailableDigitalPins
ans = 

  Columns 1 through 5

    'P8_7'    'P8_8'    'P8_9'    'P8_10'    'P8_11'

  Columns 6 through 10

    'P8_12'    'P8_13'    'P8_14'    'P8_15'    'P8_16'

  Columns 11 through 15

    'P8_17'    'P8_18'    'P8_19'    'P8_26'    'P9_11'

  Columns 16 through 20

    'P9_12'    'P9_13'    'P9_14'    'P9_15'    'P9_16'

  Columns 21 through 25

    'P9_21'    'P9_22'    'P9_23'    'P9_24'    'P9_26'

  Columns 26 through 29

    'P9_27'    'P9_30'    'P9_41'    'P9_42'

Show the physical location of GPIO 60.

showPins(bbb)

The corresponding pin identifier is 'P9_12'. Connect the digital device that you are using to 'P9_12'.

Check whether 'P9_12' is already configured.

configureDigitalPin(bbb,'P9_12')
ans =

unset

Configure 'P9_12' as a digital input.

configureDigitalPin(bbb,'P9_12','input')

Read the value from pin 'P9_12'.

readDigitalPin(bbb,'P9_12')
ans =

  1

Press a Button to Blink an LED

This example shows how to use a button to blink an LED attached to a GPIO pin.

When you press a button, this example rapidly blinks an LED. The button connects from positive voltage to pin P8_11. Pressing the button closes the circuit, raising the voltage. When readDigitalPin detects the positive voltage, if buttonPressed becomes true. The program toggles the voltages to pin P8_12 on and off 10 times. Pin P8_12 connects to an LED, which connects via a resistor to ground.

configureDigitalPin(bbb, 'P8_11', 'input');
configureDigitalPin(bbb, 'P8_12', 'output');
for ii = 1:100
    buttonPressed = readDigitalPin(bbb, 'P8_11')
    if buttonPressed
        for jj = 1:10
            writeDigitalPin(bbb,'P8_12',1)
            pause(0.05)
            writeDigitalPin(bbb,'P8_12',0)
            pause(0.05)
        end
    end
    pause(0.1)
end

Input Arguments

collapse all

BeagleBone Black connection created using beaglebone, specified as an object.

GPIO pin identifier, specified as a character vector. This argument does not accept vectors because the board cannot access multiple pins simultaneously.

To get a list of valid pin identifiers, enter bbb.AvailableDigitalPins.

Example: P8_12

Data Types: double

Output Arguments

collapse all

Logical value of the pin, returned as a scalar.

Version History

Introduced in R2015a