Skip to content

Latest commit

 

History

History
95 lines (74 loc) · 4.8 KB

1.Piezo Sounder Alarm.md

File metadata and controls

95 lines (74 loc) · 4.8 KB

#Piezo Sounder Alarm By connecting a piezo sounder to a digital output pin, we are going to create a wailing alarm sound. We are going to use the same principle that we used in Project 7 to make a pulsating lamp by creating a sine wave. But this time, we replace the LED with a piezo sounder or piezo disc.. When choosing your piezo sounder, make sure that you do not purchase an active buzzer, that is, one with a built-in oscillator. You need a passive piezo sounder that uses AC voltage, not DC.

Piezo Sounder (or piezo disc) 2 way screw terminal

2-Way Screw Terminal buzzer

#Connect It Up First, make sure your Arduino is powered off by unplugging it from the USB cable. Now take the piezo sounder and screw its wires into the screw terminal. Connect the screw terminal to the breadboard and connect it to the Arduino as in picture below. Next, connect your Arduino back to the USB cable and power it up.

circuit - piezo sounder alarm

####Enter the Code Open up your Arduino IDE and type in the following code:

float sinVal;
int toneVal;

void setup() {
        pinMode(8, OUTPUT);
}

void loop() {
        for (int x=0; x<180; x++) {
                // convert degrees to radians then obtain sin value
                sinVal = (sin(x*(3.1412/180)));
                // generate a frequency from the sin value
                toneVal = 2000+(int(sinVal*1000));
                tone(8, toneVal);
                delay(2);
        }
}

Once you have uploaded the code, there will be a slight delay, and then your piezo will start to emit sounds. If everything is working as planned, you will hear a rising and falling siren-type alarm, similar to a car alarm. The code for Project 11 is almost identical to the code for Project 7, so let’s see how it works.

####Code Overview First we set up two variables

float sinVal;
int toneVal;

The sinVal float variable will hold the sine value that will cause the tone to rise and fall in the same way that the lamp in Project 7 pulsated. The toneVal variable will be used to take the value in sinVal and convert it to a frequency we require.

In the setup function, we now set digital pin 8 to an output.

void setup() {
        pinMode(8, OUTPUT);
}

Then we move onto the main loop. As in Project 7, we set a for loop to run from 0 to 179 to ensure that the sine value does not go into the negative.

for (int x=0; x<180; x++) {

We convert the value of x into radians, as in Project 7:

sinVal = (sin(x*(3.1412/180)));

Then that value is converted into a frequency suitable for the alarm sound.

toneVal = 2000+(int(sinVal*1000));

We take 2,000 and add the sinVal multiplied by 1,000. This gives us a good range of frequencies for the rising and falling tone to go between as the sine wave rises and falls. Next, we use the tone() command that was introduced in IDE 0018 to generate the frequency at the piezo sounder.

tone(8, toneVal);

The tone() command requires either two or three parameters, thus:

tone(pin, frequency)
tone(pin, frequency, duration)

The pin is the digital pin being used to output to the piezo and the frequency is the frequency of the tone in hertz. There is also the optional duration parameter in milliseconds for the length of the tone. If no duration is specified, the tone will keep on playing until you play a different tone or you use the noTone(pin) command to cease the tone generation on the specified pin.

Finally, we run a delay of 2 milliseconds in-between the frequency changes to ensure the sine wave rises and falls at the speed we require.

delay(2);

You may be wondering why we did not put the 2 milliseconds into the duration parameter of the tone() command, like this:

tone(8, toneVal, 2);

This is because our for loop is so short that it will change the frequency in less than 2 milliseconds anyway, thus rendering the duration parameter useless. Therefore, a delay of 2 milliseconds is put in after the tone is generated to ensure it plays for at least 2 milliseconds before the for loop repeats and the tone changes again. You could use this alarm generation principle later on when you learn how to connect sensors to your Arduino. Then, you could activate an alarm when a sensor threshold has been reached, for example, if someone gets too close to an ultrasonic detector or if a temperature gets too high.

If you change the values of 2,000 and 1,000 in the toneVal calculation and the length of the delay, you can generate different alarm sounds. Experiment a little, and see what sounds you can make.