ad space

LED blinking - Programming Arduino using Matlab 1

In this programming Arduino using Matlab tutorial you will learn how to write Matlab code that that blinks a LED connected to Arduino using Matlab. It is like writing main.c file where you write the C code. You can also write program and execute the program directly from the Matlab command prompt but here we will use Matlab script file(.m).

In order to complete this tutorial you need to install Matlab support package for Arduino hardware. If you don't have already installed and don't know how, then see the tutorial Programming Arduino using Matlab/Simulink Setup.

In this tutorial, we will write a simple LED blinking program.

1. Create a directory and create a new a new script file in that directory.

Using mkdir function create a new working directory. Here we have used the name of directory as "programming-arduino-using-matlab". And go into that folder using the cd command.



2. Create a new script file

Now inside the folder created above, create a new script file called "led_blink". 


And write the following following program.

%create arduino object a using arduino() function
a = arduino();

%create a name variable led for digital pin D9
led = 'D9';

%turn on/off led using for loop
for k=1:10
    disp('LED ON');
    writeDigitalPin(a,led,1);
    pause(1);
    disp('LED OFF');
    writeDigitalPin(a,led,0);
    pause(1);
end

%display messsage to close the arduino board
disp('Arduino Close');

%clear object variable a from matlab memory
clear a;

In your matlab editor you should have something like this.

LED blinking - Programming Arduino using Matlab 1


What we wrote and why in the program is the following. First in order to connect matlab to Arduino, we create an object variable called "a" using arduino() function. The arduino() function can accept parameters like the com port and which arduino board is being used. But in our case we didn't pass any arguments to the arduino() function because only Arduino UNO is connected to computer and Matlab will automatically know and configure the Arduino board connected to it. But suppose you had multiple Arduino boards connected to the computer then you should specify the other parameters as we will show you how in later tutorials.

The next line led = 'D9' is just giving alias name led to the digital pin 9. Then we used a for loop to create turn on and off the led connected to digital pin 9. Inside the loop, we have used disp() function to display message "LED ON" and "LED OFF" to user in matlab. This is not actually required and is used for visual indication only. The function that turns on and off the LED from the matlab program is the writeDigitalPin() function. The first argument is the Arduino object 'a' that we created earlier, The second argument is the pin we are using which is led variable declared earlier. The third argument is the high or low which is 1 or 0. The pause() function stops execution of program for the real number in second specified. That is it creates a delay in seconds.


3. Connect LED with resistor to Arduino and Arduino to Computer

Before executing the above program, we should connect a LED with resistor of 220Ohm to Arduino. Then connect the Arduino to the Computer using the USB cable.

LED blinking - Programming Arduino using Matlab 1


4. Executing the led blink program

Once Arduino is connected to PC, open matlab and in the command prompt, type in the name of program file "led_blink" that we created earlier. Then hit the Enter key to run the program code.

LED blinking - Programming Arduino using Matlab 1


If everything worked out ok, then you should see message as shown above, displaying LED ON and LED OFF as programmed. The LED should also be blinking. To see how it works watch the following video demonstration. 



Post a Comment

Previous Post Next Post