.st0{fill:#FFFFFF;}

Integration of Arduino with MATLAB and Simulink 

 January 30, 2019

By  Aditya Chaudhari

Want to Learn the integration of Arduino with MATLAB? Read this Blog and understand the Basic Input/Output Commands for Arduino using MATLAB & Simulink. Now you can create a Simulink Model and configure the Arduino without even writing a single line of code.

In this Blog Post let us learn the following topics and code the Arduino:

  • Install the required Arduino Hardware Support Packages for MATLAB & Simulink.
  • Testing the connection with Arduino.
  • A Simple Blink Program for the onboard LED of Arduino using MATLAB.
  • Blink Program for external LED using MATLAB.
  • Blink LED without writing a single line of code using Simulink.
  • Read Analog Values from a Potentiometer.
  • Integrate and Understand Servo Motor Rotation with Arduino using MATLAB.
  • More fun with all these applications using basic coding techniques in MATLAB.

Install the required Arduino Hardware Support Packages for MATLAB & Simulink.

One can easily install the Arduino Support packages for MATLAB and Simulink by Navigating to the

For installing Hardware Packages for MATLAB:

Home >> Add ons >> Get Hardware Support Packages >> MATLAB Support Package for Arduino Hardware >> Install >> Install / Download only

For installing Hardware Packages for Simulink:

Home >> Add ons >> Get Hardware Support Packages >> Simulink Support Package for Arduino Hardware >> Install >> Install / Download only

For more details you can read our blog on Installation of Hardware Support Packages below.

Testing the connection with Arduino.

Once you are done installing the Hardware Support Packages, you can opt to setup the Arduino during the same Installation Process and then test the connection by typing the following code in the Command Window.

% To Test the Arduino

a = arduino; % Usage #1
a = arduino(); % Usage #2
a = arduino('COM4','UNO'); % Usage #3

% One can use any of the above usages to test the connection with Arduino.

You can use any of the above usage as per the connection requirement. You can use a=arduino; as the default line to test the connection, if there are any errors in that scenario you can use the other usage as well.

A Simple Blink Program for the onboard LED of Arduino using MATLAB.

Now let us Program the Arduino with MATLAB to Blink the Onboard LED at pin 13. We will use the ‘writeDigitalPin’ function to write the Digital Data to the Arduino.

% BlinkOnboardLED.m

clear; clc; % These functions are used to clear the workspace and command prompt respectively

a = arduino; % Setting up the connect

% Creating a for loop to keep the LED Blinking for 100 cycles. You can set it to 'inf' to keep
% swtiching the state of LED after every 1 second for infinite time.
for i = 1:100
    writeDigitalPin(a,'D13',1) % To switch on the LED
    pause(1); % Add Delay in Seconds for ON State
    writeDigitalPin(a,'D13',0) % To switch off the LED
    pause(1); % Add Delay in Seconds for OFF state
end

% If you want to stop the execution of the code, press Ctrl+C in Command Window.

Blink Program for external LED using MATLAB.

Once we are set , now we can connect an external LED to either pin 13 or another digital pin. In this part, let us connect to Arduino pin 9 and make the changes in the code accordingly. Check the connection as per the circuit diagram shown below the code.

% BlinkExternalLED.m

clear; clc; % These functions are used to clear the workspace and command prompt respectively

a = arduino; % Setting up the connect

% Creating a for loop to keep the LED Blinking for 100 cycles. You can set it to 'inf' to keep
% swtiching the state of LED after every 1 second for infinite time.
for i = 1:100
    writeDigitalPin(a,'D9',1) % To switch on the LED
    pause(1); % Add Delay in Seconds for ON State
    writeDigitalPin(a,'D9',0) % To switch off the LED
    pause(1); % Add Delay in Seconds for OFF state
end

% If you want to stop the execution of the code, press Ctrl+C in Command Window.

Let us do some more fun by asking the user to switch the LED on or off using a switch case in MATLAB.

% BlinkExternalLED2.m

clear all; clc; % These functions are used to clear the workspace and command prompt respectively

a = arduino; % Setting up the connect

time = 100; % 100 Loops
while time > 0
    state = input('Set the LED to\n 1.ON 2.OFF\n'); % Take input from the user and use switch case.
    switch state
        case 1 % When the user inputs '1' in command window
            writeDigitalPin(a,'D9',1);
            pause(2);
        case 2 % When the user inputs '2' in command window
            writeDigitalPin(a,'D9',0);
            pause(2);
    end
    time = time-1;
end

BasicArduino1 | Integration of Arduino with MATLAB and Simulink | MATLAB Helper ®

Blink LED without writing a single line of code using Simulink.

Tired of writing the code for a very simple application? Let us do the same thing using Simulink with just 2 blocks and a scope to view the output. Open your simulink by typing ‘simulink’ in your command window and browsing through the library to find the Digital Output and Pulse Generator Blocks.

BasicArduino4 | Integration of Arduino with MATLAB and Simulink | MATLAB Helper ®

Read Analog Values from a Potentiometer.

Connect the Arduino and potentiometer as demonstrated in the diagram below. The potentiometer can give maximum of upto 5 volts when connected to the power supply of the Arduino and a minimum of 0 volts when the variable is at the lowest position.

% Potentiometer.m

clear all; clc; % These functions are used to clear the workspace and command prompt respectively

a =arduino; % Setting up the connect

% This time let us set the for loop to run for infinite time.
for i=1:inf
    voltage =readVoltage(a,'A0') % This will read the voltage and print the value in the command window.
end

% If you want to stop the execution of the code, press Ctrl+C in Command Window.

BasicArduino2 | Integration of Arduino with MATLAB and Simulink | MATLAB Helper ®

Integrate and Understand Servo Motor Rotation with Arduino using MATLAB.

The servo motor which we are using is a 180 degree servo motor and for demostrating we will be dividing these 180 degree’s in 5 parts each of 36 degrees and stop the motor exactly at the specified positions. The Maximum Digital Value can be 1 and so 1 means 180 degree. Each step of 36 degree equivalents to 0.2 step. This is the reason we use the for loop from 0:0.2:1

%  Servo.m

clear all; clc; % These functions are used to clear the workspace and command prompt respectively

a = arduino(); % Setting up the connect

s= servo(a,'D4'); % Setting up the servo motor

for angle=0:0.2:1 % Dividing the angle in 5 positions of 36 deg each
    writePosition(s,angle) % Write the value to servo
    current_pos=readPosition(s);
    current_pos=current_pos*180;
    fprintf('Current motor position is %d degree\n',current_pos); % Display the value in the Command Window
    pause(2);
end

BasicArduino3 | Integration of Arduino with MATLAB and Simulink | MATLAB Helper ®

Loved the Blog? Gives us your valuable feedback through comments!

Thank you for reading this blog. Do like this blog, if you found it helpful. If you have any query, post it in the comments or get in touch with us by emailing your questions to [email protected]. Follow us on LinkedInFacebook and Subscribe to our YouTube Channel. 

Our experts assist in all fields of MATLAB & Simulink with Communication Option from Live Sessions to Offline Work with Pricing suitable for everyone. If you are looking for one-time expert help, you can go ahead with Pay As You Go Plan. If your task is research-oriented and if your timeline allows, our recommendation would be Research Assistance, a monthly plan with a steady reduction in the expert fee up to six months of continuous subscription. We also offer Corporate Assistance for large projects with annual validity. 

If you are looking for an expert's help and ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert back to you with more details.

Education is our future. MATLAB is our feature. Happy MATLABing! 

About the author 

Aditya Chaudhari

  • What is the Potentiometer Voltage Output Code in MATLAB? I want to create code that matlab read voltage putput in my task ?
    How can I do this ?can you help me ?

    • Aditya Chaudhari says:

      Any Voltage can be read using the command (function) ‘readVoltage’ as below:

      >> voltage =readVoltage(a,’A0′)

      Where A0 is the analog pin where you have connected the device or sensor from which you need to measure the voltage.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >