> For the complete documentation index, see [llms.txt](https://docs.idew.org/code-internet-of-things/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.idew.org/code-internet-of-things/tutorials/create-security-system/4.5-speaker-code.md).

# 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:

```cpp
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):

```cpp
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()`):

```cpp
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.)

<div align="left"><img src="/files/-LJEjiwZZ_lGiFWTeLmW" alt="Flash Icon"></div>

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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.idew.org/code-internet-of-things/tutorials/create-security-system/4.5-speaker-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
