D-5 Speaker Code

Next, you'll add code in your Smart Security device app to control the speaker.

The basic steps to control a speaker in your app code are:

  1. Declare a global variable to store the I/O pin number for the speaker.

  2. Set the pin mode for the speaker pin in the setup() function.

  3. Use tone() statements to produce sounds.

Global Variable for Speaker

Declare a global variable to store the I/O pin number for the speaker by adding this code statement before the setup() function:

int speaker = D1;

If you connected your speaker to a different I/O pin other than D1, then modify this code statement to list the correct pin number for your speaker.

Set Pin Mode for Speaker

Set the pin mode for your speaker pin by adding this code statement within the setup() function (between the curly braces):

pinMode(speaker, OUTPUT);

Produce Tone using Speaker

The speaker will be used to produce an "alarm sound" if the motion sensor detects motion.

The tone() method is used to produce a sound of a specific frequency (pitch) for a specific duration of time. The speaker can produce tones ranging in frequency from 20Hz (very low pitch) to 20KHz (very high pitch), covering the full range of sounds that humans can hear.

Add this code statement within your checkMotion() custom function (inside the curly braces of the if statement that checks the value of motionState – add the code before the delay()):

tone(speaker, 1000, 250);

The tone() method requires three parameters inside its parentheses (in this order):

  1. The I/O pin number, which can be the actual pin number (such as: D1, etc.) or a variable that stores a pin number. In this case, the variable named speaker is listed.

  2. The frequency for the tone, which can be an integer value (whole number) or a variable that stores an integer. The value can be between 20-20000 hertz. Lower numbers will have a lower pitch, while higher numbers will have a higher pitch. In this case, the frequency will be 1000 hertz, which will be a "less annoying" pitch than what an alarm sound would typically use.

  3. The duration for the tone, which can be an integer number (whole number) or a variable that stores an integer. The value represents the number of milliseconds that the tone will be played (1000 ms = 1 second). In this case, the duration will be 250 ms (0.25 seconds), which will be a much shorter duration than what an alarm sound would typically use.

Flash App to Device

Be sure your Photon device is connected to Wi-Fi and Particle Cloud.

Flash your app code to your Photon device by clicking the Flash icon in the left navigation bar. (Particle Build will first save and verify your code before flashing it.)

Once your Photon has downloaded the new app and restarted, the updated app will start running:

  • Press the button to "arm" your Smart Security device. (This simulates having entered a passcode into a keypad to arm the device.) The LED light should turn on when the device is in "armed" mode. Confirm that the speaker produces an alarm sound when motion is detected. The sensor should be able to detect motion in the environment up to 10 feet away.

  • Press the button again to "disarm" the device (the LED should turn off). Because of the 2 second delay required when motion is detected, you may have to press the button more than once (or hold the button down slightly longer). Once the device is in "disarmed" mode, confirm that motion near the sensor does not produce an alarm sound.