Main Content

raspi

Create connection to Raspberry Pi hardware

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

Description

example

mypi = raspi creates a connection, mypi, from the MATLAB® software to the Raspberry Pi® board. Use this syntax to connect or reconnect to the same board.

You do not need to supply the user IP address, user name, and password to create a connection. The raspi object reuses these settings from the most recent successful connection to a Raspberry Pi board. These settings must be provided for the first connection created during the setup process.

In MATLAB Online™, the raspi object reuses the settings from the most recent successful connection. If connecting for the first time, this syntax is used to create a connection to the first Raspberry Pi board listed by raspilist with status "Ready to connect".

After connecting to the board, you can use mypi to interact with the Raspberry Pi board and peripheral devices.

To close the connection, use clear to remove mypi and any other connections that use mypi.

example

mypi = raspi(ipaddress,username,password) overrides the IP address, user name, and password from the previous connection. Use this syntax to connect to a board whose settings are different from the previous successful connection. After changing the password on a board, use this syntax. Or, after connecting from the MATLAB software to a second Raspberry Pi board, use this syntax. You can use this syntax without username and address if a successful connection has been previously created with this syntax. This syntax is not supported in MATLAB Online.

Note

The firmware has a default user name, 'pi', and password, 'raspberry'. The pi user name has sudo root powers at the command line. It is a good security practice to change the default password to a strong password.

example

mypi = raspi(hostname,username,password) uses the host name instead of the IP address to make a connection to a Raspberry Pi. Use this syntax to connect a board whose settings are different from the previous successful connection or to connect to a second board. You can use this syntax without username and address if a successful connection has been previously created with this syntax. This syntax is not supported in MATLAB Online.

example

mypi = raspi(name) creates a connection to a Raspberry Pi board in MATLAB Online using the name assigned to the board during the setup steps described in Connect to Raspberry Pi Hardware Board in MATLAB Online.

Note

This syntax is supported only in MATLAB Online.

example

mypi = raspi(serialnumber) creates a connection to a Raspberry Pi board in MATLAB Online using its serial number. Identify the serial number using raspilist.

Note

This syntax is supported only in MATLAB Online.

Examples

collapse all

You can connect from the MATLAB software to a Raspberry Pi board. Use this connection to interact with the Raspberry Pi hardware.

Create a connection, mypi, from the MATLAB software to a Raspberry Pi board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18735
               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'}
        AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
             I2CBusSpeed: 100000
  AvailableCANInterfaces: {"can0"}

Use mypi to connect from the MATLAB software to a serial device.

myserialdevice = serialdev(mypi,'/dev/ttyAMA0')

To close the connection, use clear to remove mypi and any other connections that use mypi.

clear myserialdevice
clear mypi

You can connect from the MATLAB software to Raspberry Pi board that has a different IP address, user name, and password from the previous successful connection. This syntax is not supported in MATLAB Online.

Follow this example to reconnect to a board after you change its settings, or to connect to multiple boards concurrently.

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

  Raspi with Properties:

           DeviceAddress: '169.254.0.4'
                    Port: 18735
               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'}
        AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
             I2CBusSpeed: 100000
  AvailableCANInterfaces: {"can0"}

You can use a host name instead of an IP address to connect from the MATLAB software to a Raspberry Pi board. This syntax is not supported in MATLAB Online.

Support Package Installer generates the Raspberry Pi host name during the setup process.

mysecondpi = raspi('raspberrypi-hysdu8X38o','rocky','bullwinkle')
mysecondpi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18735
               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'}  
        AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
             I2CBusSpeed: 100000
  AvailableCANInterfaces: {"can0"}

The DeviceAddress reflects whether a host name or IP address created the connection.

To configure your Raspberry Pi board, see Connect to Raspberry Pi Hardware Board in MATLAB Online. If connecting to a Raspberry Pi for the first time in MATLAB Online, you can create a connection to the first board listed by raspilist with status "Ready to connect".

List the Raspberry Pi boards available for MATLAB Online connection.

list = raspilist
list =

  2x4 table

       Name        SerialNumber       PackageVersion          Status
      ______    __________________    ______________    __________________

     "homePi"   "00000000a9f2c18c"       "18.1.0"       "Ready to connect"
     "workPi"   "00000000f23438fd"       "18.1.0"       "Ready to connect"

Connect to the first board 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)'} 
      AvailableWebcams: {'USB2.0 PC CAMERA: USB2.0 PC CAM (usb-20980000.usb-1.5):'}
           I2CBusSpeed: 0

For later raspi connections, this syntax reuses the settings from the most recent successful connection, instead of connecting to the first board with status "Ready to connect".

You can connect to your board using the name you created during the setup described in Connect to Raspberry Pi Hardware Board in MATLAB Online.

List the Raspberry Pi boards available for MATLAB Online connection.

raspilist
ans =

  1x4 table

       Name        SerialNumber       PackageVersion          Status
      ______    __________________    ______________    __________________

      "myPi"    "00000000a9f2c18c"       "18.1.0"       "Ready to connect"

Connect to your board using the name listed.

r = raspi('myPi')
r =

  raspi with properties:

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

You can connect to your board using the serial number in MATLAB Online.

List the available Raspberry Pi boards in MATLAB Online.

raspilist
ans =

  1x4 table

       Name        SerialNumber       PackageVersion          Status
      ______    __________________    ______________    __________________

      "myPi"    "00000000a9f2c18c"       "18.1.0"       "Ready to connect"

Connect to your board using the serial number from the raspilist output.

myPi = raspi('00000000a9f2c18c')
r =

  raspi with properties:

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

Input Arguments

collapse all

IP address of the board, specified as a string.

Example: '169.254.0.2'

Data Types: char

Linux user name, specified as a string.

Example: 'pi'

Data Types: char

Linux user password, specified as a string.

Note

The firmware has a default user name, 'pi', and password, 'raspberry'. This user has sudo root powers at the command line. It is a good security practice to change this default password to a strong password.

Example: 'raspberry'

Data Types: char

Host name of the board, specified as a string.

Example: 'raspberrypi-hysdu8X38o'

Data Types: char

Name in MATLAB Online, specified as a character array or string.

Example: 'myPi'

Example: "myPi"

Data Types: char | string

Serial number of board in MATLAB Online, specified as a character array or string.

Example: '00000000a9f2c18c'

Example: "00000000a9f2c18c"

Data Types: char | string

Output Arguments

collapse all

A connection from the MATLAB software to the Raspberry Pi board, returned as a raspi object. You can use this connection to interact with the Raspberry Pi hardware and attached devices (e.g., Camera Board, I2C, serial, and SPI).

You can use mypi with the methods listed in Object Functions.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.