Lesson 1: Getting started with the Arduino

Step 1: Download the Software

Before we can do anything with the Arduino, we are going to need the software that can write our programs to the Arduino. The main page is located here: http://arduino.cc/en/Main/Software

Download the one for your operating system and install everything provided.

After all that is done, you should be able to run the Arduino IDE. It should look something like this:

A blank Arduino IDE tab

Step 2: Creating our first program

Programs for the Arduino are written in C. Below is the code for our first Arduino program. It will simply blink an LED over and over:

/* Blink Program - Simply blinks an LED */

// Globals
int ledPin = 13; // Our first project will use pin 13

// The setup() function gets called once when the program starts.
void setup() {
  // Set the pin to be digital output.
  pinMode(ledPin, OUTPUT);

// The loop() function gets called over and over until the Arduino is unplugged.
void loop() {
  digitalWrite(ledPin, HIGH); // Turns the LED On
  delay(1000); // Sleeps for 1 second (1000 milliseconds)
  digitalWrite(ledPin, LOW); // Turns the LED Off
  delay(1000); // Sleeps for 1 second (1000 milliseconds)

You will notice that the code is basically divided into 3 sections: the globals, the setup function, and the loop function. Most Arduino projects will contain these three elements.

The globals section holds variables that the setup and loop function need to share. In this instance, both functions need to know which pin is going to be toggled. In general, global variables are frowned upon because of their tendency to be over used. A good question to ask yourself before creating a global variable is “Will this variable be used across multiple functions, or is it limited to just one?”.

The setup function is where you put the Arduino in the state the loop function expects. It is only run once. After it finishes, the loop function is run. In the example code, we set the pin to be digital output.

The loop function is where the good stuff happens. This is what is run over and over. The bulk of the code you will write will go into this function.

Copy and paste this code into the Arduino program, and click button that looks like the “play” button. This will compile your program for the Arduino. Compiling the process of converting the code we write into instructions the Arduino understands.

If it compiles successfully, you will see a message in the bottom screen that says “Binary sketch size: 1052 bytes (of a 30720 byte maximum)”. This means you are ready to write the program to the Ardunio.

Click the “Upload” button (an arrow pointing to the right). The TX/RX LEDs should blink, then your LED should start blinking.

Congradulations! You’ve just written your first Arduino program.

An aside about digital pins:

A digital pin can have one of two directions: input or output. When a pin is set for input, you can use digitalRead to read “1” (if the voltage on the pin is over some voltage threshold) or “0” (if the voltage is below the threshold). In output mode, the pin will supply a voltage when set to HIGH, and bring the pin to 0 volts when set LOW.

Arduino Functions under your control:

The Arduino provides a few functions to make your life easier. Here are the ones we’ve seen so far:

  • pinMode( pinNumber, pinMode) – This function sets up a pin to be either INPUT or OUTPUT. Input pins can only be read, and output pins should (mostly) be written to.
  • digitalWrite( pinNumber, pinState ) – This function will change a pin’s state to be either HIGH or LOW. In the example, HIGH will turn the LED on, and LOW will turn it off.

Computer Science Concepts:

At this point, there are two key computer science concepts to understand:

  • Functions – A function is a set of instructions which can be accessed by its name. The example code contains two functions: setup() and loop().
  • Variables – A variable is a place for you to store data in your program. In the example code, we create the ledPin variable to hold the number of the pin we are using. We can then access this variable by using its name.

Part Three: 2 LEDs

Now that we know how to blink 1 LED, let’s try blinking two LEDs. Here are a few things to try:

  • Blink both LEDs at the same time.
  • Blink one LED then the other.
  • Turn on one LED. Turn on the other LED. Turn off the first LED. Turn off the second.