Setting Up Your Photon to Work With MATLAB - MAE221/Thermodynamics-Lab GitHub Wiki

Setting Up MATLAB Compatability

This tutorial will teach you how to set up your Photon so that it is capable of running off of MATLAB.

Flashing Code

In order for your Photon to execute commands, you need to send it a program. This is known as flashing. You can flash code at build.particle.io. We will not delve into how exactly to program in Particle IDE, however, the Particle company has set up several very helpful tutorials discussing how to use the Photon and code in Particle IDE. The links for these tutorials can be found here and here. We highly encourage you to look into Particle IDE as it will be extremely helpful for future projects.

For now, we will only be flashing some intro code to make sure that your Photon is properly connected. Go to the Particle Build website. To the left, you will see a toolbar. The toolbar contains helpful gadgets that will assist you in using your Photon. The top left has, from top to bottom, the 'Flash' icon, the 'Verify' icon (checks your code to make sure it follows correct syntax), and the 'Save' icon (save your code to your computer so you can flash it via CLI. We will not be doing this). The bottom right has, from top to bottom, the 'Code' icon (which you will use to write programs), the 'Libraries' icon (allows you to use special function libraries for various chipsets, we will not be using this), the 'Help' icon, the 'Docs' icon (where several helpful tutorials are located), 'Devices' icon, the 'Console' icon (where you can manipulate your Photon and receive outputs remotely), and 'Settings' icon.

Click on 'Devices' (the target icon) and ensure that your Photon is there. If not, repeat the steps in the Photon Setup manual.

Now go to 'Code' (the two angled brackets icon). You will now see a list of sample programs. Connect an LED from D0 to GND (preferably the one that came with the Photon, not the square LED). Under example apps, click 'Blink an LED' and hit the 'Flash' icon in the top left. Verify that the LED connected to D0 and the LED built into the Photon at D7 are blinking. This may take a while. You may also notice that your Photon's status LED starts flashing random colors. This is normal. Most of your Photons have never been used before and need to update. Wait a few minutes and allow the Photon to update. Eventually, the program should initiate the LEDs will start blinking. If not, hit the 'Flash' icon again. If the LEDs are not blinking, verify that you are flashing your code to the correct Photon and that your Photon is connected to the internet (it should be breathing cyan). If you are having trouble figuring out what is wrong, please consult your Lab AI.

Flashing MATLAB Integration Code

Now that you have verified that your Photon is properly connected to the internet, it's time to flash our MATLAB integration code. To do this, click 'Create New App' in the Code tab. Then go to this link which has the setup_photon.cpp program. Copy and paste the program into the Particle IDE. Give the app a title click the Save icon in the top left. After that, click the Flash icon. Your Photon's status LED will begin flashing various colors and eventually begin to breathe cyan again. If this does not happen, please consult your Lab AI.

Using MATLAB

You will first need to download MATLAB. Follow the instructions found here to download MATLAB, install it, and activate your license (you get one for free through Princeton).

Once you have MATLAB installed, you will need to install a few apps to make sure that our code works with your installation of MATLAB. To do this, click the Apps tab. Click 'Get More Apps'. Search for and install the following add-ons: Curve Fitting, Instrument Control, and Regression Learner. These apps are free and included with your Princeton license.

Once you have installed everything for MATLAB, you will need to make a folder for your code. All programs that are interconnected need to be in the same folder or you have to add a path to that folder (using the 'addpath' command). This means that all your Photon programs have to be connected to the Photon.m and IP_Photon.m programs.

Click on the editor tab and click 'New'. Copy and paste the Photon.m program into the editor and save it as Photon.m in the folder you created. Now click 'New' again and copy and paste the IP_Photon.m and save it in that folder again. These two programs are the basis of communicating with the Photon using MATLAB.

If you have never used MATLAB before, you should look through some of the programs we have provided you and look at a few tutorials. You should also look at the MATLAB help page.

flash.m

To check that you have everything setup correctly. Open up Matlab and copy the following code into a new script file:


    %Enter your photon name%
     name = 'myphoton';
     %Enter the unique access token for your photon%
     atoken = 'abc123';
     %Set the digital pin for the LED%
     dpin = 'D7';

    % Create a new Photon Object %
    g = Photon(name,atoken);
    % Display some info on your connected photon, Make Sure it is powered on!! %
    g.getConnectedDevices()

    % Check if device is connected %
    if g.getConnection
       
        g.digitalWrite(dpin,1); %Set digital pin hi
        pause(1)                %Wait 1 second
        g.digitalWrite(dpin,0); %Set digital pin low
        pause(1)

    end

Replace 'myphoton' with your Photon name. Replace 'abc123' with your access token. Note: this is NOT your Photon ID. To find your access token, return to the Photon website. On the bottom left, you should see a gear. Click this, and you should be able to find your access token. Now run this code in the Matlab command window and you should see your particle photon LED blink once then turn off.

tinker.m

You will first need to ensure that your Photon is plugged into your computer. Open up your terminal again and run the particle serial list command using Particle CLI. This will output the name of your Photon and the serial port it is connected to. Remember this. You may have to run the particle serial list command again if you disconnect and reconnect your Photon as the serial port number may have changed.

To get you familiar with some of the Photon-MATLAB integration commands, you will be using the tinker.m program. Create a new script and copy and paste tinker.m into that script. You will need to partially edit this script. At the top of the script, change the 'name' variable to the name of your Photon. Change the 'atoken' variable to the access token for your Photon. Change the 'port' variable to the serial port you found via Particle CLI.

Hit the run command. This will run the tinker.m command and allow you to manipulate the Photon using MATLAB's command line. The commands/functions you will use for this are found here.

NOTE: When you will be building programs in the future for the Photon, you will need to include the name, atoken, and port variables. You will also need to create a Photon object using this line of code g = Photon(name, atoken, port);. You can then use the commands/functions for the Photon-MATLAB integration by accessing the Photon object (ex. g.analogRead(A2)).