Programming Arduino Uno with Simulink
Install the Arduino® support package, create a simple model, and download the model to Arduino Uno using a step-by-step workflow with Simulink®.
In this tutorial, I'm going to show you how to set up the Simulink support package for Arduino. Then we are going to build a simple Simulink model and generate code from that model to run on the Arduino Uno hardware board that links in LED.
So let's see what the requirements are. On the software side, we're going to need MATLAB installed. You will also need a bat365 Account, which you can create on the bat365 website or during the installation. Just so you know, the student version for MATLAB is available for a little over $100 from the bat365 website or from amazon.com.
This student version will give you access to 10 additional products in addition to MATLAB and Simulink. So that's the software side. On the hardware side, we are building a very simple circuit, wherein an LED is connected to the pin 9 of the Arduino Uno board via a 220 ohm resistor.
The other end of the LED is connected to the ground on the Arduino Uno board. Now, moving onto the next step, we're going to assume that you've built this simple circuit, connected it to the Uno, and the Uno is connected to the computer that has MATLAB installed on it. So with that, we're ready to jump into the installation.
Let's go to our MATLAB installation. Here, you're going to click on the top, on Add-ons, and then select Hardware Support Packages, and internet as the way of installation. Click Next, and you'll see a list of hardware boards that are supported in MATLAB. And you can select whichever support package you want to install.
Select Arduino and click Next. Now, you're going to log into your bat365 Account, hit Next through the license agreement, and complete the simple installation with a few clicks. Give it a minute to install, and it's really that simple. You go to your MATLAB desktop, select your option for the support package, log into your account, and that's it. You're done.
So once the package is installed, it's going to open up the documentation and show you how to get started with the support package. These are really simple step-by-step examples that you can follow to get started on your own. In this video, I'm going to show you how to build a simple model.
In case you want to jump to the code generation step directly, simply type ArduinoUno_gettingstarted at the MATLAB command prompt and just hit Enter. This is going to open a pre-built model that's ready for code generation and does exactly the kind of things that we want to do in this tutorial.
For now, I'm going to show you how to do this from scratch, so click on the Simulink library icon to open the library browser. On the left side, you're going to see a list of libraries that are installed in your version. And on the right side shows the blocks in a specific library, like the support package for Arduino.
As you can see, the individual blocks in this library are focused on a particular interface to the Arduino Uno board. So for example, digital output or serial interface and that kind of thing. For this tutorial example, we're going to use the digital output block, and so we will create a blank canvas or a brand-new model in Simulink and to drag and drop the digital output block in that model.
So on the left, we're going to click on the New Model symbol and drag and drop the digital output block into the model. Now, if you double-click on this block, you can see the different parameters you can modify. For now, it's set to pin 9, which is perfect, so we click OK, and we're done.
Now, at this point, you can run the simulation by clicking the Play button, but it's not going to do anything because we don't have any inputs for the block. So to fix that, we're going to grab a pulse generator block from the Simulink's sources library. So we go to the sources library in Simulink and drag and drop a pulse generator block in this model.
And let's configure it now. So we want to generate a pulse every second. Let's see. Let's set the pulse time to sample based and then look at the rest of the parameters. We have the period set as 10 samples, and then the pulse rate at 5. So if we set the sample time to 0.1, 0.1 times 10 should give us a pulse every second.
So we click OK, and then how do we verify that the pulse generator is doing the right thing? So we grab a scope block from the [INAUDIBLE] library to visualize how the pulse is being generated. So drag and drop the scope block and connect the pulse generator to the scope, and then run the simulation.
When we double-click the scope block, we see that indeed, this pulse generator block generates a pulse every second, and that's what we want. Now we are ready to connect the pulse generator to the Arduino block, which is the pin 9 block, or digital output block. So we connect the pulse generator to the digital output, get rid of the scope.
And now we can move on to the code generation section of the tutorial. Let's quickly save the model as tutorial one or something, and then we can go to the co-generation section. All right, so we've built the circuit as per specifications. We have connected the circuit to Uno, and Uno's connected to the board.
And now we're ready to generate code. So we click on the Tools menu and then select Random Target Hardware and Prepare to Run. Here, we're going to select Arduino Uno as the board, and then leave the rest of the parameters as it is, as in the default scheme. So click OK, and that's it.
We're going to next click a button and generate code from this model, download it on the Arduino hardware board. So click on Run Our Target Hardware again and Run. This will start the code generation process. And at the bottom bar of your model, you're going to see different messages that show you the status of code generation process.
And once the [INAUDIBLE] model is successfully downloaded on Arduino Uno, you are done. At that point, the LED should be blinking as expected every second. So let's see if that's really true. Let's go to the hardware and check it out. All right, it's really blinking every second.
So with that, we've shown you how to install the support for Arduino in Simulink, how to build a simple model and verify what it's doing before you automatically generate code from the model and download it on Arduino Uno.
Featured Product
Simulink
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other bat365 country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)