Handout BlinkingLED
Handout BlinkingLED
Handout BlinkingLED
BLINKING LED
by Carion Pelton
1 O BJECTIVE
This experiment is an introductory lab designed to help set up and become familiar with
the hardware and software that are required for each of the take-home-labs. You will begin
by installing support for the Arduino in Simulink, and installing the Arduino drivers on your
computer. Then you will verify that everything is installed correctly by using the hardware
and software to successfully blink an LED using two different methods for running models
on the Arduino with Simulink: Normal Mode and External Mode.
2 S ETUP
Since this is an introductory experiment, the hardware and software setup portion will be a
part of the actual experimental steps. Therefore, this section will only contain the hardware
and software materials that are needed, and any prior experiments that should be performed
before running this experiment.
2.1.2 S OFTWARE
Matlab/Simulink R2013a or later
? The steps and images related to Matlab/Simulink for this experiment were created
using Matlab/Simulink 2013a. Therefore some steps and images may be a little
different if you are not using this version.
Personal Computer with administrator access
3 E XPERIMENTAL P ROCEDURES
The first exercise of this lab demonstrates how to install the Arduino drivers onto the PC being used. The next exercise shows how to install the Arduino support in Simulink. The final
exercise will verify that the first two exercises were successful. The exercise consists of creating Simulink models to blink the Arduinos built-in LED using Normal Mode and External
Mode.
3. On the right side of the download page, select Windows Installer" as shown in Figure 3.2.
4. A page asking you to contribute should show, select Just Download", unless you would
like to contribute to the Arduino software.
5. An executable file will start to download. Once the download finishes, select the file
and install the Arduino IDE. You will need administrator access on your PC to install
the IDE.
6. When the install finishes, make sure the Arduino is not sitting on a metal or conductive
surface, as it may cause the pins on the bottom of the board to short together. Connect
the USB cable from the Arduino to the computer. The green LED on the Arduino should
turn on, indicating the board is being powered.
If the Arduino is under Other Devices", it may be listed as Unknown Device". To verify that this is the Arduino, unplug the USB from the Arduino
and see that the device is removed from the list. Now reconnect the USB to
the Arduino.
12. Install the support package using the internet and hit Next".
13. A MathWorks account will be needed to proceed, so log in with an existing account or
create a new one.
14. Once successfully logged in, check any boxes next to support packages related to the
Arduino and hit next.
15. Follow the remainder of the on-screen prompts until the installation is finished, and
10
11
There are two methods that will be used to demonstrate proper communication between the
board and the PC: Normal Mode and External Mode. Both methods have their pros and cons,
and, depending on the project, one method may prove to be more beneficial than the other.
Thus, it is important to introduce both methods now so that the student is familiar with them
before starting any of the later labs. A simple experiment to introduce both of these methods is blinking an LED. A more extensive introduction to Normal and External Mode can be
found in experiment Sampling and Data Acquisition. Besides the Arduino and the USB cable, there is no additional hardware needed for this experiment, because the Arduino has a
built-in LED.
3.3.1 N ORMAL M ODE
As indicated by its name, Normal Mode is just the normal method used to download programs onto the Arduino. Usually, with an Arduino, the programs are developed and downloaded to the board in the Arduino IDE, which is not needed for these projects, since we are
using Simulink. A program downloaded to the board in Normal Mode will stay on the board
until another program overwrites it. This means that even when the board is disconnected
from power and reconnected later, the program is still loaded onto the board, and will just
start executing again. The main disadvantage with Normal Mode is that if any data needs to
be recorded, they will be more difficult to acquire than with External Mode. The main advantage with Normal Mode is that it allows programs to run at a much faster rate than External
Mode. The following steps show how to use Normal Mode to blink the onboard LED of the
Arduino. The final Simulink Model will look like Figure 3.16:
18. Open MATLAB, then open a new Simulink Model using either of the following methods:
In the Matlab windows Home" toolstrip, choose File New Simulink Model.
Type simulink in the Matlab Command Window, then press the New Model
icon.
12
20. Under the Libraries section add a Pulse Generator block to the model:
Simulink Sources Pulse Generator
21. Under the Libraries section add an Arduino Digital Output block by selecting:
Matlab R2013a: Simulink Simulink Support Package for Arduino Hardware
Digital Output
Matlab R2013b/R2014a: Simulink Simulink Support Package for Arduino Hardware Common Digital Output
22. Save the Simulink model as any filename.
23. Double-click the Pulse Generator block, make the following changes and then hit OK:
Set Pulse type" to Sample based
Set Period" to 4
Set Pulse width" to 2
Set Sample time" to 1 second
13
24. Double-click the Arduino Digital Output block, make the following changes and then
hit OK:
Set Pin number to 13, this is the pin the onboard LED is connected to.
25. Connect the Pulse Generator block to the Digital Output block. The model should look
like Figure 3.16.
26. Now select Tools Run on Target Hardware Prepare to Run...
14
or Ctrl+B
30. Wait until the the model has successfully downloaded to the board, now observe the
onboard LED of the Arduino shown in Figure 3.22. Describe what the LED is doing.
Estimate and record the timing. Is the timing consistent with the settings you made in
Figure 3.17? Explain.
15
31. Change the Pulse Generator block sample time from 1 second to 0.1 seconds.
32. Download the modified model onto the board. Describe the changes in the LED operation. How has the timing changed? Did it change in the way that you expected?
33. Change the parameters of the Pulse Generator block so that the LED will be on for 4
seconds and off for 1 second. Record your settings, download the modified model, and
describe the resulting operation. If the timing does not produce a 4 second on period
and a 1 second off period, make the necessary changes to the parameters of the Pulse
Generator and test the operation again.
T ROUBLESHOOTING : If there are any errors encountered throughout this section, try the following:
If Matlab cannot find the board when attempting to run the model, try setting the COM
port manually. In Simulink, go to Simulation Model Configuration Parameters. On
the left side choose Run on Target Hardware", then change Set host COM port:" from
Automatically to Manually. Then change the COM port number to the value written
down in the installation section earlier.
If Simulink cannot connect to the board either: try disconnecting and reconnecting the
USB cable, or closing and reopening Matlab.
Any other issues encountered may be solved by reading the Arduino troubleshooting
page located at http://www.arduino.cc/en/Guide/Troubleshooting .
16
17
37. Double-click the Digital Output block and set the pin number to be 13.
38. Double-click the top Pulse Generator block, make the following changes and hit OK:
Set Pulse type" to Sample based
Set Period" to 4
Set Pulse width" to 2
Set Sample time" to 1 second
39. Double-click the bottom Pulse Generator block, make the following changes and hit
OK:
Set Pulse type" to Sample based
Set Period" to 4
Set Pulse width" to 2
Set Sample time" to 0.1 second
40. Set up the Arduino to run in External Mode. Select Tools Run on Target Hardware
Prepare to Run...
41. Set Target hardware to Arduino Mega 2560, and wait a moment for the window to update.
42. In Matlab R2013a make sure to check the box next to Enable External Mode", as shown
in Figure 3.24. In R2013b or later, this step is skipped.
43. Select Solver on the left hand side of the window, then make the following changes and
hit OK.
18
45. Download the model to the Arduino using the same method used for Normal Mode.
The program may not start running automatically so connect or run the model by doing
the following:
Matlab R2013a: Connect to the Target Hardware using
19
46. Record what the Arduinos LED is doing. Estimate timing. Is the timing consistent with
the parameter settings?
47. Double click the Manual Switch in the Simulink Model. The switch should now be
connecting the bottom Pulse Generator to the Digital Output.
48. Record what the Arduinos LED is doing now. Describe any timing changes. Explain
why the timing changed. How is the process different than Normal Mode? Which Mode
is better? Why?
49. Disconnect from the hardware:
Matlab R2013a: Press the Disconnect from Target button
ure 3.28.
, as shown in Fig-
20
21
Any other issues encountered may be solved by reading the Arduino troubleshooting page located at http://www.arduino.cc/en/Guide/Troubleshooting.
22