Your First Arduino Program

The Arduino programming language is called Processing, and it is a simplified form of the programming language called “C++” (See-Plus-Plus, the name is an in-joke because it is the successor to a popular language called “C”, and in the C language the ++ operation means “add one”).

The Arduino has 14 Digital Input-Output Pins (also sometimes called GPIO - General Purpose Input Output) numbered D0–D13, plus six Analog pins A0–A5. The digital pins can each output (or sense) a 1 or 0. The Analog pins (in addition to digital IO) can also sense an “Analog Voltage”, that is any voltage between Zero and 5 Volts, which they map to the numbers 0..1023. For example a value of three volts would be read as approximately 614.

The best way to learn is by reading existing programs and modifying them, so let’s start with a very simple program. Most Arduinos have a built in LED on Pin 13, so we will make it turn on.

A program in Processing consists of a number of procedures (or functions). Each procedure can take optional inputs, and can return one value (or none). We declare a procedure by giving the type of return, the name of the procedure, and the type of input (called “arguments”). We use the special keyword void to mean “no inputs” (or no output).

We have a number of built-in procedures to do things for us, and we build our program by combining our own procedures with the built-in procedures to instruct the computer to perform our intended behaviour.

There are two special procedures in processing, called “setup” and “loop”. Setup is performed once at power-on, and then loop is performed (“called”) over and over as long as power is on.

We use the setup procedure to do any configuration of our pins and modules, then we do the rest of our job in the loop procedure.

Here is a simple program to turn on a LED and then do nothing.

void setup(void) 
{
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
}

Let’s look at this one line at a time

void setup(void)
This line declares a procedure named setup. It returns nothing, and takes no inputs.
{
This line starts a block. Anywhere you can put one statement in Processing you can also put a block of multiple statements enclosed in curly braces
pinMode(13, OUTPUT);
This is an invocation (or “call”) of a built-in procedure called pinMode. It is used to configure a pin as an input or output. The word OUTPUT is a symbolic constant defined by the Processing environment (it happens to stand for the number 1 here). Note the semi-colon, every statement in Processing needs to end with a semi-colon.
digitalWrite(13, HIGH);
Another procedure call, this makes the voltage of Pin 13 HIGH (5V). HIGH is a symbolic constant that also happens to be 1.
}
This brace ends the block that is the body of the setup procedure.

This program isn’t really very useful, since we could turn a LED on simply by connecting it to power and dispense with the Arduino entirely!

Let’s make the LED blink instead.

Downloading your program

Your second program

This program makes the LED blink on once per second

int led = LOW;

void setup(void) {
  pinMode(13, OUTPUT);
}

void loop(void) {
  if ( led == LOW ) {
    led = HIGH;
  } else {
    led = LOW;
  }
  digitalWrite(13, led);
  delay(500);
}

This looks a little more complex.

Let’s break it down

int led = LOW;
We are creating a variable called ‘led’ that is an integer variable (we can also use char for letters (characters), float for decimal numbers, and bool for true/false Boolean algebra). This declaration sets aside some of the Arduino’s memory and lets us refer to it by the name ‘led’. Wherever we write led, the system will consult this piece of memory to see what information we placed there. Here we are initially placing the value of the constant LOW into the variable (LOW happens to mean 0).
void setup(void) {
Our setup function is largely the same as before. Here you see the brace moved up to same line as the procedure declaration. Vast and bloody wars have been fought over whether the brace should be on this line or on a line by itself.
void loop(void) {
This is our loop procedure. It runs over and over again until bedtime.
if ( led == LOW ) {
A program that always did the same thing would be dull. Here, our program is going to do something different depending on the value of LED. We use two equals signs to mean “test if these two values are equal”. Using just one won’t work here (that means something different). An if statement executes the next line (or block) if the condition is TRUE.
led = HIGH;
If the value of led was low, we are going to change it to high. A single equal sign means “take the value on the right-hand-side, and store it in the variable on the left-hand-side. Note that we are only changing a piece of memory, this won’t affect the pin (yet). Did you see the indenting on this line? We indent each block to help us follow the structure. It isn’t mandatory, but indentation is very important to help you understand the program when you come back to it later.
} else {
We close the “then” block, and add an “else” block. This block will happen if the condition in the if statement is False.
led = LOW;
If the conditon was false (led was not LOW, i.e. HIGH) then we make it LOW. So each time through the loop our if…else statement will invert the value of ‘led’.
digitalWrite(13, led);
Now we use the digitalWrite procedure to set the state of Pin 13 to whatever number we stored in ‘led’. So as we execute the loop procedure over and over we will turn the led ON then OFF then ON etc.
delay(500);

Computers are fast. If we did not include this line, the loop procedure would run thousands of times each second, the LED would turn on and off so fast that we wouldn’t be able to see it change, and would just look sort of half-dim. We add a 500-millisecond delay here using the built procedure ‘delay’. This will make our program turn the led off, wait 500ms, then on, wait 500ms, giving us a one-second-cycle blink.

Program three - Inputs

Let’s make a program that reads a button and uses it to turn the LED on. To do this we place a button between a digital input (we’ll use input 2) and ground.

If the button is pushed, the input is connected to ground (which reads as zero). But what if it’s not pushed. The input is connected to … nothing. If we try to read an unconnected pin we will get a random result. Now you can get more complicated switches that connect to power and ground, or we can cheat.

The arduino has optional “Internal pull-up resistors” which we can activate. This makes an input “want” to be high, unless it is connected to a “stronger” input, such as a direct connection to ground.

int button;

void setup(void) {
  pinMode(13, OUTPUT);
  pinMode(2, INPUT_PULLUP); // use INPUT if you don't want a pull-up
}

void loop(void) {
  button = digitalRead(2);
  digitalWrite(13, button);
}

I won’t go line by line, but there are a couple of new things here

pinMode(2, INPUT_PULLUP); // use INPUT if you don't want a pull-up
We make the pin an INPUT_PULLUP (there are only three options, OUTPUT, INPUT or INPUT_PULLUP). But what’s that // business?
That’s a comment. It’s completely meaningless, the compiler ignores it. We use comments to leave ourselves (or other programmers) reminder notes. If you leave notes as to what you were thinking when you write your programs, you will thank yourself when you come back to them tomorrow, or next week, or in six months.
button = digitalRead(2);
Here we are using the digitalRead procedure, which returns a value. A procedure that returns a value (i.e. is not void) is also called a function. The digitalRead function returns LOW (0) or HIGH (1).

The Arduino example collection

Arduino is intended for use by novice programmers. It comes with a lot of example programs to help you learn.

If you want to do something new with Arduino, the first place you should look is in the Examples section of the File menu. Open up an example and look at it. Read the comments to see what it does. Try compiling it.