Main Content

raspi

Connection to Raspberry Pi board

Description

This object represents a connection from the MATLAB® software to the Raspberry Pi® board. To interact with the Raspberry Pi board, use this object with the functions listed in Object Functions.

Creation

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 SerialNumber. Identify the serial number using raspilist.

Note

This syntax is supported only in MATLAB Online.

mypi = raspi(___,"Timeout",time) creates a connection to a Raspberry Pi board in MATLAB Online, and waits for up to time seconds for the connection to complete. The value of time must be greater than the default connection timeout period of 12.5 seconds. You can use this syntax to specify a larger timeout value if you are unable to connect to your board.

Use this syntax with any of the input argument combinations in previous syntaxes.

Note

This syntax is supported only in MATLAB Online.

Input Arguments

expand 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

Output Arguments

expand 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.

Properties

expand all

This property is read-only.

The IP address or host name of the Ethernet port on the Raspberry Pi hardware, returned as a string.

Example: 'raspberrypi-computername'

Data Types: char

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

Example: '00000000a9f2c18c'

Example: "00000000a9f2c18c"

Data Types: char | string

This property is read-only.

The IP port number used for TCP/IP connections, returned as a double.

Example: 18735

Data Types: double

This property is read-only.

The name of the Raspberry Pi hardware, model, and revision, returned as a string.

Example: 'Raspberry Pi Model B Rev 2'

Data Types: char

This property is read-only.

A list of LEDs that are available on the board, returned as a cell array of strings.

Example: {'led0'}

Data Types: cell

This property is read-only.

The numbers of the available GPIO pins on the Raspberry Pi hardware, returned as a vector of doubles.

Example: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31]

Data Types: array

This property is read-only.

The names of the available SPI channels on the Raspberry Pi hardware, returned as a cell array of strings.

Example: {'CE0' 'CE1'}

Data Types: cell

This property is read-only.

The names of the available I2C buses on the Raspberry Pi hardware, returned as a cell array of strings.

Example: {'i2c-0' 'i2c-1'}

Data Types: cell

This property is read-only.

The speed of the I2C buses on the Raspberry Pi hardware, returned as a double.

Example: 100000

Data Types: double

List of the available CAN interfaces on the Raspberry Pi hardware, returned as a cell array of strings.

Example: {"can0", "can1"}

Data Types: cell

Object Functions

configurePinConfigure GPIO pin as digital input, digital output, or PWM output
readDigitalPinRead logical value from GPIO input pin
writeDigitalPinWrite logical value to GPIO output pin
showPinsShow diagram of GPIO pins
writeLEDTurn LED on or off
showLEDsShow location, name, and color of user-controllable LEDs
scanI2CBusScan I2C bus device addresses
enableI2CEnable I2C interface
disableI2CDisable I2C interface
systemRun command in Linux shell on Raspberry Pi hardware
openShellOpen terminal on host computer to use Linux shell on Raspberry Pi hardware
getFileTransfer file from Raspberry Pi hardware to host computer or MATLAB Drive
putFileTransfer file from host computer to target hardware
deleteFileDelete file on target hardware
i2cdevConnection to device on Raspberry Pi hardware
spidevConnection to SPI device on Raspberry Pi hardware
serialdevConnection to serial device on Raspberry Pi hardware
cameraboardConnection to camera board on Raspberry Pi hardware
canChannelConnection to CAN channel connected to a specified device
enableCANEnable CAN interface
disableCANDisable CAN interface
webcamCreate connection to Raspberry Pi web camera

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
AvailableCANInterfaces: {"can0"}

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
AvailableCANInterfaces: {"can0"}

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
AvailableCANInterfaces: {"can0"}

Extended Capabilities