ARDUINO For engaging with the physical world

Arduino is designed to experiment with interactivity, art and technology in cerative ways. Specifically Arduino is a swish army knife for experimenting with physical computing.

Relevant offical links and ressources

To start

Arduino IDE

  • Serial monitor is where you can observe the code.
  • Upload code allows you to upload code.
  • Verify code allows you to verify without uploading.
  • The status bar at the bottom informs you of a successfull upload.

Serial prompt and plotter

setup() and loop()

Setup and loop are two functions. Setup runs once you start the app and loop.

void setup() {
  //runs once at startup.

void loop() {
  //runs for each frame

Notice how the curly brackets define start and end of each function.

(xx video missing)

The core concepts of programming in Arduino

Most programming languages consists of the same set of elements. Below I try to introduce you to the coding syntax and concepts of them. The core concepts are:

  • Decisions: Making choices on the fly.
  • Scopes: Grouping multiple commands.
  • Memory: A way to store information (variables) dynamically.
  • Repetition: A way to repeat groups of code.
  • Macros: A way to reuse code and structure the logic.
  • Lists: A way to store lists of information.
  • Objects: Grouping information and macros together.

Below I made short videos for each core concept and its syntax.

This google slide presentation on the right presents them in a compressed manner. It is essentially the different concepts and syntax you will typically need to know. You can use it to get an overview of what you need to learn, and also to look up when you are going to write similar code yourself.

Decisions with if statements

Interactive programming is essentially about making decisions. If x happens then do y. The basic syntax making decisions is:

if (2==2) {
   ellipse(200, 200, 70, 70);

If the condition 2 equals 2 is met then the ellipse will be drawn on the screen.

Further, "else" can be used to execute something if the condition is not met:

if (2==3) {
    ellipse(200, 200, 70, 70);
} else {
    rect(200, 200, 70, 70);

In the above case 2 is not equal to 3 so the ellipse will not be drawn, but the rectangle will.

Multiple if statements can be combined with "else if":

  if (2==2 && 2>1) {
    ellipse(200, 200, 70, 70);
  } else if (true) {
    rect(200, 200, 70, 70);
  } else {

Only the first condition which is true will run.

Different formulas can be made with: <, >, ==, != and conditions can be combined with && (and), II (or).

A common mistake is to put semicolon after if statements. This will not result in an error but the scope beneath will always execute. Leading to utter confusion. See the following section about commands and scopes.

Grouping commands with scopes

A command is e.g. when you write print("xx"); to write into the console. A command typically ends with ";" EXCEPT when it is followed by a scope. E.g.:

Serial.println("hello world");
{ // start of scope
  print("Two does equals two");
  print("Second command");
} // end if statement and scope

Notice where the ";" and the curly brackets are placed.

A scope is multiple commands grouped together. Scopes start and ends with curly brackets. They define a group of code that is executed when a certain condition is met. In the previous you learned about if statments. Notice how the scope after the command "if" is only executed when the condition of the if statement is met. And also notice that there is no semicolon at the end of the if statement.

Also, scopes can be nested. A scope can have scope inside itself. This allows conditions to be more complex. E.g. if you are at the supermaket then buy milk if they have a low fat version. This is two scopes nsested inside each other.

{// first scope start
   { // nested scope
      print("buying milk!");

It can be difficult to wrap your head around this concept. I often wonder if this is the most essential part to be able to program.

Common mistakes and how to fix them

When starting out programming you will make a few common mistakes. This lists some of them and how to fix them:

  • Forgetting a semicolon: If you forget to put a semicolon at the end of a line the program will not run. Add the semicolon where missing (usually the line above the one that throws and error).
  • Adding a semicolon after an if statement: There should newer be a semicolon after an if statement. This one is hard to detect because the program will run, but it will always run the code inside the curly braces {}.
  • Too many or too few curly braces. Be very carefull with your curly braces if you add too many or have to few the program will not run. Make sure to indent your code properly so you are able to read it. Everything inside a curly brace should be indented.
  • Remember to relieve the stress on the cables: Most Arduino projects ends up having multiple wires hanging everywhere. If you do not tie them to the board but leave them hanging in the soldering points they will break. Use a ziptie so mount them to the edge of the board or to the inside of a box.
  • Document your setup: If you do not document your setup you will be in a lot of pain when something falls apart and you are trying to figure out where e.g. the blue wire were supposed to be connected and whether the red wire is 5v or ground. Use your phone to take pictures of your wiring on a regular basis

Memory: Storing information with variables

A variables is a container of information that can be changed during runtine.

int x = 200;

In the above Case we have declared a variable with the name "x" and given it a value of 200. We can then change the value by:

x = 300;

To incriment it by one:

x = x+1;

This also incriments by one:


There are different kinds of variable types. They are defined implicitly by adding content to it. E.g. a boolean (true or false) can be defined like this:

var playMusic = true;

Through variables we can execute different parts of the program with if statements:

if(playMusic == true)
// code to play music here

If playMusic is true the code inside the curly brackets will run.

Notice how we use one equal sign (=) when defining variables and two equal signs (==) when comparing a variable to a value.

We typically declare a variable at the top. This way we have global access to them since they are in the global scope. If we place the definition inside e.g. function draw() we will only have access to it inside the scope of the function and it will be reset everytime we run the function.

These are typical variables types:

Repetition with loops

If we want to reapeat a piece of code more than once we can use different kinds of loops to do so. The most common is "for" loops. It is usually used when you have a specific amount of repetition you want to do:

for(int i = 0; i < 10; i = i +1)

The above code will repeat itself ten times. "i" will be the counter that will increment one for each repetition as long as i is less than 10. Since the starting value is zero (a common logic in programming) the loop will repeat ten times until it reaches 9 (not ten; a common confusion).

for(Initialization; condition; incrementation)

A for loop has three parameters seperated with a semicolon:

  • Initialization: Sets the initial value.
  • Condition: Choose when to stop the repetion.
  • Incrementation: Defines how much the variable should incriment.

Another loop used for repetition is a while loop. The following code will loop endlessly:

// this loop will repeat itself forever.

Read more about for loops here and while loops here.

A for loop can be converted to a while loop. This does the same as the for loop mentioned.

Macros with functions

When programming you often have a sequence of commands you want to run at different times during your execution of your code. This can be small macros like converting from inches to mm and it can be more complex elements. These sequences are grouped in function. Let us say that we have an application the often needs to print out a value/variable. Instead of copying Serial.print commands for each situation, we can then make a function and call it multiple times:

function printValue()
  Serial.println("The value is:");

The problem is now that we do not have a way to define the value. We solve this by adding parameters to the function:

function printValue(int value)
  Serial.println("The value is:");

x and y are local variables that we can call like this:


This house will be drawn in position (40,30) and we call it multiple time with different parameters .

Functions can also return something. Let's say that we want to have a function the converts from meters to milimeters:

function mToMM(float length)
  return length*1000;


Calling mToMM(10.2) will return 10200.

Read more about the use of functions here.

Lists' of variables with arrays

Often one wants to have a list of elements. This can be particles in a particles system or a list of employees in a company. For this to work we need to group variables together in a list that we can iterate through. This is called an array. An array is a variable containing a list of variables. Square brackets are used to define it as a list:

var names[3];

This is a empty list with six allocated spaces. We can predifine it with content:

var names = ["Peter","Lisa", "Olga"];

If we want to get the first name:


This prints "Peter".

If we want to print out all the names in our list, we can use a for loop:

for(var i = 0; i < 3; i=i+1)

Notice that the for loop repeats until it has reached the length of the list and the "i" from the for loop is used to retrieve the individual names.

(Arduino pin layout)