From the series: Raspberry Pi Support from MATLAB
This video gives you a step-by-step guide on how to install the MATLAB® Support Package for Raspberry Pi™ using the MathWorks Raspbian image. It also includes an example of how to blink the onboard user LED to help the user confirm the proper setup of the hardware with the support package.
Hi, I am Saipraveen. I work at MathWorks. In this video, I will show you how to get started with the MATLAB Support Package for Raspberry Pi using the MathWorks Raspbian image. After going through the setup, I will show you how to bring the onboard LED on the Raspberry Pi B+ board.
Before we get started, let's quickly go through the things you will need to follow along. Most importantly, you will need a MathWorks Account and MATLAB release 2017b or later, and start with an active license. For the purpose of this video, I will be using MATLAB release 2018b.
Next, you will need a Raspberry Pi board. I will be using a Raspberry Pi 3 Model B+ board in this video. The setup procedure will be the same for the other Raspberry Pi boards except Zero W, which has a slightly different workflow. Then you will need a micro-SD card which is 4GB or larger. [? Port ?] is a micro-SD card reader.
Next, you will need a power supply for the board, and lastly, an Ethernet cable for LAN connection where a known wireless access point is required. Now that we have all the things we need, let's get started by opening MATLAB.
Once the MATLAB windows is open, you can navigate to the add-ons dropdown menu and click on Get Add-Ons. This should open the Add-On Explorer. In the Ad-On Explorer, you can browse through various tool boxes and support package, which are there for MATLAB and Simulink. Let's search for Raspberry Pi. And this should return a set of results.
One of them is the MATLAB Support Package for Raspberry Pi hardware. By clicking on it, it brings you to this page where you can show the support package. Clicking on Install, you'll be asked to sign-in to your MathWorks Account. After signing-in you can read through the terms and conditions and click on Accept. So this should download the required support packages and install them.
As you can see, once installation is completed, it brings you to this page. So you have the option of setting-up the doorways now or setting-up the device later. I would highly recommend that you go ahead and setup the device now. But in case you would like to set it up later, let's click on that. Now that we are back in MATLAB, you can go to the add-ons dropdown menu and click on Manage Add-Ons.
So this should take you to the Add-On Manager, where you can see all the tool boxes and the hardware support packages which are in this chart. For the MATLAB Support Package for Raspberry Pi, if you need to go back to the setup, you can click the gear icon, and that should open the hardware setup.
In case you would like to access the documentation on an install, you can click here. Lets go back to the setup. This would be the screen in case you had clicked on Setup Now earlier. In the screen we need to select the Raspberry Pi board that we have.
So I have a Raspberry Pi 3 Model B+ board that I've selected. Please choose the model that you have. In case you have a Zero W, the workflow will be slightly different, and you might want to follow the instructions which are there in the wizard as we proceed along. Let's go to the next screen.
So now we need to select the operating system. In this video, I am going to setup the hardware with the MathWorks Raspbian image, but you also have an option of customizing an existing operating system running on the hardware. The MathWorks Raspbian image comes with all the required libraries and packages which are compatible with MATLAB and Simulink. So let's go to the next screen.
So now you can click on download. And this should open your default web browser and go to a [? keytop blink ?] from where the image is downloaded. So once the image is downloaded, you can click here and click on Show In Folder, to open a file explorer. That gives you the link of where the file is downloaded. This will be used in the next part of the setup process. Now we can go to the next screen.
So in this text box, you can paste the file part of the download, or you can go to Browse and select the file that you have downloaded. Please make sure to select the latest file and click on Open.
So now that there has been images validated, let's go next to the network setting part of the hardware setup. Here you can see that we have four different options available. The option that you choose from this network configuration dictates the way in which the Raspberry Pi will be connected to the host computer. So the first option is an ethernet connection to a LAN or a home network. So you should choose this option when the Raspberry Pi is connected to LAN network or when you have your Raspberry Pi connected to your modem or router via an ethernet connection.
The second option is a wireless network in which your Raspberry Pi could be connected to a known wireless network onto which your host computer is also going to turn on too. The third option is directly connecting your host computer to the Raspberry Pi via an ethernet cable. The fourth option is where you can enter your network settings manually, but that is not covered in this video as it is for advanced users.
So let's start with the wireless configuration. So the first thing you need to enter is the SSID. SSID is simply a technical term for the network name. So you can find your SSID by using another device that is connected to the same network.
So on the Windows computer you can click on the Wi-Fi icon on the taskbar. In my case, I'm connected to w-inside. That will be the SSID. Now select the security for your Wi-Fi connection and the password for your wireless network. Many routers provided by internet service providers have a sticker on the bottom or the side listing the default SSID and the passphrase. So that will be the one that you need to enter here.
In case you want to fix the IP address that the Raspberry Pi will be connected on to, you can enter here, with the network mask and the default gateway address. But in my case, I'm going to choose Automatically Get IP Address and go to the next screen.
So now in this part of the setup process, we are going to burn the Raspbian image with the selected network setting onto the SD card. So for that, please insert the micro SD card into the micro-SD-card reader and then on to your host computer. So once done, click on the Refresh button. And this should list the drives which are available. In my case, D drive is the micro-SD card. Let's go to the next screen.
Say you already have the Raspbian image written onto the SD card, and you're just doing a network settings change, then you need not write onto the SD card again. So in that case of just a network change, we can directly go to next. So here, we are going to write the firmware onto the micro-SD card. Please note that we are writing the firmware onto the memory card and it will erase all the existing data that was there.
So now that the write is complete, we can go on to the next screen. So as a first step, now we need to remove the micro-SD-card reader and the micro-SD card from the computer and insert it into the Raspberry Pi. Please note that it is important to insert the micro-SD card into the Raspberry Pi before turning it on.
So once the micro-SD card is inserted into the Raspberry Pi and the Raspberry Pi is powered using a 5-volt micro-USB power supply, please make sure that the power LED on the Raspberry Pi is red and the act LED is blinking green. That indicates the SD-card activity. Now once you ensure this, we can go on to the next screen.
So now this will try to detect the Raspberry Pi. So now you see that our Raspberry Pi is detected, and you're brought on to this screen. Now you see the IP address, the hostname, user name, and password are displayed here. So this IP will be used to connect the board from MATLAB. The user name and password are useful when you need to do an SSH.
So now let's test the connection to the board. The test connection does a ping. And it says that the test connection is successful. Now by pressing Next, we complete the setup.
So now let's discuss directly connecting to the host computer or through a line network. The process is the same for both. I'm going to show you, taking an example of directly connecting to a host computer. So in this case you're directly connecting the Raspberry Pi and the computer via an ethernet cable. So let's go to the next part.
Unlike the wireless configuration, in this case you need not enter any details. You need to insert the micro-SD card into the micro-SD-card reader and then onto your computer. So once you insert the micro-SD-card reader, click on Refresh, and you should see the drive. Let's go to the next screen.
So here you can see that I have already written the firmware onto the SD card. Now the only change I'm doing is the type of network configuration. So for that we need not write the image again onto the SD card, but just make this particular network configuration change onto the SD card. So for that let's just go on to the next screen, and this should do the right changes.
So the procedure is the same as for wireless. We need to remove the micro-SD card and insert it into the Raspberry Pi and then power it on. Additionally, in this case, ensure that an ethernet cable is directly connected from your computer to the Raspberry Pi. So once you see the power LED on and the green LED blinking, you can go on to the next screen. So now this will try to detect the Raspberry Pi.
So now you see that our Raspberry Pi is detected, and you're brought onto this screen. So now let's test the connection to the board. The test connection does a ping. And it says that the test connection is successful. Now, by pressing Next, we complete the setup for the Raspberry Pi. Please note that this process will be the same for the LAN connection as well.
Now we can move on to some examples. So once the setup is complete, you are brought to this documentation page, where you have further information about the support package and some examples discussed. Once you're back in MATLAB, let's first start by creating an object-- r is equal to raspi. So in this case, r is the handle to the raspi object. So while creating this object, MATLAB connects to a server running on the Raspberry Pi hardware.
In case you have any issues with connecting with the Raspberry Pi-- the most common issues are please ensure that the board is powered on or the network configuration that was selected during the setup process is not the same that is used currently. Ensure that the ethernet or the LAN connection between the Raspberry Pi, either to the host computer or to the modem, is proper.
Now, in case you would like to connect to a specific Raspberry Pi board with an IP address, you can also use r is equal to raspi. Enter the IP address of the board, the user name, and the password. Now you can press Enter.
But in this case it will throw an error since an active connection to the same board already exists. So in this case, you will have to clear the current object and then execute the same command again. And this should give a successful connection.
Now here you can see that led0 is an available LED, which is the user LED on the board. Let's do a clear again. So now let me write a simple script to bling the onboard LED. Let me create a newsgroup name blinkLED.
So let me start by creating a function named blinkLED. So within this function, let's call the raspi object. I want to bling the LED for 10 cycles. So let me create a for loop for i is equal to 1 to 10. Let me use the disp function to show the value of i on the command window.
So now let's use the writeLED function to turn on the LED. So we need to enter object's name, which is r, then the LED. In our case it is LED0. And then 0 is the value, so it turns off the LED. Let me do a pause for 0.5 seconds. And then I'm going to copy the same part. But in this case, I'm going to turn the LED on.
So now we have our simple script ready, that should blink the onboard LED on the Raspberry Pi board. Let me save it and do a run. So here you can see that the value of i increments. And you can see the LED blinking on the Raspberry Pi board.
So once it increments up to 10, it takes this out of the program. So by doing this, we have implemented a basic blink LED on the Raspberry Pi board.
For our additional resources and documentation, you can use the below links. These resources should help you explore the package further. Be sure to like this video if you found it was helpful and comment below with your questions. Thanks for watching.
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.