Connect to Raspberry Pi Hardware Board in MATLAB Online
Prepare your Raspberry Pi® hardware board to establish a connection from MATLAB® Online™.
Prerequisites
Check that your board and operating system are supported for a MATLAB Online connection. The supported operating systems are Buster, Bullseye, and Bookworm. The supported boards are the Raspberry Pi 2 Model B, Raspberry Pi 3 Model B, 3 Model B+, Raspberry Pi 4 Model B, and Raspberry Pi 5. 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 MATLAB Online.
If you are using a version of Raspbian 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
Using the full version of the Raspberry Pi OS is recommended when deploying MATLAB functions on the Raspberry Pi hardware in MATLAB Online.
Set Up Raspberry Pi Hardware Board
Install the MATLAB 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 MATLAB 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 MATLAB Function to Raspberry Pi.
Step 3: Enable/disable deploying MATLAB Function to Raspberry Pi. Do you want to enable deploy MATLAB function as a standalone executable? (y/n): y
Provide your MathWorks® Account credentials to authenticate the MATLAB 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 4: 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. We recommend that you enable the Two-Step Verification for your MathWorks account for additional security. For specific authentication instructions if the Two-Step Verification enabled, 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 MATLAB Online. To re-authenticate your board, follow the steps in MathWorks Account Credentials Have Expired.
Restart your Raspberry Pi to start listening for MATLAB Online connections.
$sudo reboot
You can modify settings at any time after the initial setup by using flags. These are the flags you can provide to
matlab-rpi-setup
package installation command.Description Option syntax Example Set Raspberry Pi display name in MATLAB Online. --name=name
sudo matlab-rpi-setup --name myPi2
-n
sudo matlab-rpi-setup -n myPi2
Set authentication credentials for Raspberry Pi in MATLAB 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 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-rpi-setup 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 MATLAB Online
Open a MATLAB 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" "00000000a9f2c18c" "18.1.0" "Ready to connect"
These are the possible statuses for your Raspberry Pi:
Ready to connect Raspberry Pi has the correct version of matlab-rpi package installed. You can create a MATLAB Online connection. Connected Raspberry Pi has the correct version of matlab-rpi package installed. It is already connected in MATLAB Online. Upgrade required Raspberry Pi has an outdated version of matlab-rpi package installed. See MATLAB Package on Raspberry Pi Needs to Be Updated for instructions. Authentication required Provide your MathWorks Account credentials to reauthenticate online connection. This status appears two weeks before your login credentials expire. See MathWorks Account Credentials Have Expired for instructions. Use
raspi
without any inputs to reuse the settings from the most recent successful 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: '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
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('00000000a9f2c18c');
Note
If you encounter errors after running the above commands, try using additional arguments (as listed in
raspi
) or refer to Troubleshoot Raspberry Pi in MATLAB Online.After successfully connecting to your Raspberry Pi from MATLAB Online, see Getting Started with MATLAB Support Package for Raspberry Pi Hardware to perform basic operations on the hardware.
Note
Only one user can access a Raspberry Pi through MATLAB Online.
Deploy MATLAB Function on Hardware from MATLAB Online
Create a MATLAB function
blinkLED
and verify it in connected mode in MATLAB Online on Raspberry Pi.function blinkLED() % Create a Raspberry Pi object r= raspi(); % Blink the LED for 100 cycles for count = 1:10000 % Turn on the LED writeLED(r,"LED0", 1); % Pause for 0.5 seconds pause(0.5); % Turn off the LED writeLED(r,"LED0", 0); % Pause for 0.5 seconds pause(0.5); end end
Add compilation directive
#codegen
and make code changes to ensure the function is deployable. For more information on deploying MATLAB function on the hardware, see Workflow to Deploy MATLAB Function on Raspberry Pi.function blinkLED() % #codegen % Create a Raspberry Pi object r= raspi(); % Blink the LED for 100 cycles for count = 1:10000 % Turn on the LED writeLED(r,"LED0", 1); % Pause for 0.5 seconds pause(0.5); % Turn off the LED writeLED(r,"LED0", 0); % Pause for 0.5 seconds pause(0.5); end end
Create the hardware configuration object.
board = targetHardware('Raspberry Pi')
board = targetHardware with properties: Name: 'Raspberry Pi' DeviceAddress: '00000000a9f2c18c' Username: '' Password: '' BuildDir: '/home/matlabrpi' EnableRunOnBoot: 0 BuildAction: 'Build, load, and run' CoderConfig: [1x1 coder.CodeConfig]
Deploy the
blinkLED
function on the Raspberry Pi. Deployment can take a few minutes.deploy(board, 'blinkLED');