Programming Arduino using Matlab | applied electronics engineering


Programming Arduino using Matlab

By Applied Electronics - Monday, January 30, 2017 No Comments
Arduino can be programmed and it is simple. You just have to add the Matlab support for Arduino Hardware from the application addons to you matlab. Once installed you are ready to go. It is not complex process, just few steps. We recommend you try this as Matlab has so many built in functions and signal processing capabilities you just want to use with arduino.

We walk you through the process here.

First of all add the Matlab Support for Arduino Hardware. To do this click on the Add-Ons and select/click Add-Ons support.

Programming Arduino using Matlab
You will see a list of available add-ons installed on your system and not installed add-ons. Go down the page until you see the Matlab Support for Arduino Hardware. Once you see it click on it and follow to install it into your computer.

Programming Arduino using Matlab

Now you are ready to go.

Plug in your Arduino and create a simple LED circuit with a resistor as shown below.

Programming Arduino using Matlab

The LED is connected via the resistor to the 10th digital pin.

Now we can write a Matlab script or directly write commands to play with the LED. Here we will show you using script.

Example 1:

The following is the Matlab code to turn ON and OFF the LED.

% create arduino object

a = arduino();

% to turn on/off LED

% writeDigitalPin(a, 'D10', 0);
% pause(2);
% writeDigitalPin(a, 'D10', 1);

What we have done here is- first we create an ardunio object. Then we have used the writeDigitalPin() function to send either 0 which means Off or 1 which means On to the pin 10. the writeDigitalPin() function accepts three argument which are the arduino object, the digital pin which is D10 in this case and the value to write which is either 0 or 1. Thus writeDigitalPin() matlab function is same as writeDigital function in Arduino. The pause() function is same as delay but accepts value in seconds. So pause(2) means delay of 2 seconds.

Now we just run the code and the LED will Blink for the 2 second in between low(0) and high(1).

You can watch the video demonstration below for this example 1.

Example 2:

In this example we will blink the LED for 2 seconds, that is the LED will turn ON and OFF for 2 seconds. Following is the matlab code.

% create arduino object

a = arduino();

% blink LED for 2 sec

for k=1:20
    writeDigitalPin(a, 'D10', 0);
    writeDigitalPin(a, 'D10', 1);

In this example we used for loop to write 0 and 1 twenty(20) times. Each loop is of 1 second(0.5ms+0.5ms). Everything else is the same as in example 2.

Now the following video demonstrate these two examples on real hardware.

This tutorial demonstrated how you can use matlab to program arduino to play with LED. See other tutorials on Arduino such as How to control LED using LDR using Arduino and How to read analog signal into Arduino.

No Comment to " Programming Arduino using Matlab "