training-labs

Gentle Introduction to Arduino Universe

This introduction assumes some programming experience in MATLAB or Python. By engaging with this introduction we hope that you will learn:

Video Segments

Video segments cover the same material as the text. You may read the text, watch the video, or do both.

TinkerCad

We use TinkerCad’s Arduino simulator to demonstrate some Arduino concepts. Accounts are no-cost, and you may register for a standard account with Cornell GSuite authentication. After logging in, you should arrive at your dashboard. You can create a new Arduino sketch by navigating to Circuits, creating a new circuit, and dragging an Arduino Uno onto the circuit area. You can find an Arduino Uno in the scrollable components sidebar. This video demonstrates the process as well.

Running Your Simulation

Click on “Start Simulation” to start the simulation. If your code compiles successfully, the button will turn green and become a “Stop Simulation” button.

What does the default program do? In setup(), the program sets LED_BUILTIN to be an output using pinMode. You could pass another pin number (i.e. 5) to set that pin to be an output. If a pin is an output, you can use digitalWrite to write one of two voltages to that pin. A HIGH or nonzero value makes the pin carry the supply voltage. On our Feather M0+ boards, that is 3.3V relative to ground. On the Arduino Uno, that is 5V. A LOW or zero value makes the written pin carry zero volts relative to ground.

After setting the builtin LED to be an output, the program writes a 1 or a 0 to that pin once per second, blinking the LED slowly. The blinking continues infinitely, in theory.

The video below demonstrates starting and stopping the simulator and explains the default program.

Introduction to C++ Functions

A function in C++ has a few basic parts. Please follow along in the dissection of the function below.

void setup(){

}

That function has a return type of void. void means that the function does not return anything. Other functions may return int, float, or any other C++ data type, including custom data types. int is an integer, like 3. float is a number with a decimal point, like 3.14. We will talk more about data types later.

That function’s name is setup. You call a function by its name. Arduino calls setup and loop automatically. So, you need functions called setup and loop in every sketch!

That function takes no parameters, signaled by the empty (). You can pass data to a function by using parameters. Here is a different function:

int addOne(int theNumber){

}

Check for understanding: What kind of data does addOne take?

Answer addOne takes an int and calls it theNumber in the function body. You can pass multiple parameters in a comma separated list. We could pass an int and a float by making the parameters be int theNumber, float secondNumber. We will soon explain what a function body is, but here is one more question.

What kind of data does addOne return?

Answer addOne returns an int.

The function body is between the {}. Functions usually have nonempty bodies, but empty function bodies are allowed. Let us give addOne a nonempty body.

int addOne(int theNumber){
  return theNumber + 1;
}

Use return to return a value. Remember that the value that you return must match the return type. In this case, we are fine. theNumber is an int and 1 is also an int. Their sum is an int as well, so we can return it. Statements end with ; in C++.

You can call a function by using its name and passing it arguments that it expects. For example, we call our addOne function in setup like this:

void setup(){
  int numberTwo = addOne(1);//1->int constant
  addOne(3);//return value ignored
}
int addOne(int theNumber){
  return theNumber + 1;
}

You can also simply discard a return value if you do not want it. In a normal C++ compiler, this code will return an error. Why? We used addOne before a declaration of its prototype int addOne(int theNumber) appeared in the function. The compiler has no idea that addOne exists when it processes setup.

We can fix this by declaring our function.

int addOne(int theNumber);//declare existence
void setup(){
  int numberTwo = addOne(1);//1->int constant
  addOne(3);//return value ignored
}
int addOne(int theNumber){
  return theNumber + 1;
}

To check your understanding, please define your own function that

The video below covers the introduction to functions.

Setup and Loop

Now that we know a bit about C++ functions, let us revisit the setup and loop functions. Because of the Arduino structure, every sketch must have a setup function that returns nothing and accepts no parameters. Every sketch must also have a loop function that returns nothing and accepts no parameters.

Arduino calls setup once at the beginning of your program. That makes it a good place to do things like setting the mode of output pins.

Arduino then repeatedly calls loop infinitely many times. This assumes that your loop function actually returns. That is why the LED in the simulation blinks infinitely many times.

To check for understanding: Where would you put a call to a function that attempts to initialize a light sensor?

You would probably call that function in setup.

Where would you put a call to a function that uses the light sensor to measure the light level periodically?

You would probably call that function in loop.

Serial

What are some other things that you can do in your setup function? You can set up the Serial Monitor. In the bottom left of the code pane, you can click on Serial Monitor to open the Serial Monitor. You can send text to the Arduino, but more immediately useful is its ability to display text sent from the Arduino.

To set up the Serial Monitor, you need to call

Serial.begin(115200); //look up Arduino baud rates for more information

to start the serial hardware. Serial is an object (discussed later) that represents the USB hardware serial connection. Beginning serial allows us to print stuff.

void setup(){
  Serial.begin(115200);
  Serial.print(34);
  Serial.print(56);
}
void loop(){

}

that program will print

3456

every time the Arduino starts, without a new line. So, every time you hit “Start Simulation” or the red reset button, the Arduino will send more 3456s, like below.

345634563456345634563456

We can also print text, and we can also print new lines. The following program demonstrates this.

void setup(){
  Serial.begin(115200);
  Serial.print(34);
  Serial.print(56);
  Serial.println("Hello World");
}
void loop(){

}

The output after pressing the reset button two additional times is:

3456Hello World
3456Hello World
3456Hello World

You may also print in the loop function. Remember that loop runs repeatedly.

Please print some numbers and strings of your own in TinkerCad to practice using Serial.

Declare Functions Before Use

Arduino (in the sketch) and Tinkercad do allow you to define functions after their first use. However, in .h and .cpp files, Arduino’s build system acts like a standard C++ compiler, producing an error. Please watch the video for a demonstration that is hard to describe in text. Following the demonstration in the Arduino desktop IDE may also help you to experiment when you can and cannot call functions.