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

Now you’ll let your Arduino work on its own for a change, without having to operate potentiometers. You’ll use your piezo buzzer to play the beginning of Beethoven’s famous piece “Für Elise” through a series of tone functions.

That alone would be a bit too simple. Additionally, you’ll use a for loop to set how many times this melody should play consecutively.

First, let’s look at the melody. This is based on a sketch available on Github that saves you a lot of work. The user spara took the trouble to enter each individual note, its length, and the pauses into an Arduino sketch. Here are the first two notes, for example:

tone(10, 329.63, 300);
delay(350);

tone(10, 311.13, 300);
delay(350);

 

All these notes are located in the void setup() function of your sketch. This means they are actually only played once when you start your Arduino. That’s nice, but in this lesson, loops come into play, allowing you to control how many times the piece is played.

The For Loop

Let’s first take a look at the for loop. In our case, it is created according to the following schema:

for (int i = 0; i < 3; i++) {
  //Code to be repeated
}

 

First, of course, the command for, which initiates the loop. In the parentheses ( ) there are some additional parameters.

First, the so-called initialization. This basically means that you declare a variable and assign it a value (zero in this case). This variable is typically simply referred to by the letter i. It’s the counter that increases after each iteration of the loop – until a certain condition is no longer true.

___STEADY_PAYWALL___

Now the condition: with i < 3 you check if the variable i is less than 3. Only as long as this is the case will the code in the loop be executed again. As soon as i has the value 3, the loop stops.

Finally, the counter, which increases after each iteration: For this you use i++

As you already know, this increases a value by 1. As soon as the code within the curly braces { } has run through once, this command increases the value in the variable i from 0 to 1.

Let’s look at the individual iterations:

  • Before the first iteration, i equals the value 0 -> The loop begins.
  • After the first iteration, i equals the value 1 -> The condition i < 3 is true -> The loop runs a second time.
  • After the second iteration, i equals the value 2 -> The condition is still true -> The loop runs a third time.
  • After the third iteration, i equals the value 3 -> The condition is false -> The loop stops and is not executed further.

With a for loop, you can control quite well how many times something should be executed in succession. This could also be, for example, the blinking of your LED.

The While Loop

There is another method to create a loop in your sketch: while

Here too, it checks whether a condition is true, and the code between the curly braces is executed as long as the condition remains true.

int i = 0;

while (i < 3){
  //Code to be repeated
  i++;
}

 

However, there are two important differences from the for loop: First, the variable for the counter – i.e., i – must be created before the loop. Additionally, the counter is incremented in the code that’s being repeated.

Otherwise, this loop works the same way. Apart from the fact that you need less space in the sketch for the for loop, there is another consideration that sometimes speaks for for, sometimes for while:

In a for loop, you must know in advance how often it should run. In a while loop, on the other hand, the “looped” code itself can influence whether it will be repeated once more. For example, a special event could increase the counter so much that the loop stops.

The more experience you gain in programming, the easier the decision between loops will become. And then you’ll eventually come across the third loop method: do...while, which we won’t cover in this course.

Back to Beethoven: Download the sketch from the downloads onto your Arduino. Do you hear some classical music? 🙂

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