Course Content
The Arduino UNO
In the following three lessons, you will get to know your microcontroller - the Arduino UNO. You will learn more about its history, its functions, and most importantly: how to power it.
0/3
The Arduino IDE
For beginners, the Arduino IDE (Integrated Development Environment) is usually the first choice – and for good reason. You can program all Arduino boards with it and manage libraries for sensors, displays, etc. It also features the "Serial Monitor," where you can output data and troubleshoot.
0/2
Your first sketch
In the following lessons, you'll get to know the basic structure of an Arduino sketch and write your own programs. Let's get started!
0/2
The Serial Monitor
Now let's turn our attention to the Serial Monitor – a feature of the Arduino IDE that you will use in virtually every one of your projects.
0/2
Variables
No programmer can avoid variables. In the following lessons, you'll learn what types there are and what you can do with them.
0/3
Controlling an LED
Now it's time for more hardware! In the next lessons, you'll connect an LED to your Arduino. You'll first turn it on and off with a button. After that, you'll build a dimmer to control the brightness of the LED.
0/6
Lie Detector
Discover the entertaining side of electronics by building your own simple lie detector with your Arduino. This fun project uses basic components to measure skin resistance changes when someone might be telling a fib, perfect for adding some playful suspense to your next gathering with friends.
0/1
There’s music inside!
Your Arduino can do much more than "just" make LEDs shine at different brightness levels. For example, it can make music. In the following lesson, you'll learn how to use a piezo buzzer and coax some charming tones out of it.
0/3
A Theremin with Ultrasound
Do you want to make a bit more music? In this lesson, you'll build a theremin that you operate with your HC-SR04 ultrasonic sensor. You move your hand toward and away from the sensor - your Arduino calculates the pitch of the tones from the distance, which are then played through your piezo buzzer.
0/5
The Sound Sensor
Ready to explore how your Arduino can respond to sounds? In this lesson, we'll connect a sound sensor to your Arduino and learn how to make it respond to both digital noise detection and analog volume levels.
0/1
Build an Alarm System
In this project, you will build your own alarm system. It consists of three components: the sound sensor, which you have just learned about, the active piezo buzzer, and the RGB LED.
0/2
The DHT11 Temperature Sensor
Let's move on to another component that you'll certainly use in many projects: the temperature sensor. In this case, the popular DHT11, which can measure not only temperature but also humidity.
0/3
Arduino Course for Beginners

Let’s move on to another important concept in programming: conditional statements – also known as if…else… With these statements, you can create branches in your sketch for specific events.

We’ll use the LED that’s already on your breadboard. You’ll also use the counter again that increases a number by 1 every second and displays it in the Serial Monitor.

This time, the LED should only light up when the current number is divisible by 3. For all other numbers, it remains off.

For this, conditional statements are ideal. But let’s take it one step at a time. First, declare some variables at the beginning of your sketch:

int ledPin = 9;
int number = 1;
int remainder;

___STEADY_PAYWALL___

You already know the first two from previous lessons. The third variable remainder is needed to test whether the current number is divisible by 3. You set the value of remainder to 1 initially.

In the code above, you see another shorthand form. Instead of writing:

int remainder = 1;

 

Just the name of the variable and a final semicolon ; is sufficient.

This also works with Strings:

String text; // creates an empty String just like:
String text = "";

 

The Setup Function

The void setup() function also contains code you already know. Here you start the Serial Monitor and define the pinMode of ledPin:

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

 

The Loop Function

In the loop, you first output the current value of the variable number in the Serial Monitor – during the first pass, that’s 1.

Serial.println(number);

 

Then follows the test whether this number is divisible by 3. You do this with the so-called modulo operator.

remainder = number % 3;

 

You write the modulo with a percent sign %. It calculates the remainder when one integer is divided by another. If you divide 3 by 3 – the remainder is 0. If you divide 4 by 3, the remainder is 1. You store this remainder in the variable of the same name.

 

If the remainder is 0…then…

Now comes the conditional statement. Whenever the variable remainder contains the value 0 (meaning the current number is divisible by 3), the LED should light up. If not, then it should remain off.

It works like this:

if (remainder == 0) {
  digitalWrite(ledPin, HIGH);
} else {
  digitalWrite(ledPin, LOW);
}

 

Expressed in pseudocode, this statement would look like:

if remainder is 0, then
  turn the LED on;
  if not, then
  turn it off;

 

In the first statement if, you check if something is true – in our case, whether the value 0 is stored in the variable remainder (and the current number is thus divisible by 3). If that’s actually true, the code between the curly braces { } is executed. This is the first possible branch in your code.

Afterwards, you use else to provide another branch for all other cases, which is contained in the curly braces that follow. In these cases, the LED is turned off.

Comparison Operators

Whenever you check whether something is true or false, you need the so-called comparison operators.

In the code above, you made this check with a double equals sign ==:

remainder == 0

 

If the value 0 was stored in the variable remainder, this check returned true and the code in the curly braces was executed.

In all other cases, the check resulted in false and the code in the curly braces after else was executed.

There are several other comparison operators that you probably already know from math class:

==  is something equal?
!=  is something not equal?
<   is something less than?
<=  is something less than or equal to?
>   is something greater than?
>=  is something greater than or equal to?

 

With these, you can check, for example, whether a number is less than or greater than another. Something special is !=, because with this, something becomes true when one side does not correspond to the other:

3 != 4 // is true

 

Back to the sketch. There are still two lines missing for the counter, which you already know.

number++;
delay(1000);

 

In the first line, you increase the value in the variable number by 1, and in the second line, you wait one second before you start the next iteration of the loop function. Now upload the sketch from the downloads to your Arduino and try it out.

In the next lesson, another component is added: a potentiometer, with which you can dim the LED.

Exercise Files
conditional_statements.zip
Size: 1.35 KB
We don't track you. Enjoy your cookies while making awesome projects!