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.