Skip to main content Skip to local navigation

Matlab and Arduino (Fall 2023)

[updated in 2024]

I use Arduino-compatible hardware in two of the engineering programming classes that I teach here at York University. Rather than program the boards directly, I have the students install a little program on the Arduinos that can communicate with the programs that the students actually write on their own computers. For the first course, the students learn how to do this with Matlab and, in the second course, the students learn how to do this with Java.

Here I’ll show you a few examples of how this works with Matlab. (more videos on the EECS 1011 playlist here)

LED on an Arduino

First, is the “light up an LED” example:

Light up an LED on an Arduino board using Matlab: https://youtu.be/3V3dSSFYVSI

Read a button on an Arduino

Next, we look at how to read a button on the Arduino board:

Read a button on an Arduino using Matlab https://youtu.be/i65t_ZpSWwM

Rotary Potentiometer on an Arduino

Next, let’s use Matlab to read a rotary potentiometer on the Arduino board.

Reading an analogue sensor on an Arduino board using Matlab https://youtu.be/nIKF2ROgk-U

Play a sound from a buzzer on the Arduino

Next we can play a sound on a buzzer connected to the arduino board.

Playing a sound from an Arduino board using Matlab (https://youtu.be/8nVpZ0uHKz8)

Light intensity measurement using an Arduino and Matlab

Other analogue sensors can be monitored using Matlab. Here is a light intensity sensor being read:

Measuring light intensity on the Grove / Arduino board.

Soil Moisture sensor

Connecting a soil moisture sensor is a little more complicated. But it’s doable using the right cabling:

Soil moisture sensor connection to an Arduino for reading from Matlab (https://youtu.be/nEdGbxCjxJY)

Water Pump & MOSFET Connection

The following video is longer than the others because it requires the connections of multiple cables and adapters. Here we connect an external water pump to the Grove / Arduino board via a MOSFET switch. The switch is required because digital devices like the Grove / Arduino board can’t provide enough current or the current voltage to the pump directly. The switch takes the weak signal from the Grove / Arduino board and amplifies it to a level that can be used to drive the pump using the 9 volt battery.

Connecting a water pump and MOSFET switch to the Arduino board. (https://youtu.be/qw0vNtWBtR0)

Reading an Air Pressure Sensor

Reading an Air Pressure Sensor (BMP280) (https://youtu.be/4Fa8xraOITk)

Reading an eCO2 Sensor

Reading an eCO2 sensor (https://youtu.be/AG_7A2ElHbI)

Reading a Temperature and Humidity Sensor

Reading a DHT20 Humidity and Temperature Sensor (https://youtu.be/asKWLbl8SGI)

Making a hobby servo move

Hobby servos and Matlab.

Connecting a relay and a motor

relays, motors and matlab.

OLED and Matlab

Using the GitHub library from YorkU student Aradhya C.

Github library for the OLED: https://github.com/AradhyaC.


Setting this up

In order to get the above examples working we need to install Matlab and we need to install the Arduino support package.

For Windows that looks like this:

Installing Matlab on Windows (https://youtu.be/NTzuaNKTx2E)

and

Adding the Arduino Support Package for Matlab in Windows. (https://youtu.be/6xgea6-Tx1Y)

While on macOS, we currently have to use Matlab 2022b (not the latest version of 2023a or b):

Install 2022b and the support package will work for Matlab on Apple macOS machines. (https://youtu.be/LeLe_6I4–s)

Edit: Nov 18, 2023: adding DHT20, BMP280 and SGP-30 sensor videos.

Edit: Nov 20, 2023: servos and relays added.


a pen

James Andrew Smith is a Professional Engineer and Associate Professor in the Electrical Engineering and Computer Science Department of York University’s Lassonde School, with degrees in Electrical and Mechanical Engineering from the University of Alberta and McGill University.  Previously a program director in biomedical engineering, his research background spans robotics, locomotion, human birth and engineering education. While on sabbatical in 2018-19 with his wife and kids he lived in Strasbourg, France and he taught at the INSA Strasbourg and Hochschule Karlsruhe and wrote about his personal and professional perspectives.  James is a proponent of using social media to advocate for justice, equity, diversity and inclusion as well as evidence-based applications of research in the public sphere. You can find him on Twitter. Originally from Québec City, he now lives in Toronto, Canada.