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)
{
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
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.
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;
void setup(void) {
void loop(void) {
if ( led == LOW ) {
led = HIGH;
} else {
led = LOW;
digitalWrite(13, led);
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.
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
//
business?button = digitalRead(2);
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).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.