Main Content

Use BeagleBone Black SPI Interface to Connect to Device

This example shows how to exchange data with an SPI device.


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. Create a connection to the BeagleBone Black hardware.

    bbb = beaglebone
    bbb = 
      beaglebone with properties:
               DeviceAddress: ''
                   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: {} 

    By default, SPI is disabled, so AvailableSPIChannels does not show any channels.

  2. Enable SPI and get the channels.

    enableSPI(bbb, 0)
    ans = 
  3. Show the location of the SPI pins:

    • P9_22 (SPI0_SCLK => SCLK) outputs a serial clock signal to synchronize communications.

    • P9_18 (SPI0_D1 => MOSI) outputs data to the SPI peripheral device.

    • P9_21 (SPIO_DO => MISO) receives data from the SPI peripheral device.

    • P9_17 (SPI0_CSO => CE0) enables one SPI peripheral device.


  4. Before continuing, research the manufacturer product information to determine which settings the SPI device supports.

  5. Physically connect the BeagleBone Black hardware to an SPI devices. Connect the SCLK, D1, and D0 pins to their counterparts on the SPI device. Connect the CS0 pin on BeagleBone Black hardware to the CE pin on the SPI device.

  6. Create a connection to one of the SPI devices.

    spi = beaglebone.spidev(bbb,'spidev1.0')
    spi = 
      spidev with properties:
          Channel: spidev1.0      
             Mode: 0               (0, 1, 2 or 3)
      BitsPerWord: 8               (only 8-bits per word is supported)
            Speed: 500000          (View available speeds)

    The SPI device determines the data speed. BeagleBone Black hardware supports speeds from 5 kHz to 32 MHz (spi.Speed from 500000 to 32000000)

  7. SPI is full duplex. Perform read or write operations concurrently using writeRead.

    To read data from SPI, send dummy values. To write data to SPI, discard the data SPI returns.

    out = writeRead(spi,hex2dec('08'))
    out = 

When you are finished using the SPI interface, restart the hardware to make additional GPIO pins available.