writeDigitalPin

Write logical value to GPIO output pin

Description

example

writeDigitalPin(bbb,pin,value) sets the logical value of a GPIO pin to 0 or 1.

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

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

Examples

Configure Pin as Output and Write Its Value

You can configure a pin as a digital output and write 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 location of GPIO 48.

showPins(bbb)

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

Configure pin 'P9_15'as a digital output.

configureDigitalPin(bbb,'P9_15','output')   

Write a logical value of 1 to pin 'P9_15'.

writeDigitalPin(bbb,'P9_15',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

Logical value of the pin, specified as a scalar.

Example: 1

Data Types: double | logical

Introduced in R2015a