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 that you have assembled everything on your breadboard, it’s time for the program. You can find the sketch in the Exercise Files for this lesson. Before uploading, please check once more whether the pins in the sketch match those where you connected the sound sensor, the piezo buzzer, and the RGB LED.

Below, you’ll learn more about the individual parts of the program and their functions.

The Variables for the Connections

At the beginning of the sketch, you define which hardware you have connected to which pins. Of course, you can also define these as constants using const. Additionally, you need a few variables for the brightness of the individual color channels of the RGB LED (e.g., brightnessRed = 150) and the volume measured by the sound sensor. You initially set these to zero.

int ledRed = 11;
int ledGreen = 10;
int ledBlue = 9;

int brightnessRed = 150;
int brightnessGreen = 150;
int brightnessBlue = 150;

int noise = 0;
int sensor = A1;

int piezo = 4;

___STEADY_PAYWALL___

The Setup Function

Here there’s nothing you haven’t already seen. You set the pins for the LED and the piezo as OUTPUT and start the Serial Monitor.

void setup() {
  
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);

  pinMode(piezo, OUTPUT);

  Serial.begin(9600);
}

 

The Loop

This is where it gets exciting. First, you measure the ambient volume, as all subsequent actions in the sketch will be based on this – whether your alarm system triggers or not.

noise = analogRead(sensor);
Serial.println(noise);

 

Next comes the first conditional statement. If the noise level is below a certain value, the LED should light up green – which means “All clear.” The value of 200 (and the following values in further instructions) can of course be adjusted. Also make sure that when you first start the alarm system, you calibrate the sound sensor so that it’s below the value you’ve set when it’s quiet.

if(noise <= 200){
  analogWrite(ledGreen, brightnessGreen);
  analogWrite(ledRed, 0);
  analogWrite(ledBlue, 0);
  digitalWrite(piezo, LOW);
}

 

As mentioned, in this state the LED should light up green. You achieve this by only illuminating the green color channel with the brightness you defined as brightnessGreen. The other two channels for red and blue are set to zero brightness, so they’re off.

The piezo should not sound here either, which is why you don’t supply it with power. You achieve this with the parameter LOW in the digitalWrite() function.

Another Instruction

Now if the noise level rises somewhat, but still isn’t high enough for an alarm, a second conditional instruction comes into play – with else if{}.

else if(noise > 200 && noise <= 350){
  analogWrite(ledRed, brightnessRed);
  analogWrite(ledGreen, brightnessGreen);
  analogWrite(ledBlue, 0);
  digitalWrite(piezo, LOW);
}

 

These commands are executed when the volume is between 201 and 350. As mentioned, experiment with these values to adjust them to your circumstances.

So if the volume is in this range, the light of the LED changes from green to yellow. There is no separate color channel for this color, which is why you simply mix it. As you know, yellow is a mixture of red and green. Therefore, you switch on these color channels of the LED and let the blue channel go out.

Alarm!

Now if it gets even louder, the alarm should sound. The LED shines in a rich red and the piezo buzzer starts to beep shrilly.

else if(noise > 350){
  analogWrite(ledRed, brightnessRed);
  analogWrite(ledGreen, 0);
  analogWrite(ledBlue, 0);
  digitalWrite(piezo, HIGH);
  delay(10000);
}

 

This time you only turn on the red channel of the LED. You also now send current from the Arduino to the piezo – with the function digitalWrite(piezo, HIGH);

Certainly, an alarm system should make noise until it is turned off. For a first experiment, however, perhaps one second is enough. That’s why there’s a delay() of 1,000 milliseconds at the end.

And that’s it! Upload the sketch to your Arduino, calibrate your sound sensor, and try out your alarm system.

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