Connect to Raspberry Pi Hardware Board in Simulink Online
Prepare your Raspberry Pi® hardware board to establish a connection from Simulink® Online™.
Prerequisites
Check that your board and operating system are supported for a Simulink Online connection. The supported operating systems are Raspbian Stretch, Buster, and Bullseye. The supported boards are the Raspberry Pi 2 Model B, Raspberry Pi 3 Model B, 3 Model B+, Raspberry Pi 4 Model B, and Compute Module 4. It is a best practice to implement the security measures outlined in Securing your Raspberry Pi.
Turn on your Raspberry Pi, and make sure it is connected to the Internet. Your device does not have to be connected to the same computer or the same network as the computer running Simulink Online.
If you are using a version of Raspberry Pi OS with a desktop environment, you can open a shell to enter the installation commands. If your device boots into the command line, enter the installation commands there. You can also access your device via SSH or another remote access method, as described in Remote Access.
Note
Use a full version of the Raspberry Pi OS with a desktop environment when deploying Simulink models on the Raspberry Pi hardware in Simulink Online.
Set Up Raspberry Pi Hardware Board
Install the Simulink package for Raspberry Pi by entering these commands in the Raspberry Pi shell.
$sudo apt-get update $sudo apt-get install matlab-rpi
Configure your Raspberry Pi to make it discoverable in Simulink Online. Follow the setup steps that appear in your command line.
$sudo matlab-rpi-setup
Give your device a name.
Step 1: Provide a name for your Raspberry Pi in MATLAB Online. Name: myPi
Choose to enable or disable peripheral features.
Step 2: Enable/disable peripherals on Raspberry Pi. Do you want to enable I2C? (y/n): y Do you want to enable SPI? (y/n): y Do you want to enable camera? (y/n): y Do you want to enable UART? (y/n): y
Choose to enable or disable deploying a MATLAB® function or Simulink model to the Raspberry Pi.
Step 3: Enable/disable deploying MATLAB Function/Simulink model to Raspberry Pi. Do you want to enable deploy MATLAB function/Simulink model as a standalone executable? (y/n): y Userland library is required to deploy MATLAB function/Simulink model to Raspberry Pi Hardware. Userland library is licensed to you by third parties under separate license terms available in the GitHub repository, https://github.com/raspberrypi/userland/ Do you want to install userland? (y/n): y Paho MQTT C library is required to deploy MATLAB function/Simulink model to Raspberry Pi Hardware. Paho MQTT C library is licensed to you by third parties under separate license terms available in the GitHub repository, https://github.com/eclipse/paho.mqtt.c Do you want to install Paho MQTT? (y/n): y "nnpy" library is required to deploy MATLAB function/Simulink model to Raspberry Pi Hardware. nnpy python ibrary is licensed to you by third parties under separate license terms available in the GitHub repository, https://github.com/nanomsg/nnpy Do you want to install nnpy? (y/n): y
Choose to enable or disable deploying Simulink customizable dashboard blocks to the Raspberry Pi.
Step 4: Enable/disable deploying Simulink customizable dashboard blocks on Raspberry Pi. Do you want to enable dashboard deployment workflow? (y/n): y Username used to login to the display session on the Raspberry Pi Hardware is required for dashboard deployment. Enter the username: pi Update display username success! Chromium-browser, required to deploy Simulink customizable dashboard blocks to Raspberry Pi Hardware will be installed as part of the setup.
Provide your MathWorks® Account credentials to authenticate the Simulink Online connection. Your user ID and email address associated with your account are both valid. This information is not stored locally on your Raspberry Pi.
Step 5: Provide your MathWorks Account credentials. Email Address or User ID: jsmith Password: ***********
If you are having problems logging in to your MathWorks Account, visit the MathWorks website. Enable the two-step verification for your MathWorks account for additional security. For specific authentication instructions if you have enabled the two-step verification, see Two-Step Verification Is Turned On.
Note
This login remains valid for six months until your credentials expire. Once they expire, you will not be able connect to your Raspberry Pi from Simulink Online. To reauthenticate your board, follow the steps in MathWorks Account Credentials Have Expired.
Restart your Raspberry Pi to start listening for Simulink Online connections.
$sudo reboot
You can modify the settings at any time after the initial setup by using flags. You can provide these flags to the
matlab-rpi-setup
package installation command.Description Option syntax Example Set Raspberry Pi display name in Simulink Online. --name=name
sudo matlab-rpi-setup --name myPi2
-n
sudo matlab-rpi-setup -n myPi2
Set authentication credentials for Raspberry Pi in Simulink Online. --login
sudo matlab-rpi-setup --login
-l
sudo matlab-rpi-setup -l
Enable or disable I2C feature on Raspberry Pi. Requires a reboot. --i2c={enable | disable}
sudo matlab-rpi-setup --i2c enable
-i
sudo matlab-rpi-setup -i enable
Enable or disable SPI feature on Raspberry Pi. Requires a reboot.
--spi={enable | disable}
sudo matlab-rpi-setup --spi enable
-s
sudo matlab-rpi-setup -s enable
Enable or disable camera feature on Raspberry Pi. Requires a reboot. --camera={enable | disable}
sudo matlab-rpi-setup --camera enable
-c
sudo matlab-rpi-setup -c enable
Enable or disable UART feature on Raspberry Pi. Requires a reboot. --uart={enable | disable}
sudo matlab-rpi-setup --uart enable
-u
sudo matlab-rpi-setup -u enable
Deploy a MATLAB function or Simulink model as a standalone executable on Raspberry Pi. --deploy={enable | disable}
sudo matlab-rpi-setup --deploy enable
-d
sudo matlab-rpi-setup -d enable
Display MATLAB and Raspberry Pisetup usage. --help
sudo matlab-rpi-setup --help
-h
sudo matlab-rpi-setup -h
To see the full description of each flag in your shell, enter this command.
$man matlab-rpi-setup
Connect to Raspberry Pi from Simulink Online
Open a Simulink Online session at https://matlab.mathworks.com from any computer connected to the Internet. Log in with your MathWorks Account credentials.
Note
You must log in with the same account credentials you used during the Raspberry Pi setup.
Scan for Raspberry Pi hardware configured with the same MathWorks Account credentials.
raspilist
ans = 1x4 table Name SerialNumber PackageVersion Status ______ __________________ ______________ __________________ "myPi" "10000000675b0321" "21.2.0" "Ready to connect"
The following table provides a list of the possible statuses for your Raspberry Pi.
Ready to connect Raspberry Pi has installed the correct version of MATLAB Support Package for Raspberry Pi Hardware. You can create a Simulink Online connection. Connected Raspberry Pi has installed the correct version of MATLAB Support Package for Raspberry Pi Hardware. It is already connected in Simulink Online. Upgrade required Raspberry Pi has installed the correct version of MATLAB Support Package for Raspberry Pi Hardware. For more instructions, see Simulink Package on Raspberry Pi Needs to Be Updated. Authentication required Provide your MathWorks Account credentials to reauthenticate the Simulink Online connection. This status appears two weeks before your login credentials expire. For more instructions, see MathWorks Account Credentials Have Expired. Use
raspi
without any inputs to reuse the settings from the most recent Raspberry Pi connection. If connecting for the first time, this function creates a connection to the first Raspberry Pi device designated as "Ready to connect" in theraspilist
output.r = raspi
r = raspi with properties: DeviceAddress: 'myPi' SerialNumber: '10000000675b0321' 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
Alternatively, you can specify the
name
orserialnumber
of the Raspberry Pi from theraspilist
output to connect to a certain device.r = raspi('myPi');
r = raspi('10000000675b0321');
Communicate with Raspberry Pi Hardware Using Connected IO in Simulink Online
In Simulink Online, you can use connected IO to communicate with the input-output peripherals on the Raspberry Pi hardware board during normal mode simulation. For more information on connected IO, see Communicate with Hardware Using Connected IO.
This section demonstrates the connected IO workflow in the Get Started with Simulink Support Package for Raspberry Pi Hardware example. You can use the same steps for any example in the support package.
In the MATLAB Command Window, run this command to open the Simulink model.
openExample('raspberrypi_gettingstarted')
On the Modeling tab of the Simulink model, in the Setup section, click Model Settings.
In the Configuration Parameters dialog box, ensure that the Hardware board parameter is set to
Raspberry Pi
.Select Target hardware resources > Board Parameters and enter the Device Name.
In the Connected I/O section, ensure that the Communication Interface parameter is set to
Internet
.Click Apply > OK.
On the Hardware tab of the Simulink model, in the Mode section, select Connected IO.
In the Run on Computer section of the Simulink model, click Run with IO. During simulation, observe that the LED on the Raspberry Pi board blinks with a time period of 1 second.
Deploy Simulink Model on Raspberry Pi Hardware from Simulink Online
In Simulink Online, you can build and deploy a Simulink model on your Raspberry Pi hardware board.
This section demonstrates the deployment workflow in the Get Started with Simulink Support Package for Raspberry Pi Hardware example. You can use the same steps for any example in the support package.
In the MATLAB Command Window, run this command to open the Simulink model.
openExample('raspberrypi_gettingstarted')
On the Modeling tab of the Simulink model, in the Setup section, click Model Settings.
In the Configuration Parameters dialog box, ensure that the Hardware board parameter is set to
Raspberry Pi
.Select Target hardware resources > Board Parameters and enter the Device Name.
Click Apply > OK.
On the Hardware tab of the Simulink model, in the Mode section, select Run on board.
In the Deploy section of the Simulink model, click Build, Deploy & Start. The generated code is built on the Raspberry Pi hardware and runs automatically. Observe that the LED on the board blinks with a time period of 1 second.
See Also
Get Started with Simulink Support Package for Raspberry Pi Hardware | Communicate with Hardware Using Connected IO | Security Considerations for Raspberry Pi in Simulink Online | Troubleshoot Raspberry Pi in Simulink Online