ECET 307, Bonus Lab Purdue School of Engineering and Technology, IUPUI Electrical Engineering Technology, ECET307 Bonus Lab, Implementing Matlab to Arduino Dev Board Objective: Learn how to compile and run Simulink Models on an Arduino Board. Requirements: Other Recources: Arduino Uno, Due, Mega, Mega ADK, Leonardo, Micro, Mini, Nano 3.1, Pro, & Fio Drivers for Arduino Boards (Provided). Admin rights to install drivers on School computer(craig Stevens) Four wires. LED and 330Ω OR 220Ω resistor Matlab Arduino support files. Visual C++ Professional(should be on computer already) Prelab: Step 1: See a tutorial Video on the following Link for a basic application: (You will be directed to a different example in this assignment). http://sti.tice.ac-orleanstours.fr/spip2/img/pdf/working_with_arduino_hardware.pdf https://learn.adafruit.com/how-to-use-matlab-and-simulink-witharduino/overview http://www.mathworks.com/help/supportpkg/arduino/examples.html http://www.mathworks.com/videos/programming-arduino-uno-with-simulink- 86316.html Step1: To be able to download your Simulink code into the Arduino board, you must get the required drivers installed on the Windows machine. Please see Craig Stevens, he has the authority to do this. Have the drivers ready; download them to your machine from the lab folder. When he does this and is in Device Manager, take note of what COM port your Arduino is assigned to. It will likely be a small number, 3 or 4. Step2: Download the Hardware add-ons for the Arduino board from within the Matlab window.
Select Install from Internet And hit Next. Yours should say Install not reinstall like shown here. Select all packages. It will proceed to download and install.
Trouble Shooting At This Point: A couple of things can prevent you from continuing or installing the packages properly. Matlab can hold onto Arduino packages and not remember(the pointer file is locked.) If someone has used Arduino with Matlab on the system you are working on, simply delete the Arduino folders from Matlab and reinstall them. C:\MATLAB\SupportPackages\...
Another problem you can encounter, although less likely on IUPUI s systems is not having a recognized C compiler. From the Matlab Command Window, you can test to see if it is configured properly. Type mex setup. From the Command Window, with the Arduino Uno plugged in, you can test to see if the communication is working. Type a = Arduino( com ). In this case, the Arduino was registered to com14. Step3: Prepare your Arduino board to talk to Simulink. If you followed the previous step and communicated with the board from the Command Window. Unplug your USB cable to disengage the connection and then re-plug it back in. In the Simulink window, navigate to Tools/Run on Target Hardware/Prepare to Run /. Select your board and hit OK. We ll assume UNO in this case but any of the listed boards would work fine. Keep in mind that different boards have different PWM, I 2 C, SPI and etc. pinouts.
You can either select automatic or manual port configuration. Stick with automatic unless it gives you trouble. Then look in the Windows Device Manager for the COM port that the Arduino is assigned to.
Step4: Test out with Simulink. If your installation of the package worked, the Simulink Library Browser should now contain blocks for the Arduino. Create the schematic shown below to quickly test. If it compiles and downloads you ll see Rx & Tx on your board flashing and Matlab won t have errors. You can double click on the pulse generator to get different pulse rates if you wish. The default setting is around 1Hertz. Use two wires to connect ground and the output from the board, PIN9, to an oscilloscope or your LED. You should see your square wave or pulses of LED. Exercise 1: Find the transfer function of the following circuit with values R=3.14, L=1mH. Vin Vout V out V in = R L s+ R L Exercise 2: Create the Simulink Model shown below. Take note that we need to use the Data Type Conversion blocks to change the input and output types of the Transfer Function block; this can be found in the Logic and Bit Operations Tab. Also used is the Compare to Constant block; found in the Math Operations tab. The Digital Output of the Ardiuno is of type Boolean but the output type of the transfer function is double, it needs to be converted. The same is true of Digital Inputs. Analog Input will produce a type of INT with a value from 0-1023 on the UNO board. We use the compare block to know when the signal is high. For this exercise, multiply the R/L value by two. Try and think about why this is necessary.
Exercise 3: Build the Circuit. We are using a DC offset and a square wave to help protect the AtMega328 pins. DO NOT PUT MORE THAN 5V INTO THE ARDUINO! The goal is to catch each high portion of the square wave. The ATD produces a numeric value based off of 5V. We are only using 4V for safety. That makes our numeric range 0-818. Each step size is of the ATD.00489V. We want to send a 1 into the Matlab transfer function every time the wave is high. Comparing to a constant of 300(this is a little arbitrary, 409 would be the zero crossing @ 2V). @ a compare to constant value of 300, any voltage over 1.46V will give us a 1 or True. DO NOT CONNECT THE FUNCTION GENERATOR YET. Exercise 4: In Simulink, before the model is compiled and run, the sample times must be the same. It s also worth checking the sampling rate of the Arduino ATD.
This will be a limiting factor for us. Set MATLAB s to.0001 sampling time. You will need to do this on the Analog Input block and the Configuration Parameters. Change it config parameters to a fixed-step. If these values are not the same, Simulink will tell you.
Exercise 5: Compile the model and run on the Arduino. Start with a low frequency around 1Hertz and work your way up to the assumed cutoff point of 50 HERTZ. Where did the signal actually stop getting through? Why do you think this is? What happens at very High frequencies; try 2.5K, 5K, 10K, 20K, 50K? What do you think is happening (Think Nyquist). Conlusion: Write a conclusion about what you learned, What you liked, didn t like and places that caused confusion? Are you likely to use Matlab with the Arduino again? How many bytes was the Sketch file that downloaded to the board? Why did we need to multiply the target cutoff frequency by 2?