E5 Lab 9 - Son et Lumière

This week in lab you will work as a group and do some programming, including getting MATLAB to interact with the real world - in preparation for the final project.

On this lab you should work in groups of 2 or 3 people. You should periodically (every 15 to 20 minutes) switch the person sitting at the keyboard, so everybody gets some experience. The main goal of the lab is not just to finish quickly, but to ensure that everybody in the group is understanding and contributing. Getting a group to work together effectively is an important part of project-based work.

Start by getting on of the boards you soldered together and attaching it via a USB cable to the computer you are working with. We will now try to communicate with the "arduino" computer connected to the board you made.

>> a=arduino

If you are lucky you should get output like that shown below.

a = 
  arduino with properties:
                    Port: 'COM9'
                   Board: 'Uno'
     AvailableAnalogPins: [0, 1, 2, 3, 4, 5]
    AvailableDigitalPins: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
               Libraries: {'I2C', 'SPI', 'Servo'}

If you got something similar, you can skip the next debugging step. If not, read the following.


If you got an error when you tried to initialize

>> a=arduino
Cannot detect Arduino hardware. Make sure original Arduino hardware is
properly plugged in. If using unofficial(clone) hardware, specify port and
board type. For more information, see the arduino function reference page.

then you will have to explicitly tell MATLAB how to connect to the board. Click on the "Start Menu" and select "Control Panel" and then "Device Manager"

When the device manager opens, expand the "Ports (COM & LPT)" item. On the computer shown below, the Arduino is connected to COM30.

To connect to the Arduino, you can type

>> a=arduino('com30','Uno')

and you should get

a = 
  arduino with properties:
                    Port: 'COM30'
                   Board: 'Uno'
     AvailableAnalogPins: [0, 1, 2, 3, 4, 5]
    AvailableDigitalPins: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
               Libraries: {'I2C', 'SPI', 'Servo'}

Lighting lights

Try the following:

>> writeDigitalPin(a,2,1)  % First argument is arduino (a), 2nd is pin (2), and third is value (1)

The LED labeled "L1" should come on because we are writing a 1 (high voltage) to pin 2. If you look at the schematic (below) you see that pin 2 of the arduino (the green rectangular block at the left) has pin 2 (labeled "D2") connected to LED L1. Make sure that everybody in your group understands these connections.

Turn off the LED in a similar way by connecting the LED to a low voltage (0).

>> writeDigitalPin(a,2,0)

Now try turning on and off LED L3.

Now turn on and off the Red, Green and Blue LED's of L7.

Sounding Sounds

Figure out what this does:

>> playTone(a,5,440,1)
>> playTone(a,5,880,2)

Documentation is here: https://www.mathworks.com/help/supportpkg/arduinoio/index.html?s_cid=doc_ftr. Click on the "Read and Write Data" section at the right of the page.

Inputting Inputs

Try the following

>> x=readDigitalPin(a,12)

It should set the value of "x" to 0. Press (and hold) the pushbutton switch and repeat. The value of "x" should change to 1.

Putting it together

Copy the text below into a script and run it a few times:

format compact
for i=1:10

It should generate random numbers between 0 and 1 (but never actually equal to zero or 1). Repeat for

for i=1:10

We multiply the random number by 6 (to get a number between 0 and 6). We take the "floor", which means that we take the next lowest integer (so we always get a number from 0 and 5).

Copy the following code into a file and save it as displayLED.m.

function displayLED(a, myLED )
% This function turns on the desired LED (L1 through L6)

% Turn off all LED's
writeDigitalPin(a, 2, 0);
writeDigitalPin(a, 3, 0);
writeDigitalPin(a, 4, 0);
writeDigitalPin(a, 6, 0);
writeDigitalPin(a, 7, 0);
writeDigitalPin(a, 8, 0);

if (myLED == 1)   % execute the next line if myLED is equal to 1
  writeDigitalPin(a, 2, 1);  % Turn on L1
if (myLED == 2)   % execute the next line if myLED is equal to 2
  writeDigitalPin(a, 3, 1);  % Turn on L2
if (myLED == 3)   % execute the next line if myLED is equal to 3
  writeDigitalPin(a, 4, 1);  % Turn on L2
% xxx  Fill in the rest of the code, and test that it works for all 6.


Now, from the command prompt, try:

>> displayLED(a,2)

LED L2 should light up. Edit the code (fill in at the place marked "xxx") so that it works for LED 1 through 6.

In a new script file, enter the following code

for i=1:10
  disp('Number is'); disp(x); displayLED(a,x);

If you run this code it should simulate 10 rolls of a die (singular of dice).

A "real" die

Now write your own function called "rollDie.m" with two arguments (similar to displayLED). The first argument is the arduino variable (called "a" in the displayLED function, above), and the second is a number from 1 to 6. The board should display a die face showing a number from 1 to 6. Rerun the script above, but replace "displayLED" by "rollDie" to simulate 10 rolls of the die. Save this file to upload to moodle.

1 2 3 4 5 6

A Better displayLED Function.

The code below is a rewrite of the previous displayLED function. Save it in a file called "displayLEDbetter.m" and make sure it behaves the same as the original function. Comment the code to demonstrate that you know how it works. Save this file to upload to moodle. You may need to wait until next weeks lecture before you add the comments - or you can look up unknown syntax in your text (or use MATLAB's help commands).

function displayLEDbetter(a, myLED )
% This function turns on the desired LED (L1 through L6)

LED_pins = [2, 3, 4, 6, 7, 8];  
for i=1:6,
  writeDigitalPin(a, LED_pins(i), 0);

if (myLED >0 ) && (myLED < 7),
   writeDigitalPin(a, LED_pins(myLED), 1);


More tricks

Try the following:

>> readVoltage(a,0)

You should get a number from 0 to 5. Turn the small potentiometer (the black dial on the board) and read the voltage again. It should change. You should get a voltage whose value is proportional to the angle of the potentiometer.

Now try:

>> writePWMVoltage(a,11,2.5)
>> writePWMVoltage(a,11,1)
>> writePWMVoltage(a,11,0.5)
>> writePWMVoltage(a,11,4)

One of the colors of the RGB (Red, Green, Blue) LED's (i.e., the one connected to pin 11) should change in intensity as you change the voltage written to it. You can also change the color of the other RGB LED's to get different color combinations. If you reflect the LED light off a piece of white paper, you can get different colors.

This weeks assignment: "Be creative"

Your last task for this lab is to use at least three of the following functions of the board to pursue a project of your own choosing:

You should discuss as a group and pick a project to which everybody in the group can contribute. You should plan on spending 1-3 hours on it (including the report) during the next week. Write at least one ".m" file (you may also need some helper files). You will turn in a Word document (or pdf) to moodle with a brief (less than one page) description of what your project does, and also include copies of all the necessary ".m" files (each ".m" file should begin on a new page to facilitate grading).

If you come back to the lab later, remember that you may need to reassign the com port (as described at the beginning of the lab). Also, keep in mind that there are a finite number of boards, so if you wait until next Thursday, there may not be a board available.

Remember, documentation is at: https://www.mathworks.com/help/supportpkg/arduinoio/index.html?s_cid=doc_ftr