Main Content

Create Connection to Specific Raspberry Pi Hardware Board

Create Connection to One Board

Use the raspi function to create a connection to Raspberry Pi® hardware and assign the connection to a handle. Later, you can use the handle to interact with Raspberry Pi hardware and peripherals. Using raspi without any inputs creates a connection to the Raspberry Pi that had the most recent successful connection.

For example, enter:

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

In this example:

  • The raspi function uses the IP address, user name, and password from the most recent connection to the Raspberry Pi hardware.

  • The handle, mypi, displays properties from the Raspberry Pi hardware.

In MATLAB® Online™, using raspi with no inputs also connects to the Raspberry Pi that had the most recent successful connection. If no such device exists or if connecting for the first time, using this syntax creates a connection to the first board listed by raspilist with status "Ready to connect".

r = raspi
r =

  raspi with properties:

         DeviceAddress: 'homePi'
          SerialNumber: '00000000a9f2c18c'
             BoardName: Raspberry Pi 3 Model B
         AvailableLEDs: {'led0'}
  AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE0','CE1'}
     AvailableI2CBuses: {'i2c-1'}
      AvailableWebcams: {'UVC Camera (046d:0809)'}
           I2CBusSpeed: 0

In this example, the DeviceAddress property is the name given to the Raspberry Pi during the setup steps described in Connect to Raspberry Pi Hardware Board in MATLAB Online. The SerialNumber property uniquely identifies each Raspberry Pi hardware board and can be found using raspilist.

For more information about using the handle to control and exchange data with the Raspberry Pi hardware and peripherals, see:

Create Connection to a Board That Has Different Settings

To connect to Raspberry Pi hardware that has a different IP address, user name, or password from the previous connection, use raspi with arguments. This syntax is not supported in MATLAB Online.

For example:

myotherpi = raspi('169.254.0.4','rocky','bullwinkle')
myotherpi = 

  raspi with properties:

           DeviceAddress: '169.254.0.4'
                    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

In MATLAB Online, connect to a Raspberry Pi with different settings by providing the device name or serial number as inputs to raspi.

r = raspi('homePi')
r =

  raspi with properties:

         DeviceAddress: 'homePi'
          SerialNumber: '00000000a9f2c18c'
             BoardName: Raspberry Pi 3 Model B
         AvailableLEDs: {'led0'}
  AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE0','CE1'}
     AvailableI2CBuses: {'i2c-1'}
      AvailableWebcams: {'UVC Camera (046d:0809)'}
           I2CBusSpeed: 0

In this example, the device name is used to create a connection. The serial number can be used as well. To identify the device name and serial number of your Raspberry Pi, use raspilist.

See Also

Related Examples

More About