Lab 1: Introduction to Mac and Arduino


Topics:

  1. Welcome to the Mac Lab
  2. What is Arduino?
  3. Hardware
  4. Basic Code
  5. References

  6. Exercise

1. Intro to the Mac Environment

1.1 First time login

1.2 Account Storage

Your Mac Lab account comes with 4GB of networked storage on mcserver.cs.uregina.ca. Your homefolder is stored there. Any custom desktop settings you make will follow you from computer to compter in the lab. Any files you save to your personal folders, including the Desktop, will also follow you. Any very large files that you will be working with intensely should not be stored on the network, but should be stored on the local computer (see Local Storage).

1.3 Local Storage

Each machine has several gigabytes of available space on the hard drive. You can store files in the folder called /XDATA. Note that Mac OS X is based on BSD Unix and uses Unix file system conventions.

Any files you store on the local machine will only be available on that machine. If you plan to use them on another machine, copy them to a USB drive or your network account.

File permissions on files stored in /XDATA are too permissive - all files are world readable. See the section on Finder/File Management to learn how to secure your data.

1.4 Desktop Overview

When you first login, your desktop will look like this:

desktop
  1. Dock: used for launching programs and organizing frequently used items.
  2. Desktop: used for storing stuff. NOT generally used for shortcuts to applications.
  3. Mac Menu: Log out, System restart and lock screen are here. You can also find information about your Mac. Has a permanent link to the Preferences panel, just in case you accidentally delete it from the dock.
  4. Application Menu Bar: One application, called the front application, is active for user interaction at a time. This applications main menu is displayed here.
  5. Utilities area: You will find controls for Sophos Antivirus, a clock, display settings and volume here, just like the system tray or notification area on the Windows taskbar.
  6. Spotlight: The coolest tool in the utilities area. Helps you find what you need. Just click it and start typing the name of a file or application and watch the results pile in. Click on the best match.

1.5 Working with Programs

There are several ways to start programs:

1.5.1 Managing Running Programs

A light colored dot under a program's icon in the dock indicates that the program is running.

There's usually only one command menu in Mac OS X. It is at the top of the screen and it is controlled by the active program. Some crossplatform software, such as MATLAB, breaks this rule.

You can switch between active programs by clicking their icon in the dock, clicking a window that belongs to that program, or by using Command-Tab. That key sequence may seem similar to the Windows Alt-Tab that switches windows, but it is different. It switches Applications. If an application has no windows, or they are all minimized you will switch to it and the only indicator will be the menu bar. If an application has multiple windows and they are not all minimized, you will only switch to the last one you used.

Windows have three buttons in the upper left corner. The red one closes the window, the yellow one minimizes the window, and the green one "zooms" the window. The meaning of "zoom" depends on the program. In iTunes it switches between player and music manager modes. In Safari it selects a window width and height that is large enough to optimally display the current page. In Firefox it maximizes the window as a Windows user would expect.

Mac applications have traditionally had multiple mini windows with no application container. If you have many applications running with many windows each, the desktop can get very cluttered looking. You can clean it up by hiding other programs. (insert menu entry, key sequence, dock command here)

1.5.2 Stopping Programs

If you want to stop a running program It is not enought to close its windows. The program will stay in memory waiting for you to use it again. Instead you must quit the program in one of three ways:

  1. Switch to the program, click its name in the menu and select quit
  2. Switch to the program and press Command-Q
  3. Right-click on a program and select quit.

If you would like more information about the Mac Environment, including customizations, click here.


2. What is Arduino?

"Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments". (from http://arduino.cc/en/).

Arduino can use sensors ( for instance, temperature, tilt, and light) to test the environment and can control lights, motors, sound, and more. In later labs, you will get an opportunity to try out these things. In lab one, you need to know the environment that you will be writing the code and a little about the hardware.

The goal of this lab will be to make a light blink. To get there, you need to know about three things:

  1. The environment (the Arduino software)
  2. The hardware (the Arduino Uno)
  3. The code

The remainder of section 2 will discuss the environment. The hardware and the details of the code will be discussed in two subsequent sections.

2.1 Opening Arduino on the Mac

  1. Type arduino into Spotlight
  2. Click the match that appears in the "Applications" section of the list

Spotlight

The Arduino IDE will now start running.

2.2 Overview of Environment

The following diagram demonstrates what the Arduino IDE looks like.

Areas

There are three main areas:

  1. Input Area - this is where you will type or edit code.

  2. Status Bar - this is where you get information about the status of your code. For instance, "Done compiling." or "Done uploading." are two messages that appear in the Status Bar.

  3. Program Notification Area - this is where you get additional details about the status of your code. Pay attention to this area if you notice that compiling or uploading are not working. Usually there will be some reason why.

2.3 Settings

At this point, you should plug your Arduino devices into the USB Port for the Mac.

You might have to adjust some settings of the IDE to get Arduino to work on the Mac. The settings for the Serial Port will be different on windows (likely COM2 or COM3). The following two diagrams illustrate the settings for the Arduino Uno in the Mac lab:

  1. Under the Tools menu, select Serial Port and /dev/tty.usbmodem1d11
    ToolsSerialPort


  2. Under the Tools menu, select Board and Arduino Uno
    ToolsBoardUno

2.4 Your First Blinky Light

And now, for the moment that you have been waiting for, let us see something happening.

Under the File menu, choose: Examples > Basics > Blink. As shown in the diagram below.

 

A new window will open with code in the input area. You now have your first sketch, which is a special name that Arduino uses to mean a program, or a unit of code that will be run on the Arduino board (hardware). We will be looking at that code more later.

BlinkCode

Let us try running the code:

  1. Press the Verify button. Verify
    You will notice a message "Compiling..." and then "Done compiling." on the bottom of the Arduino window.

  2. Press the Upload button.Upload
    You will notice a couple of things:
    1. a message of "Uploading to I/O Board.." and then "Done uploading." on the bottom of the Arduino window.
    2. TX and RX lights will blink when the uploading occurs.

  3. You should now notice a light (labelled "L") blinking on your board. Congratulations, you have gotten your first sketch running on the Arduino board!

The below diagram (modified from http://arduino.cc/en/Main/ArduinoBoardUno) illustrates where to look for the lights mentioned above.

arduinoboarduno

A little more about these lights:

  1. The TX and RX lights flash when data is being transmitted to or from the computer (using the USB connection).

  2. L is a special built-in light that is connected to pin 13. When the pin is a HIGH value, the light is on, when the pin is LOW, it's off. This might not mean anything right now, but you will notice that L blinks on and off after you have uploaded the code.

So, you have a light blinking, but it isn't that spectacular. The next section will give you the details of how to add your own light and make it blink.


3. Hardware

As mentioned earlier, Arduino consists of two major parts: the hardware (the Arduino board) and the Software (the IDE). We already focused on the IDE, now let us consider the hardware:

Not too long ago, working on hardware meant building circuits from scratch, using hundreds of different components with strange names like resistor, capacitor, inductor, transistor, and so on

Every circuit was "wired" to do one specific application, and making changes required you to cut wires, solder connections, and more.

With the appearance of digital technologies and microprocessors, these functions, which were once implemented with wires, were replaced by sofware programs.

Software is easier to modify than hardware. With a few keypresses, you can radically change the logic of a device and try two or three versions in the same amount of time that it would take you to solder a couple of resistors.

(page 19 of Getting Started with Arduino by Massimo Banzi)

So, we don't have to solder things together. We will temporarily build our circuits on a breadboards and change the code to modify how the device responds.

3.1 Breadboard

The key to building tempory circuits is a solderless breadboard. The following diagram (modified from page 94 of Getting Started with Arduino) depicts what the breadboard looks like inside.

breadboard

The thing to note is that there are strips of metal connecting the rows and columns together. Each hole has a spring loaded contact underneath so that when you poke a wire into the hole, a clip grabs onto it.

There are two kinds of metal strips:

  1. rails- (shown in red on the diagram above) connecting the 25 column holes. Notice that on your own breadboard, the rails are between the solid pair of red and blue lines. Typically the + column will be connected to the power and the - column will be connected to the ground.

  2. bars- (shown in blue on the diagram above) connecting 5 holes at a time in the direction of the letters. Notice that there is a gap in the middle.

3.2 The Arduino Board

Let us spend a few minutes looking at the board (modified version of the picture taken from http://arduino.cc/en/Main/ArduinoBoardUno)

Arduino Uno

Throughout this lab you will connect wires to many of the pins encircled in pale red:

3.3 A Basic Schematic Diagram

Through your class, you have been discussing a basic circuit that has the following components (encircled in pale red):

voltage_LED

If we want to use the Arduino to control the light, we can modify the above schematic diagram to the following:

arduino_LED_schematic

Notice that we have connected our power source to pin 13 so that the LED will turn on and off. GND represents ground and is not actually in that place on the board (but it makes a nice diagram). More realistically, our board will look like this:

arduino_LED

Or, if we want to get rid of the black wire on the breadboard, we can turn the resistor 90o and get a layout that looks like this:

Arduino_led_nowire

As discussed in class, we can put the resistor on the other side of the light and we will still get the same results.

Now, it's your turn to build this circuit.

Something to watch for:

3.4 Another Blinky Light

Under the File menu, choose: Examples > Basics > Blink.

Remember, the code looked like this:

BlinkCode

Let us try running the code:

  1. Press the Verify button. Verify

  2. Press the Upload button.Upload

  3. Your light should now start blinking. If it doesn't, try reversing the light. Remember that the longer lead of an LED connects to positive.


4. Basic Code

A sketch is the fancy name that Arduino uses to mean a program or code. It is written in text--just like a document. For those of you who would like to know, sketches are C/C++ based. When you sucessfully Compile/Verify the program, it is translated to Arduino machine-language, which is not human readable. So, let us look at the program below in detail:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

You may notice that many of the lines have a semi-colon (;). Semi-colons are the way of indicating the end of a statement; similar to how we use a period (.) to end our statements. The semi-colon is important--if you forget one, then your code will not Compile/Verify.

4.1 Comments

Comments are like reminder notes of what is going on in the code. Comments are ignored by Arduino, but are a highly recommended addition to your code. Comments remind you what you have done and make it easier for someone else to know what your code is doing.

There are two forms of comments demonstrated in the code above:

  1. /* */ - Anything that is between the opening slash-star (/*) and the closing star-slash (*/) is ignored by Arduino. These are good for comments that go over several lines.

  2. // - Anything after the double-slash (//) is ignored by Arduino. These are good for short little reminders at the end of a line.

4.2 Variables

We can change the above code to have a variable called ledPin. Notice how instead of using 13 everywhere, you now use ledPin. Why would we want to do that?

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
int ledPin=13;
void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(ledPin, OUTPUT);     
}

void loop() {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

You will notice that:

int ledPin=13

is a declaration to say "that we would like to create a box named ledPin and put the number 13 in that box." (http://www.ladyada.net/learn/arduino/lesson2.html). Notice that int indicates that the type of box is an integer. After the declaration, you use ledPin where you had 13 before.

Try: Adding the variable, then change the 13 to a 9. What else do you have to change in order to get your light to blink?

4.3 Procedures

We will be looking at procedures in detail in a later lab. For now, you can interpret procedures as a collection of statements that are grouped by a task. To mark the beginning and ending of the grouping, an opening curly bracket ( { ) and closing curly bracket ( } ) are used respectively. Arduino expects two procedures in every program:

  1. setup()- any statements in this procedure will be executed once at the beginning of your program (when it is uploaded or when the Arduino wakes up after being reset).

  2. loop()- any statements in this procedure will be executed over and over again forever, or at least until you turn your Arduino off or upload something new. The statements in this procedure are the core of what you want your Arduino program to do.

We will look at the statements within these two procedures in detail in the next two subsections.

4.3.1 Inside setup()

Remember, the statements within setup are executed once at the beginning of your program. In our example code, there is only one statement within this procedure:

pinMode(ledPin, OUTPUT);

This statement tells Arduino to "setup" the ledPin (originally pin 13) as an OUTPUT pin. OUTPUT means that a voltage will be sent out from the pin, thus controlling our LED. The other option (not used in this lab) is INPUT, which means that a voltage will be read from the pin.

4.3.2 Inside loop()

Remember, the statements within loop are executed over and over again forever. The guts of what we want our program to do are inside this procedure. The two major statements are :

  1. digitalWrite(ledPin, HIGH)
  2. delay(1000)

5. References

 


6. Exercise

Well, you now know how to make a light blink.

What can we do with a blinky light? How about making a message with Morse Code?

You have the choice of making SOS or the initials of your name.

Details:

Morse code is made of a combination of 5 elements:

Dots (or Dits) 1 unit of time
Dashes (or Dahs) 3 units of time
Pause between a Dot or a Dash 1 unit of time
Pause between Letters 3 units of time
Pause between Words 7 units of time

Let's say that 1 unit of time is 240 milliseconds. But it could change so make this a variable (called uTime). At the top of the code, you will have:

int uTime=240;

Then when you are using delay, you can write:

delay (uTime);

or

delay(3*uTime); //if you want 3 units of time

To make S0S, the S is three dots and the 0 is three dashes. To break this down by timing:

At the beginning, I said that you would have the choice of making your own initials in Morse code. For your reference, to look up your initials, you can find a Morse code chart here:

http://en.wikipedia.org/wiki/File:International_Morse_Code.svg

Deliverables (to show your lab instructor):