Now that we have the necessary software installed, it's time to blink the on-board LED. In this example, we will use the MATLAB support package. 

To establish a connection between MATLAB and Pi 2, the following command can be used from MATLAB. 

 >> mypi = raspi

This command connects MATLAB to the most recent successful connection to a Raspberry Pi board, including the setup process. A MATLAB variable by the name 'mypi' should now appear on the workspace or an error message indicating what went wrong. This object represents the connection that MATLAB has established with the physical object (Raspberry Pi). 

To switch on the on-board LED connected, execute the following command 

>> writeLED(mypi,'led0',1)

Here we are sending a request from MATLAB to Raspberry Pi board, to write the value of 1 on the on-board LED. This should switch ON your LED.

To blink the LED, we have to switch it ON and OFF periodically. 

for i = 1:10
    writeLED(mypi,'led0',1);
    pause(0.5);
    writeLED(mypi,'led0',0);
    pause(0.5);
end

Other functions that are related to the MATLAB Support Package for Raspberry Pi can also be used along with the Raspberry Pi object 'mypi'.

To take a look at the featured examples from MATLAB Support Package for Raspberry Pi, type raspi_examples in MATLAB Command Window. 

Common Error Messages

Error using raspi (line 157)
Cannot establish an SSH connection to the board with device address "169.254.0.2".

Caused by:
Error using raspi (line 153)
Error executing command: FATAL ERROR: Network error: Network is unreachable

Ensure that the Raspberry Pi board is connected to the computer that you are working with. Usually when the lights on the ethernet port and the ACT LED are blinking, there is a connection between the Pi board and the computer. 

If for some reason the connection between MATLAB and Raspberry Pi board is lost, the following error message appears. 

The host and client connection is lost. Make sure the board is plugged in and/or recreate arduino and its related objects.

The best practices to follow to resolve this error -

  1. Disconnect the USB cable and Ethernet cable from both the Pi board and computer 
  2. Clean up MATLAB by typing clear in MATLAB Command Window
  3. Plug in the USB cable to computer and Pi board
  4. Try connecting to the board from MATLAB by using raspi command 
Temporarily unable to load embedded content:

This guide was first published on Apr 16, 2015. It was last updated on Apr 16, 2015.

This page (Using MATLAB Support Package and Raspberry Pi) was last updated on Apr 13, 2021.

Text editor powered by tinymce.