How to control Arduino using MatLab

Posted by Momen 02/08/2017 0 Comment(s)

This tutorial will teach you basics about control Arduino using MatLab and learn how to do simple project like blinking an LED.

         Overview.

What is Matlab:

Is a high-performance language allows implementation of algorithms, creation of user interfaces and technical computing. Where problems and solutions are expressed in familiar mathematical notation and can interfacing with programs written in other languages, such as C, C++, Java, and Python.

Matlab supports two primary workflows using the simple traditional Arduino programming:

  • Read, write, and analyse data from Arduino sensors
  •  Develop algorithms that run standalone on the Arduino device

 

What is Arduino:

Is an open source, computer hardware and software company for building digital and interact with devices, Arduino board are able to - light on a sensor, turning on an LED and control motor.

After this tutorial, you will learn how:

  1. Set up the Matlab using package for Arduino
  2. Control Arduino to Blink LED by send a command to it using Matlab.
  3. Build a simulation Model.
  4. Download and run the Model on Arduino Uno.

 

  • The Matlab Support Packages for Arduino Hardware lets you use Matlab to communicate with your board through USB cable.

Software:

  1. Matlab software
  2. MathWorks Account

Hardware:

  1. Arduino
  2. Breadboard
  3. 220 Ohm Resistor
  4. Light- Emitting- Diode (LED)
  5.  Jumper leads.

 

            Implementation

Before going further, you have to install Matlab using MathWork Account, then open it and the interface will look like this.

 

MatLab-Interface

Getting started:

Hardware Steps:

  1. Connect D9 on Arduino with the Resistor
  2. Connect other leg of Resistor to the long leg of LED
  3. Connect short Leg of LED to the GND
  4. Turn on the Arduino and connect it to the Laptop.

Connect the components as the figure bellow:

Connect-components

Software steps:

  1. Click on + Add- Ons on the top right of interface, then Get Hardware Support Packages, to download support package for Arduino.

get-Arduino-support-Package

      2. A new interface will open, choose internet (recommended) and go to next.

download-Arduino-package

     3. Now you will see a list of many Support Package, choose Support Package for Arduino  and click next

Arduino-support-package

    4. Log in to your MathWork  Account, chick I accept box,  click next, next and finally install.

Note: installation will take a while, so please be a patient.

install-Arduino-packages

       5. Click finish and now you will be able to control Arduino using Matlab.

       6. Click on Simulink Library , new window will open and you will see a list on the left, click on plus sign behind Simulink Extras and go to Simulink Support Package for Arduino

simulink-library

      7. On top lift of the same interface, click on Add blocks and you will notice the anther new interface will open, click on Digital Output and drag it to those new interface.

 

click-on-digital-output

Drag-digital-output

        8.Double Click on Digital Output  and chick the pin number if it 9 or not, if not change it to 9 and press OK

Note: you can send commands to Arduino based on pin number, So here you will control LED over D9 on Arduino board.

pin-number

      9.Go to the list of libraries, click on Sources and drag Pulse Generator behind Digital Output

drag-pulse-generator

      10. Double Click on Pulse Generator, on Parameters Fields, choose Sample based as Pulse type, 10 as Period, 5 as Pulse width and 0.1 Sample time then press OK.

pulse-generator-information

     11.Click on Sinks where you can find it on list of libraries, drag Scope to the same window with Pulse Generator and Digital Output.

add-sinks

      12.Connect Pulse Generator with Scope, click on run on the top of window and double click on Scope, a new window will open that show the pulses generated by pulse Generator.

Note: That pulses will sent to Arduino to blink the LED, when high pulse, LED will turn on and when low pulse, LED will turn of and so on.

connect-generator-to-scope

 

       13.Delete Scope (by click on it then press delete on keyboard), connect pulse Generator with Digital Output and save your work as new file.

connect-generator-with-digital-output

 

     14.Click on Tools, then run on target hardware and prepare to run, select Arduino Uno as target Hardware, automatically as set host COM port, default as analog input reference voltage and 9600 as serial baud rate, Now the commands will download to Arduino and you can start communicating with it.

The-target-is-Arduino

15.Click again on Tools and Run. Here notice the LED, it should blinking and the project finish.

run-the-program

 

Now you have basics about Matlab communicating Arduino and you can do more and more projects using it such as:

  1. Set analog and digital sensor data from your Arduino board
  2. Control other devices with digital and PWM outputs
  3. Drive DC, servo, and stepper motors (also supports Adafruit Motor Shield)
  4. Access peripheral devices and sensors connected over I2C or SPI
  5. Communicate with an Arduino board over a USB cable or wirelessly over Wi-Fi
  6. Build custom add-ons to interface with additional hardware and software libraries

Leave a Comment