arduino with matlab
TRANSCRIPT
Arduino-MATLAB Communication EE185
1 | P a g e
Objective In this lab, MATLAB will be used to communicate with the Arduino to do some interesting things. The Arduino platform
can be considered a convenient data acquisition device, and the tools in MATLAB can be used to analyze this data
extensively.
Videos Watch the video about Arduino-MATLAB posted on the labs website this week.
Arduino Setup This section describes how to set up the Arduino and prepare it to communicate with MATLAB.
Download the “arduino_ml” zip file provided
Unzip its contents to a known location (somewhere convenient, where the rest of this lab’s documents will be
stored)
In the Arduino environment
In the environment, open the “adiosrv.pde” program
Connect the Arduino hardware via USB
Upload the “adiosrv.pde” program to the Arduino, it should compile and upload with no errors as-is
The adiosrv.pde program allows the Arduino to act as a “server” for MATLAB, constantly listening for MATLAB
commands. The Arduino makes it easy to capture data, and MATLAB is a powerful tool for analyze this data.
MATLAB Setup This section describes how to set up MATLAB to communicate with the Arduino. Some basic commands will also be
discussed.
Open MATLAB, and in the “Current Folder” browse to the
“arduino_ml” folder (this makes the “arduino_ml” folder
the current directory)
In this current directory, a file called “arduino.m” exists.
This is the file that will be used to communicate with the
Arduino.
In the command prompt, type: help arduino. This will confirm that MATLAB recognizes the “arduino.m” file
exists.
In the command prompt, type: a = arduino(‘COM4’), then type delete(a)
NOTE: in this example, the Arduino uses the serial communication port COM4. It may be different per computer.
To know what port the Arduino is using, right-click on Computer Properties, then click “Device Manager.”
Expand the “Ports” category and note what COM port the “USB Serial Port” is using.
If the command was successful, a list of Digital Pins will be shown. If not, contact the TA.
Arduino-MATLAB Communication EE185
2 | P a g e
Experiment 1 In this experiment we will design a system that turns on one of three LEDs based on how much light is detected. This will
be done using the Arduino as the data acquisition device and MATLAB as the control software. The following is a block
diagram of this system:
MATLABArduino
LEDs
Photoresistor
The following Arduino-MATLAB functions are used. Use the ‘help’ command to describe these:
Function Description Example
pinMode
digitalWrite
analogRead
Part 1 1. Make sure the adiosrv.pde file has been uploaded to the Arduino, and that the Arduino is plugged into the
computer via USB
2. Make sure the MATLAB current directory is set to the directory where the arduino.m file has been saved
3. In the MATLAB command window type a = arduino(‘COMX’), where COMX is the com port associated
with the Arduino
4. If no errors appear when doing this, the Arduino is ready to accept MATLAB commands
5. In the MATLAB command window, type delete(a)to close the com port.
Part 2 Create the following:
1. A voltage divider circuit that has the photoresistor as R1, a 10kΩ resistor as R2, 5V as Vin, and analog input pin 2
as Vpin.
2. A MATLAB script that:
a. Turns on a green light for full brightness
b. Turns on a yellow light for medium brightness
c. Turns on a red light for low brightness
Arduino-MATLAB Communication EE185
3 | P a g e
Challenge Keep the hardware setup as in experiment 1, we will use the voltage divider Utilize Arduino-MATLAB communication to
create a script that does the following:
1. Obtains n analog readings (the user specifies how many), and stores them in an array, and then plots them.
2. When the first 1/3 readings are being taken, the green light is illuminated.
3. When the second 1/3 readings are being taken, the yellow light is illuminated.
4. When the final 1/3 readings are being taken, the red light is illuminated.
MATLABArduino
LEDs
Photoresistor
Conclusion This lab showed that the Arduino hardware can interface directly with MATLAB. This is very useful because acquiring
physical data from the Arduino (e.g. seeing a light blink or obtaining sensor information) is very easily done, and the
tools provided by MATLAB can allow for powerful analysis.
(1/3)n (1/3)n (1/3)n
n readings
Start… …Finish