EE314 Systems Spring Semester 2018 College of Engineering Prof. C.R. Tolle South Dakota School of Mines & Technology Lab 4 Rev. 1 Open Lab Due COB Friday April 6, 2018 In this lab we will setup Matlab s Simulink to work with an Arduino Mega 2560. System Setup: We will also program, compile, and deploy a piece of embedded test code on to our Arduino Mega 2560. Some general background information pertaining to the lab is given in the table below: 1 Matlab 2017b was used in the production of this lab handout. 2 When appropriate, utilize Matlab s publishing features to produce your lab report as much as is reasonable. 3 Useful Matlab commands or Simulink blocks: Simulink, constant block, gain block, Arduino PWM block, Arduino Analog in Block, Scope Block, Transfer Function Block, PWM Generator (DC-DC) Block, Matlab and Simulink Arduino Support Packages. First obtain an Arduino Mega 2560. They are available from a number of sources on the internet. One such vendor is Sparkfun, a link to an Arduino Mega 2560 at their site is given below: Hyper-link to Sparkfun s Arduino Mega 2560 Simulink supports other Arduino boards as well however for this class it is suggested that you use the Mega 2560 since it has more memory and resources then most other Arduino boards. The student should be made aware that using high level programing tools like Simulink allows for quicker development times at a cost of more inefficient code bases. These inefficiencies include both larger code bases, use of additional chip resources, and added code complexity to many applications that directly effect their speed of operation. However for the right applications, the simplicity of implementation provided by such tools far outweigh any code inefficiencies. Once you have obtained your Arduino you need to install Matlab s Arduino and Simulink s Arduino support packages. This is described in the next several pages of this lab. First we need to add a few packages to Matlab and Simulink. We can do this by selecting Add-Ons Get Add-Ons function within Matlab s tool bar: 1
Next search for arduino add-ons: Select the Matlab Support Packages for Arduino Hardware (double click on this package): Follow the instructions to install the package Not all of the screens are included below but the install proceeds mostly as follows: 2
3
As a side note, there are a lot of other Adruino Based Matlab support tools available: 4
Next select the Simulink Support Packages for Arduino Hardware (double click on this package): Follow the instructions to install the package Not all of the screens are included below but the install proceeds mostly as follows: 5
As with the Matlab tools, there are a lot of other Adruino Based Simulink support tools available: Now that the Matlab and Simulink tools are installed we are ready to create a new Simulink project. Start up Simulink via the command line or Matlab toolbar. Navigate to the Simulink Support Package for Arduino 6
Hardware on the New Tab and select External Mode: This will create a new untitled.slx file that is setup to execute on your Arduino Mega. Save the file as ee314 lab4: Review the documentation provided on the new Simulink model page. Note that the Simulation Mode is set to External in this new file. External implies that portions of the Simulink model will be executed on the Arduino hardware and portions will be executed on your computer system, i.e. inputing data and changing set points as well as displaying scope data is accomplished on the main computer system while other portions of the code are executed on the embedded processor. In this lab, we will be using this simple basic starting model to test our hardware setup and functionality for programming our Arduino via Simulink. We will also explore a few important measurement and data acquisition concepts while we have the chance. Let s familiarize you with what pre-made driver blocks exist for your Arduino. To see these, open the Simulink Library Browser and navigate to 7
the Simulink Support Package for Arduino Hardware. First look at the Common library block set: Lab Procedure The functional blocks shown here provide the basic hardware support models for your embedded system. In this lab we will only focus on the A to D (analog in) and PWM (poor man s analog out when used with a low pass filter) blocks provided in our newly created Simulink model. We need to make a few useful changes to the model created for us, they are listed below: 1 Update the Dimmer Block sample time 2 Update the Analog In s sample time (if you get a communication error then slow this down to 0.001, you might also need to up the com port baud rate within windows device manager and within Simulink model parameters to 115200 from default 9600.) 3 Add some scaling blocks to our sub system block so that the dimmer value is scaled from 0-100% and that 8
the Analog reads in volts 4 Reconfigure the scope s output display 5 Change the project title to EE314 Lab4 Now before we discuss the hardware implementation side of this demo, let first create a expectation for our demo. Our in our model demo, we will be exploring both the poor-man s D/A via a low pass filter and a PWM. In this case we will use a single pole R-C lowpass filter with a break frequency of 1 rad s. Create a Simulink model for this 9
device. The ideal poor-man s D/A model is shown below: Evaluation & Report What we want to do is compare this idealized model with some real hardware. Below are some questions to consider and fully discuss within your lab report: 1 Run the idealized PWM-lowpass filter model, does it work as expected? 2 What might be the limitations of this type of D/A? 3 What kind of Loads could it service? 4 What are the limits of the PWM unit? 5 What happens if we over drive the power requirements from the PWM? Short out our current limiting 3KΩ resistor, what happens? 6 What are the effects of using an oscilloscope to look at our signals for this system? 1x probe (add a 1MΩ resistor in parallel with the capacitor)? 10x probe(add a 10M Ω resistor in parallel with the capacitor)? 7 What have you learned within this hardware lab? 8 What issues or problems did you encounter completing the lab? In order to explore these ideas, we need to add some hardware to our Arduino board. Using a simple bread board, jumper wires, diode, capacitor, and resistors implement the following circuit using the Arduino s PWM pin 13, Analog pin A0, and Ground pins: 3KΩ 10MΩ + PWM13.1µF A D A 0 GND Test circuit that simulates the use of a 10x oscilloscope probe on our A/D input: 3KΩ 10MΩ + PWM13.1µF A D A 0 10MΩ GND 10
Test circuit with no current limiting resistor for our PWM output driving our diode: 10MΩ + PWM13.1µF A D A 0 GND Please complete your formal lab report by due date provided at the top of this lab assignment. 11