Arduino is an open source electronics platform that allows us to control hardware easily. An Arduino is a microcontroller. That means it does not have full power like a CPU (the brains behind the computer you are using right now), but it has enough power to control hardware electronics by specifying what wire to output a voltage to and which wire to read voltage from.
A Sketch is an Arduino program that gets compiled and uploaded to the Arduino board. In order to tell the Arduino how to control the electronics that we connect to it, we need to write a program, a Sketch, that defines what we want the Arduino to do.
//this is a comment
//setup function, runs only once when the Arduino starts
void setup() {
}
//loop function, runs over and over again
void loop() {
}
The above is the main structure of every Sketch (Arduino program). The program uses a language called C++
.
The first line of the program is a comment. A comment means that this line will not become part of the program compiled and uploaded to the Arduino board. It is just available for the reader of the programs (humans).
In C++
we can have two types of comments: one line comment and comment block.
The comment needs to start with //
. Anything that comes after the //
will be ignored by the compiler.
A block comment can contain multiple line comments. To start the block we use /*
and to end the block we use *\
. A block comment will be ignored by the compiler of Arduino, but might have useful information for the reader of the program. For example when you submit your solution to your teacher.
/*
Student ID: 0123456
Team Name: Cool Team
This program does blablabla
*/
A function is a way to group multiple lines of code and give them a name. Whenever we use that name, the program will call all the lines of code inside of that function. In the next sections we will be writing our own functions. The setup function in the Sketch program will be called by the Arduino board when we first turn it on ONCE.
This is important to keep in mind. Whatever we want to setup at the very beginning, we will write that code inside the setup function.
//setup function, runs only once when the Arduino starts
void setup() {
}
The second function in a Sketch program is the loop function. This function will keep on being called over and over again. Until you turn off the Arduino board. This is where you will write the code that will control your electronics.
The Uno board is one type of Arduino boards and the most popular one for learning Arduino. The board consists of the main chip that is the 'brains' behind the Arduino Uno and multiple pins that you can connect wires to them. These wires are usually connected to electronics components so that the Uno can control the voltage that goes into these electronics.
There are two kinds of pins, digital and analog. We will not cover the analog pins for now. The digital pins mean they can be either HIGH
or LOW
. Which also means we either output 5V through that pin or 0V. A pin can be either INPUT
or OUTPUT
. If we are outputting voltage from the Arduino board to an electronic component connected to that pin, then our pin is OUTPUT
pin. If the electronic component is producing voltage through that pin, and the Arduino board is reading that voltage then the pin is INPUT
pin. The digital pins can be seen on the top side of the Uno board labeled from 0 to 13.
On the Uno board also there is a built in LED next to the L letter. This LED is connected to pin 13. In the Arduino platform if you use LED_BUILTIN
in your code, it will be translated into 13
as that is the pin for the built in LED.
The following program turns on the built in LED and then waits for 3 seconds. In the setup function, the first thing we do is specify for each pin we are using whether that pin is going to be used as output or input pin. We do that by calling the Arduino function pinMode(pinNumber, mode);
The pinNumber is the number of the pin we want to setup, and the mode is whether it is OUTPUT
or INPUT
. On line 2 of the program, we are specifying that pin 13 is an output pin. Which means we will be deciding if the pin has 5V or 0V. If we output 5V then the LED will turn on.
Line 7 specifies the output to be HIGH
to the LED_BUILTIN
pin. Which is the same if we use 13
instead of LED_BUILTIN
. We specify the output to a digital pin by using the Arduino function digitalWrite(pinNumber, HIGHorLOW);
.
At line 9, the Arduino board is sleeping for 3 seconds. We tell the Arduino to sleep by using the Arduino function delay(timeInMilliSeconds);
.
Run the program by clicking the toggle switch and see what happens!
The above program turned on the built in LED and waited for 3 seconds and then did nothing. Can you turn the LED off after the 3 seconds?