Home‎ > ‎CS 11M‎ > ‎

Getting Started with Arduino

Getting Started with Arduino

Your Arduino Kit

Recommended Arduino Kit
  • You should order your kit no later than today so you're ready next week.
  • If you do not have a kit already, partner with someone who does

Arduino Kit Contents

The required kit includes components like these:

  • Arduino Uno
  • Solderless breadboard
  • USB cable
  • Motors and servo
  • Piezo Buzzer
  • 74HC595 Shift Register IC
  • Assorted LEDs and resistors
  • Sensors for light, heat, pressure
  • Relay, potentiometer, push button, transistors, diode, wires

Assembling the Kit

  • The kit comes with an acrylic holder
  • Attach the Arduino to the indicated side with the screw and nuts provided
  • Attach the Breadboard to the other side by removing the backing from the adhesive and pressing into place
  • Make sure the text on both the Arduino and breadboard faces the same way

More Information

Powering Up the Arduino

  • An Arduino board usually comes with a preinstalled blink program
  • All we need to do is supply power
  • Using the cable, plug the Arduino into the USB port on a computer
  • Look for the blinking LED, which is marked: L

Applying Power to the Arduino

Connecting the Arduino to power
Source: www.arduinoclassroom.com

Activity

  1. Plug in your Arduino board to your computer using the USB cable.
  2. Verify the "L" LED is blinking.
  3. If you have problems, ask a classmate or the instructor for help.
When finished, please help those around you.

Installing the Software

  • To control the Arduino, we must upload sketches
  • Sketch is the Arduino word for program
  • The Arduino site has software installation instructions for:
  • The software is known as the Arduino IDE (Integrated Development Environment)
  • Note that the Arduino IDE is installed on school computers in the classroom and the STEM center

Exercise: Installing the Arduino IDE

In this exercise we install the Arduino IDE (Integrated Development Environment) on a computer.

Specifications

  1. Follow the software installation instructions for your computer:
  2. When finished uploading the Blink sketch, you should see the pin 13 (L) LED on the board start to blink
  3. If you have problems, ask a classmate or the instructor for help.

When finished, please help those around you.

Verifying the Installation

  • At this point you should have:
    1. Downloaded and installed the software
    2. Opened the blink example
    3. Selected a board type and port
    4. Compiled and uploaded the Blink program
  • Has anyone not completed these steps?
  • Otherwise, you now know how to get set up and ready to go with Arduino!

Using Mike's GitHub

My course material is on GitHub. You can find it here:


GitHub is a community of programmers. Git is a form of source control management (SCM) sometimes also called version control. You'll use the following code projects in this class:
  • CS-11M - The repository (called "repo") has code that you'll need. 
    • It has a template sketch with comments 
    • It has code to test your Arduino circuit for the class
  • ArduinoSTL - The repository that enables many C++ functions in Arduino
    • You can use Arduino's library manager to install the library. 
You might also be interested in the ArduinoGrader. It's the program I use to grade your assignments. 

Exercise: Download and Run the Template

  1. Go to the CS-11M repository on my GitHub
  2. Download the code as a ZIP file 
  3. Extract the ZIP file to the desktop 
  4. Open the Template sketch in Arduino

Exploring the Template Sketch

Sketches and Comments

  • Let us walk through the Blink sketch
  • Sketches contain C/C++ programming language code
  • By default, code executes sequentially, top to bottom, left to right
  • To read program code, start at the top and follow the code downward, like a book

Coding Comments

  • A useful tool in any programming language is the ability to leave notes to yourself and other humans
  • We use comments to document blocks of code and to describe unusual code
  • Arduino has two main styles of comments:
    1. C style Comments
    2. C++ style Comments
  • Both have their uses, which we now explore

C style Comments

  • The first part of the Blink program is a C-style "block" comment (see below)
  • Comments are notes to humans, like you and me
  • C-style comments start with /* and end with */
    /* this is a comment */

    Everything between the /*...*/ is ignored by the compiler
  • All programs should have this C-style comment block -- Why?

C-Style Block Comment Example

/*
* This is the CS-11M program template. Change this text to
* be the title of the assignment and a short description
* of what it does.
*
* Author: Michael Matera
* Date: 8/16/2016
*
*/

C++ Style Comments

  • C++ has a second type of comment
  • C++comment starts with // and lasts to end of the line

    // this is a comment

    How many C++ style comments can you find in the following code? What are they used for?

Example Code with C++ Style Comments

// the setup function runs once when you press reset or power the board void setup() { pinMode(led, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }

Check Yourself

  1. True or false: comments are notes to the compiler.
  2. Which of the following indicates that a single line of commentary will follow it?
    1. /*
    2. /**
    3. //
    4. */
  3. Which of the following should be used at the top of each program file to describe the program?
    1. /*..*/
    2. //...//
    3. //...
    4. #...

Arduino Commands

  • The following is the Blink code with comments removed
  • This remaining code is executed, after compiling, by the computer

Blink Code Without Comments

void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }

Programs

  • Programs are a list of commands like those shown above
  • Commands are carried out in the order they are written (by default)
  • Commands are listed between the curly braces {...}
  • These groups of commands are known as a function
  • A function is a group of statements with a name that can be called in program
  • In C/C++, the statements are grouped into a block between the curly braces {...}

Examining the Commands

  • Here are some of the commands, one per line:
    digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000);
  • Each line does something
  • First line sets the output of pin 13 to HIGH, which turns on power to the LED
  • Second line waits 1000 milliseconds (1 second)
  • What does the 3rd line do? 4th line?
  • Notice the way words are written and the punctuation
  • The arrangement of words and punctuation is known as syntax
  • Notice that most Arduino C/C++ commands end in a semicolon ";"
  • A semicolon ";" is like the a period in a sentence of English, marking the end of the phrase

Arduino Names

  • Recall our snippet of code from the Blink program:
    digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000);
  • Program languages are picky about syntax details like names
  • Names must be written as a single word (no spaces)
  • digitalWrite is the name of a function that does something -- sets the output of a pin
  • Names are case-sensitive: digitalWrite, not DigitalWrite or Digitalwrite

Check Yourself

  1. Which of the following is a command?
    1. /** good choice! */
    2. // another great choice!
    3. digitalWrite(13, HIGH);
    4. digitalWrite(13, LOW)
  2. The purpose of the following command is to ________.
    delay(1000);
    
  3. True or false: names in C/C++ are case sensitive.

Arduino Functions

  • Recall our snippet of code from the Blink program:
    digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000);
  • Because digitalWrite is a function it has parenthesis
  • Thus we usually write a function name followed by a set of parenthesis: digitalWrite()
  • Function digitalWrite() must know which pin to set and whether to set it HIGH or LOW
  • These two pieces of information are called arguments
  • Arguments are said to be passed to a function
  • Writing a function name with its arguments is known as calling (aka invoking) a function

Required Function Definitions

  • Arduino commands are organized into two groups
    void setup() {...} void loop() {...}
  • These two groups are known as function definitions
  • Every sketch must have these two function definitions

Functions setup() and loop()

  • Notice the word void and the curly braces in the required functions:
    void setup() {...} void loop() {...}
  • These two parts of the syntax means we are writing function definitions for Arduino to call
  • Function setup() runs once when the sketch starts
  • Function loop() runs forever
  • We place our commands inside the {...} of these function definitions
  • We will discuss how to write other function definitions later

Check Yourself

  1. Of the following, the valid C/C++ names are ________.
    1. foo
    2. foo bar
    3. bar none
    4. fooBar
  2. Information passed to a function is known as a(n) ________.
  3. The following function call has how many arguments?foo(0, 1, 2);

    0
  4. 1
  5. 2
  6. 3
  7. The two required function definitions in Arduino are ________ and ________.

Using the Reference

  • Arduino has many built-in functions
  • How do we know which ones to call?
  • We use the reference

    Help > Reference

Finding Terms in the Reference

Arduino software allows us to highlight a word and find it automatically in the reference.

Activity (5m)

  1. In the Blinky program, highlight the term pinMode.
  2. Open the Help menu and click on Find Reference.

    The Arduino Reference opens in a browser on the entry for the term.

  3. Take a few minutes to look up a few terms.
  4. If you have problems, ask a classmate or the instructor for help.

When finished, please help those around you.

Exercise: Changing the Delay 

In this exercise we change and recompile code.

Specifications

  1. Start the Arduino IDE.
  2. Open the Blink sketch.

    File > Examples > Basics > Blink

  3. Save the sketch as blink2 to a convenient place like the Desktop or your Arduino folder.

    This will create a file named blink2.ino in a folder named blink2.

  4. Experiment with the delays and change them to something besides 1000 milliseconds.
  5. Compile and upload your code after changing the delays. Verify button Upload button

    If you have problems, ask a classmate or the instructor for help.

  6. When finished, please help those around you.

Summary

  • Sketches contain C/C++ programming language code
  • By default, code executes sequentially, top to bottom, left to right
  • We can add comments to our code to leave notes to humans reading the code
  • We add comments to document blocks of code and to describe unusual code
  • There are two main types of comments:
    • C style: /* this is a comment */
    • C++ style: // this is a comment
  • Programs are a list of commands like those shown above
  • Commands are carried out in the order they are written (by default)
  • Program languages are picky about syntax details like names
  • Names must be written as a single word (no spaces)
  • Arduino has several functions that we can call (aka invoke)
  • We look up which commands to call and what they do in the reference
  • In addition to built-in functions to call, we must write two functions for every sketch
    void setup() {...}
    void loop() {...}
    
  • We place our commands inside the {...} of these function definition

Compiling and Debugging Sketches

Compiling Sketches

  • When we write a program, we start with source code
  • Source code is a text listing of C/C++ commands in human readable form
  • When we press the Verify button we start the compile process
    Verify button
  • Compiling means that we translate source code into machine code
  • Source code is the human understandable text inside the sketch
  • Machine code is what the computer understands
  • When compiling is successful, we will see a message at the bottom of the source code window like:
    Done compiling
  • We can see the flow of the compiling process in the following image
  • The executable program at the end can be uploaded to Arduino's flash memory

Compilation Process

Compilation process

Running a Compiled Program

  • To run or execute a program we press the Upload button 
    Upload button
  • The compiled code is transferred to the Arduino's flash memory
  • After the upload process completes, the Arduino boards starts executing the code

Check Yourself

  1. Another word for "verifying" on the Arduino is ________.
  2. Code that humans type is known as ________ code.
  3. Code that computers understand is known as ________ code.

Syntax Errors

  • As we develop programs, we make errors
  • Often the error is because we left out a character or misspelled a word
  • In programming, one small mistake means the program does not work

Detecting Syntax Errors

  • The Arduino IDE reports syntax errors found during compiling
  • Syntax errors are caused by incorrectly placed words or punctuation
  • As an example, we can try compiling a blank sketch
  • When we do we get a message: Error compiling
  • We get more information about the error by pressing Copy error messages and pasting the error text into a text editor

Compiler Error

Example compiler error message

Code with a Syntax Error

  • The following code has a syntax error
  • Can you spot the error?
void setup() { pinMode(13, OUTPUT) } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); }

Another Syntax Error

  • A common syntax error is a missing semicolon ';'
  • Removing the first ';' in the above sketch produces the error:
    blink.ino: In function 'void setup()': blink:3: error: expected ';' before '}' token expected ';' before '}' token

  • The error message shows many key details, like:
    • blink:3
  • The 3 is the line number of the error
  • Line numbers are very useful for locating errors
  • Look at number of line reported to find error on or before that line
Debugging Syntax Errors

First computer bug
  • Errors in programs are known euphemistically as "bugs"
  • The process of removing errors is known as debugging
  • First step is to read the error message and find the line number reported
  • The error is usually on the reported line or preceding lines
  • After fixing the first problem -- recompile
  • Other reported errors are often caused by the first error

Check Yourself

  1. To know if our sketch is OK we ________.
  2. Errors in the syntax of code is known as ________ errors.
  3. True or false: line numbers are useful for finding errors.

Exercise: Errors and Debugging

In this exercise we compile, fix errors and add print commands to code.

Specifications

  1. Start the Arduino IDE.
  2. Copy the following code into the Arduino IDE and save the sketch as buggy.
  3. Find and correct the code errors.

    If you have problems, ask a classmate or the instructor for help.

  4. Add two print commands to display the value "On" or "Off" after every LED change and before the delay command.

    If you have problems, ask a classmate or the instructor for help.

  5. When finished, please help those around you.

Buggy Code

void setup() { pinMode(13, OUTPUT); Serial.beggin(9600); } void loop() { digitalWrite(13, HIGH) Delay(1000); digitalwrite(13, LOW); delay(1000); }

Summary

  • When we write a program, we start with source code
  • Source code is a text listing of C/C++ commands in human readable form
  • When we press the Verify button we start the compile process
  • Compiling means that we translate source code into machine code
  • Source code is the human understandable text inside the sketch
  • Machine code is what the computer understands
  • We upload the machine code to the Arduino
  • As we develop programs, we make errors
  • The Arduino IDE reports syntax errors found during compiling
  • Often the error is because we left out a character or misspelled a word
  • We find the error by first looking for the line number in the error message
  • The error is on or before the reported line
  • After fixing the first problem we recompile the source code
  • Another tool for finding errors is the serial monitor
  • With the serial monitor we can print information about our program as it runs

Wrap Up and Reminders

  • When class is over, please shut down your computer.
  • Complete unfinished exercises from today before the next class meeting
Subpages (1): Project 0: Get Ready
Comments