Control Tutorials with MATLAB and Simulink (2023)


  • Start of work
  • A simple example
  • Extensions

We will use most of the hardware actions listed in these guidesArduinoIO packagewith MathWorks to support the interface between Simulink and our hardware. We use this package because it allows fairly transparent real-time communication between Simulink and the Arduino Uno (and other Arduino board models) without having to delve into the intricacies of serial communication. You can download the ArduinoIO packageHereor withCentral MATLAB file exchange.

Some Arduino boards (e.g. Mega 2560) can communicate during the experiment using a standardArduino hardware support packagerunning Simulinkoutdoor mode. Details of this support package can be found hereHere. Note R2014a or later is required.

ZArduinoIO packagewhich we will mainly use consists of a simple sketch program running on the Arduino board and a separate set of blocks for Simulink. The program acts as a server for transferring information between the hardware and the host computer running the Simulink model (using specialized blocks). In these applications, all the control logic we develop in Simulink will run on the mainframe. Together, these elements allow us to access the Arduino's digital inputs and outputs and analog inputs and read the encoder, all from Simulink (or the MATLAB command line).

If we want to create a standalone project that does not require connection to a host computer (say an autonomous mobile robot), we will need to implement our control logic on Arduino. We can achieve this by using the standard mentioned aboveArduino hardware support package. In this case, the server program will not be run on the Arduino board, but rather the program translated into the executable code of the Simulink model created by us. Details can be found in individual didactic classes. Note that if you want to run the Arduino board separately from the host computer, you will need a separate power source for the board, which is a 9V battery.

Start of work

After downloading and unzipping the ArduinoIO package, you can start browsing the included packageReadfile. We'll repeat some parts of the instructions here, but the Readme file has more details.

Connection to the Arduino board

The first thing we need to do is power up our Arduino board. Here are step-by-step instructions for connecting the board to a host computer and downloading the board driverHere. This link also describes the Arduino development environment (IDE) and how to load the test program onto the board. The Arduino IDE consists of an editor with which you can write programs (the so-calledsketch), build, compile, then upload the executable to your board. In general, we will only use the IDE to load our server program.

Download the server program

The "server" program will run in parallel on the Arduino board while the Simulink model will run on the host computer. The program runs continuously and receives commands from the Simulink model through the serial port. It then executes these commands and, if necessary, returns the result. Several versions of the program are available in the IO package. We will mainly use the sketchadioes.pde. This sketch performs analog and digital I/O ("adio"), reads quadrature encoders ("e") and can communicate with motors via the screen ("s"). We will not be using the engine screen when performing the steps in this manual. Take into account that.pdeis an older file extension and is currently used by Arduino.I don'tenlargement.

The instructions below are required to load the adioes.pde file into the flash memory of the Arduino board. Until another file is loaded later, this step does not need to be repeated and the IO package can be used as soon as the board is connected to the host computer.

Select the Arduino IDE from the toolbar menuFile > Openand find the adioes.pde file (in ArduinoIO/pde/adioes folder) and open it. Then connect the Arduino board and make sure the correct board and serial port are selected in the IDE (check menuTools/Panel:ITools/Connection:), then selectFile > Moveand wait for the "Upload complete" message.

Control Tutorials with MATLAB and Simulink (1)

At this point, the adioes.pde file has been loaded and you can close the IDE, which is no longer needed for the purpose of this package. In fact, it is recommended to close the IDE to ensure that the IDE does not have a serial connection to the Arduino board when MATLAB is to be used.

Install the package

Run MATLAB as administrator (only once to install packages) by right-clicking the MATLAB icon and selectingRun as administrator. This saves the updated track. Then set MATLAB's current directory to the location of the fileinstall_arduino.m(in ArduinoIO/ folder). Finally, you enterinstall_arduinoon the MATLAB command line to run the script. The script simply adds the appropriate ArduinoIO boards to the MATLAB path and saves the path.

If you keep opening Simulink you will see that the library now contains the Arduino IO library shown below. These blocks are used to connect to the physical Arduino board. For example, to read the sensor throughAnalog inputor to activate the actuator by adigital output.

Control Tutorials with MATLAB and Simulink (2)

A simple example

Here we will implement a simple example showing how the ArduinoIO package can be used to communicate with the Arduino board via Simulink. Many of the examples in this tutorial will use the Arduino board to generate digital control commands (ON/OFF) for a specific system (or its actuators). We will also use the Arduino board to read analog signals (voltages) from the system (or its sensors). In this simple example, we will turn a lamp (LED) on and off.

Hardware installation

The circuit in this simple example consists of a resistor in series with the LED. A resistor is needed to limit the current, otherwise the LED may burn out. The LED is a light-emitting diode and, like any diode, allows current to flow in only one direction (from anode to cathode). The LED "lights up" when turned on.

Control Tutorials with MATLAB and Simulink (3)

You will need the following equipment to perform this activity.

  • Arduino board (e.g. Uno, Mega, etc.)
  • USB cable
  • Bread board
  • Resistor (and capacitor if you want)
  • Jumpers

Our circuit can be implemented on a breadboard and connected to an Arduino board as shown. The specific resistance applied is not important. We'll use one hereControl Tutorials with MATLAB and Simulink (4)resistance indicated by colored stripes (red-red-brown for four-band resistance). The orientation of the LED is indicated by the position of the flat point on the circular lamp and the length of the legs. In particular, the flat dot indicates the cathode (corresponding to the bar at the end of the triangle in the LED symbol). The cathode also corresponds to the shorter of the two legs. The voltage source for the circuit is taken from:digital outputArduino boards (we will use pin 8).

Therefore, you can create an example circuit by connecting one end of the resistor to digital pin 8. The other end of the resistor can then be connected to the longer leg of the LED. This can be achieved by placing the longer leg of the LED and the free end of the resistor on the same row of the motherboard. The short end of the LED is then connected to the ground pin of the Arduino board.

The digital output can generate two outputs, 0 volts or 5 volts. In this case, 5 volts and the amount of current that the board can produce is enough to turn on the LED. For some of the other activities covered in these guides, the digital output does not provide enough power to run your system. In these cases, we can use the low-power signal from the digital output to connect and disconnect the system from a higher-power source (for example, a battery), for example by switching a transistor. The Arduino board will also be used to measure the voltage across the LED by aAnalog input(pin AO). So we connect a jumper wire from the AO pin to the connector where the resistor and the LED meet. The other end of the LED is already connected to the ground pin of the board. In this example, the voltage across the LED is not very interesting, but at least it illustrates how we will be returning signals from our various systems.

Control Tutorials with MATLAB and Simulink (5)

Installing the software

In this example, we'll use Simulink to generate a command to turn on an LED. We will also use Simulink to read the voltage data from the board and plot the data in real time. The Simulink model we will be using is shown below and can be downloadedHere. The Arduino Digital Write block, the Arduino Analog Read block, the Arduino IO Setup block, and the Real-Time Pacer block are part of the IO package. The other blocks are part of the standard Simulink library. In particular, the Pulse Generator block can be found in the Sources library and the Scope block in the Sinks library.

Control Tutorials with MATLAB and Simulink (6)

Each model we create with the IO package includes an IO Setup block and a Real-Time Pacer block. By double-clicking on the IO Setup block, we can identify ourselvesSerial port (COM).to which the Arduino board is connected. In this example, the board is connected to "COM3", although yours may be different. You can identify the port your board is connected to through your computer's control panel (or through the Arduino IDE). We will always determineSpeed ​​upreal-time Pacer block parameter to "1", which means that our Simulink model has the same pace as real-time, i.e. 1 second of simulation time corresponds to 1 second of physical time.

As shown, the input voltage command is generated by the pulse generator block. This block generates 0 or 1 values ​​which are then sent to the Arduino digital write block. You can set the pulse generator block by double-clicking on itInternshipat "0.1" seconds. Next, we'll set the block to toggle its output between two states every second (every 10 samples). Since we are using channel 8 for digital output, double click on the Arduino Digital Write block to configurePinAct8from the drop-down menu. We will let the block sampling time be taken from the pulse generator block. Input 0 to the digital writing pad generates an output voltage of 0 V on the corresponding pin, while input 1 to the digital writing pad generates an output voltage of 5 V.

The Arduino analog reading block reads the output voltage data through the analog input A0 on the board. By double-clicking on a block we can place itPinAct0from the drop-down menu. We were sitting tooInternshipback to "0.1". Then the standard Simulink model plots the recorded data (LED voltage) through the Scope block.

After creating a Simulink model, you can launch it from the drop-down menuSimulation > Runningor by pressing the "play" button on the toolbar. The model we created was set to run for 10 seconds. After starting the model, the LED will flash alternately. By double-clicking on the range and pressingcar scalethe button on the toolbar then generates the chart as shown below.

Control Tutorials with MATLAB and Simulink (7)

The data we plotted here corresponds to the voltage across the LED. Because LED and resistor are good approximationsstaticdevice, the voltage across the LED jumps from zero to non-zero almost immediately each time the digital output is turned on. At this point, the data is represented in bits. Since the Arduino board uses a 10-bit analog-to-digital converter (ADC), the analog input range from 0 to 5 volts is represented by integers from 0 to 1023 (Control Tutorials with MATLAB and Simulink (8)). Therefore, the recorded data shows that when turned on, the LED has a voltage drop of about 2 volts (Control Tutorials with MATLAB and Simulink (9)volts). This means that a "real" diode has some resistance if it is forward biased; it does not behave like a perfect short circuit.

In this example, our system can easily achieve our 0.1 second sampling time. There may be cases where you want to do more calculations in Simulink or when you want to use a shorter sampling time because the dynamics of the read signal is very high. In such cases, you can try to use a sampling time that cannot be achieved continuously. This can be a problem, especially since Simulink runs on a non-real-time operating system that has many other (higher priority) tasks that may limit the speed at which Simulink can communicate with the Arduino board. One way to check that the sampling time was met is to look at the MATLAB command window after running the Simulink model. If Simulink was able to meet the recommended sampling time, you'll find a report on the command line showing the average idle time per time step as shown below. If the model was unable to meet the timing requirements and thus there was no idle time, this statement will not be on the command line.

Control Tutorials with MATLAB and Simulink (10)


Additional activities that can be performed include: You can read an external signal, e.g. from a button, and on its basis determine when to turn on or off the LED. Alternatively, you can use the PWM signal (analog output block) to change the brightness of the LED. Finally, you can replace the LED with a capacitor to observe adynamic, insteadstatic, system. Such an RC circuit has been studied in detailActivity 1this guide.

Published in MATLAB® 8.2


Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated: 08/15/2023

Views: 6067

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.