Tutorial: Arduino Basics - Part 1
Content
- Introduction
- Setup the IDE and open a new sketch
- Communication between the Arduino and your computer
- The first breadboard circuit
- Variable types and switching on the LED
- Flashing LED
- If-Then-Else
- Light switch via button
- Switch-Case-Function
- Click Here For Part Two...
Introduction
For this tutorial you need some Arduino gadgets. You will find the following gadgets on Amazon or other resellers:
- 1x Red-, Blue-, Green-LED
- 3x 220 Ohm Resistors
- 1x 10K Ohm Resistor
- 1x Button
- 10x Breadboard-Cables
- 1x Arduino (or other) UNO
- 1x Breadboard
You can buy every single item or all together in a bundle. For example, “Miuzei Starter Kit for Arduino” (link) includes all needed items and more, only for 42,99 € on Amazon.
For programming you will need the “Arduino IDE” software. Download the “Arduino IDE” here. Do not worry, this software is absolutely free!
Setup the IDE and open a new sketch
First of all, the software setup:
- Connect your Arduino with your Computer
- Start “Arduino IDE”
- Go to “Tools” -> “Board” -> and select your Arduino Board, in our example “Arduino/Genuino UNO”
- Go to “Tools” -> “Port” -> and select the Port, for example “COM6 (Arduino/Genuino UNO)”
- Now the Arduino IDE knows how to find your Arduino
Now we turn to the Arduino IDE itself! Open a new sketch (“File” -> “New”). In the new sketch are already two functions added. Please never delete these two functions! But why do we need these functions?
The setup function:
This function is called whenever you start your Arduino. This happens on the one hand when you upload a new sketch to the Arduino board, or on the other hand when the Arduino is switched on again. The latter happens not only when you plug it into a USB port, but also when you connect it to another power source.
We use the function to initialise new variables or other things.
The loop function:
This function repeats as fast as possible all the code it contains; so it works like a repeater. Therefore, the function is the output of all further calculation steps after the setup. For example, measured values are read out and processed again and again, or the Arduino waits until a set time and then executes a function.
Communication between the Arduino and your computer
Start the Arduino IDE, open a new sketch and connect the Arduino to your computer.
To be able to read sent data from the Arduino, we have to use the "Serial Monitor". We will always use this to check if our programmes are working properly.
However, the Arduino and the computer need to know which "line" they are using to communicate with each other. Like on Discord, the same server/connection address must be used to be able to talk. We give this information to the Arduino in the setup function discussed in the chapter before. We now write the first line of code in the setup function:
void setup() {
Serial.begin(9600);
}
This information says: Start the serial monitor with the address "9600".
After initialising the serial monitor, we will send a message that our programme has started. We thus continue with the setup function:
void setup() {
Serial.begin(9600);
Serial.println("---Programme started---");
}
"println" means that we print the following text ("---Program started---") on a new line. A common mistake is to use the apostrophe instead of the quotation marks. This results in a number being returned and not the text. If we were to just write "print", then no paragraph would be inserted after the text, new text would thus simply be appended to the current line. The output without paragraph is practical if you want to output several pieces of information in one line using several "print" commands. You may have noticed that we write a semicolon (";") after each statement. The programming software needs this symbol to know where the statement ends. This is equivalent to a full stop at the end of a sentence.
Now it is time to run our programme for the first time. But first we have to start the serial monitor. To do this, go to "Tools" -> "Serial Monitor", a new tab will appear at the bottom. Check that "9600 baud" is selected in the drop-down menu in the upper right corner of the tab, which corresponds to the selected communication frequency of the Arduino and computer.
Now we upload the code to the Arduino. Click on the green right arrow button in the top left corner of the IDE. The software now checks our code to see if any errors have occurred, then the code is uploaded. Our Arduino shows us in the Arduino IDE as a progress bar with a small light that the code has been uploaded.
If the code is compiled and executed, we will find our coded message in the serial monitor.
The first breadboard circuit
We will now build our first Arduino circuit, which we will use in the next section. First, though, let's explain the breadboard.
Each slot on the breadboard is for one pin of a cable, LED or resistor. In each row, the columns (terminal strips) a, b, c, d, e and f, g, h, i, j are connected to each other, i.e. if you apply a voltage to one of these slots, all four other pins will also have this voltage. Otherwise, the + and - columns (busbars) are not connected horizontally (row) but vertically (column). So if you apply a voltage to one of the slots, all the pins in the column have this voltage.
We will use the + and - columns to supply voltage to all components.
We will now build a circuit with a red LED. The black lines represent the cables. Our red LED has two pin lengths, a long and a short pin. Please place the long pin in the same row as the 220 ohm resistor. Connect the other end of the 220 ohm resistor to the digital connector 2. Connect the other pin of the red LED to the ground (GND) of the Arduino as shown in the picture via the power rails. We will need more connections to ground later.
Variable types and switching on the LED
After we have finished building the small circuit, we now come to programming the corresponding program to turn on the LED.
The Arduino itself knows very little about its connections with other components, thus we have to teach it by code which port is connected to what. First we set all the information about the port in the setup function. The following code is used to determine the port:
pinMode(redLED, OUTPUT);
The "pinMode"function has two parameters, first the pin number and second the type of the pin, which can be either INPUT or OUTPUT. An INPUT pin functions as a receiver of data, for example from a temperature sensor. An OUTPUT pin functions as a transmitter that sends data to a component, for example to an LED in the form of power on or power off.
We will start with a new sketch, so we will create a new sketch in the Arduino IDE. If we would run the previous code in the IDE, we would get an error that the variable "redLED" is unknown. Therefore, we still have to define the variable. There are two ways to do this, either we define the variable globally, which means that all functions know the variable, or locally, which means that only the corresponding function knows the variable. In order to define a variable globally, we write it above the setup function, at the beginning of our sketch. If we want to define a variable locally, then it must be written in a function, for example inside the loop function. In order to define a variable, the following scheme must be followed: [variable type] [variable name] = [value]; For our variable "redLED" we define it globally, i.e. at the beginning of the sketch, as an integer variable with the value 2, because we have connected pin 2 to the LED. Our sketch looks like this:
int redLED = 2;
void setup(){
pinMode(redLED, OUTPUT);
}
void loop(){
}
Before we continue with the sketch, we now introduce the most common variable types with examples and their short cuts.
Type | Variable short cut | Example | Description |
Integer | int | 10000 | Number in the range from -32.767 to +32.767 |
Long | long | 350.000 | All numbers |
Float | float | 1.2 | Decimal numbers |
Char | char | a | One letter |
String | String | Hello | An array of letters |
Array | array | 1,2,3,5,4 | A list of any previous variable type. For example, a string is also an array of chars. |
Boolean | boolean | true | Can only have two states: true (1) or false (0) |
The last missing part for our programme to turn on the LED is the command to turn on the power at pin 2. Since all pins without an 'A' in front of the number are digital pins, we use the command "digitalWrite". The command needs two parameters, first the pin number and second the state (LOW - no current, or HIGH - current on). To switch on the LED, we add the digitalWrite command after the pinMode line. The code is now complete and we can compile and upload it. Don't forget to connect the Arduino to the computer. The finished code looks like this:
int redLED = 2;
void setup(){
pinMode(redLED, OUTPUT);
digitalWrite(redLED,HIGH);
}
void loop(){
}
Flashing LED
So far, we have been able to switch on the LED by code, which it then remains until eternity. For a little more show and to introduce the delay command, we now want to get the LED to flash. For this purpose, we use the loop function, which repeatedly executes the code it contains. However, if we were to write two lines consisting of the digitalWrite command first with the HIGH state and then the LOW state into the loop function, we would not see any blinking due to the high execution speed (in the range of milliseconds). We must allow time to elapse between the change of state. This can be done with the delay command, which stops all processes for the set time. Be aware that the delay command stops all processes of the sketch. However, if you only want to interrupt one, this is not possible with the delay command. Since we only want to make one LED flash, we do not have to take this into account. The waiting time must be passed in milliseconds. One second corresponds to 1000 milliseconds. In our case, we will wait half a second in each state, so 500 milliseconds. We adapt our previous sketch to the following code and you can upload, and run it on the Arduino right away. The LED should now flash. Feel free to adjust the wait time to see what happens.
int redLED = 2;
void setup(){
pinMode(redLED, OUTPUT);
digitalWrite(redLED,HIGH);
}
void loop(){
digitalWrite(redLED,HIGH);
delay(500);
digitalWrite(redLED,LOW);
delay(500);
}
If-Then-Else
Now we come to the most important chain of command of all: the if-then-else condition. This condition determines what happens if a condition is true or if it is not, what should be done instead. Basic, but important for every programme, because a computer cannot decide on its own. We have to define the rules for him.
The basis of the if-then-else condition (always the same in every programming language) is the if-condition itself:
If (condition){
//several statements that are executed if the condition evaluates to true.
}
After the "if" we need to set a condition that the Arduino/computer can check when the algorithm is executed. After the curly brackets you can write your code for what should happen if the condition is true. A condition can be, for example, if the LED is off, then it should be switched on.
If the condition is false, the algorithm does nothing in this case. If we want the computer to do something else, we have to add the else algorithm:
If (condition){
//several statements that will be executed if the condition evaluates to true.
}
else{
//several statements that will be executed if the condition evaluates to false
}
Below is sample code that checks whether the variable is greater than or less than 100. Since the value was previously set to 50, "Smaller" is output to the serial monitor.
int value = 50;
if(value<100){
Serial.println("Lower");
}else{
Serial.println("Higher");
}
It is even possible to combine more than one if-then-else condition. The following example code checks whether the value is equal to 100 in addition to the previous example. The result would be the same, as 50 is still lower than 100.
int value = 50;
if(value<100){
Serial.println("Lower");
}else{
if(value == 100){
Serial.println("Equal");
}else{
Serial.println("Higher");
}
}
Light switch via button
We need to add a button to our circuit before the next step. We want to program that our red LED lights up when the button is pressed. For this we need a 10k-Ohm resistor, a button and five cables. In the picture you can see how the button is placed and connected to the Arduino by cable and resistor.
Now that the hardware has been adjusted, we need to adjust the software. To do this, we first teach the Arduino that there is now a button connected to it. We remember that the pinMode function needs to know the type of the pin. A button is clearly an INPUT because it communicates its state (pressed - not pressed) to the Arduino.
int redLED = 2;
int button = 5; //NEW LINE
void setup(){
pinMode(redLED, OUTPUT);
pinMode(button,INPUT); //NEW LINE
//DELETE THIS LINE: digitalWrite(redLED,HIGH);
}
void loop(){
digitalWrite(redLED,HIGH);
delay(500);
digitalWrite(redLED,LOW);
delay(500);
}
After we have made the button available to the Arduino via the setup function, we now want to read and use its state. To do this, we globally define another variable of type Boolean with the name "buttonstate", to which we do not assign an inertial value. Secondly, as the first line in the loop function, we insert a query of the state of the button using the function "digitalRead". The function digitalRead needs only one parameter and that is the pin number. It then outputs the value present at the pin. If we would store this value in an integer variable, a number would be written there. But since the button only knows on or off, we can also store its state in the previously defined Boolean variable, which takes away the query whether the value corresponds to LOW (a value of zero) or HIGH. Once we have queried the state, we evaluate it in an if-then-else condition. If the button is pressed, the variable Buttonstate is true or equal to one, then the LED should be switched on, otherwise it is switched off. This results in the following code:
int redLED = 2;
int button = 5;
boolean buttonstate; //NEW LINE
void setup(){
pinMode(redLED, OUTPUT);
pinMode(button,INPUT);
}
void loop(){
//CHANGED:
buttonstate = digitalRead(button);
if(buttonstate == true){
digitalWrite(redLED,HIGH);
}else{
digitalWrite(redLED,LOW);
}
}
If you don't want to have to hold the button down for the LED to light up, you have to add another variable. This variable stores the current state of the LED and changes the state accordingly when the button is pressed. We define the Boolean variable "lightstate" with the inertial value false (here standing for off) and adjust the loop function. It is important that when the state of the LED changes, the lightstate variable is also adjusted. For better handling it may be advisable to add a delay of 200 ms at the end of the loop function.
int redLED = 2;
int button = 5;
boolean buttonstate;
boolean lightstate = false; //NEW LINE
void setup(){
pinMode(redLED, OUTPUT);
pinMode(button,INPUT);
}
void loop(){
buttonstate = digitalRead(button);
//CHANGED:
if(buttonstate == true){
if(lightstate == false){
digitalWrite(redLED,HIGH);
lightstate = true;
}else{
digitalWrite(redLED,LOW);
lightstate = false;
}
}
delay(200); //MAYBE NOT NEEDED
}
Switch-Case-Function
As the last theory in part 1 of the Arduino Basics Tutorial we deal with the switch-case function. This makes it much easier for us to check a variable for its value if it can take more than two values. Otherwise we would have to use a lot of if-then-else conditions. The switch-case function has a simple structure:
switch(variable) {
case value: //something happens
break;
case another value: //something else happens
break;
//...
}
The switch() statement is the beginning and calls the variable whose value is to be compared with different values. Each comparison starts with "case". After that you write the value (only integer or string/char are allowed) and then a colon. Now you can string together all the statements that should be executed if the variable is equal to the value. When you finish the statements for a case, end them with "break;", which stops the switch function and executes the code after it.
As an example, let's change the previous code (the only difference besides the switch-case function is that we changed the type of the lightState variable from Boolean to Integer, so true = 1 and false = 0):
int redLED = 2;
int button = 5;
boolean buttonstate;
int lightstate = 0; //CHANGED
void setup(){
pinMode(redLED, OUTPUT);
pinMode(button,INPUT);
}
void loop(){
buttonstate = digitalRead(button);
if(buttonstate == true){
//CHANGED:
switch(lightstate){
case 0:
digitalWrite(redLED,HIGH);
lightstate = true;
break;
case 1:
digitalWrite(redLED,LOW);
lightstate = false;
break;
}
}
delay(200); //MAYBE NOT NEEDED
}

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.