Main Content

Use BeagleBone Black GPIO Pins as Digital Inputs and Outputs

This example shows how to use the digital pins on the BeagleBone® Black hardware as digital inputs and outputs.

Caution

Excessive voltage and current can damage the BeagleBone Black hardware. Observe the manufacturer precautions for handling the BeagleBone Black hardware and connecting it to other devices. For more information, see the local copy of the BeagleBone drivers and documentation in the BeagleBone Black Getting Started folder on your host computer, or Getting Started with BeagleBone Black.

  1. When you create a connection to the BeagleBone Black hardware, the AvailableDigitalPins property shows the list of digital pins that are available.

    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 BeagleBone Black hardware shares some digital pins with the SPI and I2C interfaces. Enabling or disabling those interfaces changes the number of available pins.

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

    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'
    
  3. To show a pin diagram for the specific model of the BeagleBone Black hardware that you are using, type:

    showPins(bbb)

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

    configureDigitalPin(bbb,'P8_12','input')

    This example configures pin P8_12 as an input.

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

    readDigitalPin(bbb,'P8_12')
    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).

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

    configureDigitalPin(bbb,'P8_11','output')
  7. To write a logical value to a digital pin, use writeDigitalPin.

    writeDigitalPin(bbb,'P8_11',1)

    This example writes a logical value of 1 to pin 'P8_11'.