> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/magnetic-switch.md).

# Magnetic Switch

A [magnetic switch](https://www.sparkfun.com/products/13247) can detect whether a door, window, drawer, etc. is open or closed.

The switch consists of two pieces, and it can detect whether these two pieces are close to each other. The two pieces have to be within 20 mm (about 0.75 inches) of each other – otherwise, the switch will detect that it is open.

The piece with the wires contains a special switch called a [reed switch](http://www.explainthatstuff.com/howreedswitcheswork.html) that moves in response to the presence (or absence) of a magnetic field. The other piece (without the wires) contains a small magnet, so it can activate the reed switch when the two pieces are close to each other.

For example, the piece with the wires (the reed switch) could be attached on a stationary door frame, and the piece without the wires (the magnet) would be attached on the door near its edge. The two pieces would be installed so they are very close together when the door is closed. When the door is opened, the reed switch detects that the magnet has moved away.

These magnetic switches are commonly used in security systems, but they are also used in many other products. For example, a doorbell uses a magnetic switch. Small magnetic switches are also used in many laptops and tablets to put the device to sleep when the laptop lid or tablet cover is closed.

![Magnetic Switch](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXGkd3GZwxMve7sTN%2Fmagnetic-switch.jpg?generation=1527608057842885\&alt=media)

| Magnetic Switch     | Photon Pin  |
| ------------------- | ----------- |
| Wire 1 (either one) | any I/O pin |
| Wire 2              | GND         |

The second part of Experiment 9 in the online SparkFun Photon Experiment Guide shows how to connect the motion sensor. Here is the connection diagram from Experiment 9:

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXGkfIDUX9TDEiMHr%2Fexperiment-9b.jpg?generation=1527608057835979\&alt=media)

The stationary piece of the magnetic switch (the piece with the wires) is connected to the Photon device. The other piece of the magnetic switch would be attached to the movable object (door, window, drawer, etc.) that can be opened.

## Code for Magnetic Switch

### Library

The magnetic switch does **not** require any special code library.

### Global Variables

In the global variables, you should declare which pin is being used as the magnetic switch pin. The example below declares a variable called "magnetPin" (but you could use a different variable name).

```java
// if necessary, change data pin number to match your wiring
int magnetPin = D0;
```

If you are using **multiple** magnetic switches, then be sure to give each magnetic switch a unique variable name. Use variable names that will make sense when someone reads your code.

```java
// if necessary, change pin numbers to match your wiring
int magnetPin1 = D0;
int magnetPin2 = D1;
```

### setup( ) function

Within the `setup()` function, you have to include a statement to set the pin mode for the magnetic switch pin variable:

```java
pinMode(magnetPin, INPUT_PULLUP);
```

If you are using multiple magnetic switches, be sure to set the pin mode for each magnetic switch's pin variable.

### Check If Switch Closed or Open

Code for checking the magnetic switch would be placed within the `loop()` function or within a custom function.

The magnetic switch can be checked by using a `digitalRead()` statement.

* If the magnetic switch is **closed**, `digitalRead()` will return a value of `LOW`.
* If the magnetic switch is **open**, `digitalRead()` will return a value of `HIGH`.

Often, a local variable is used to store the result of the `digitalRead()`, so that your code can use this result to make decisions based on whether the switch is open or closed.

```java
// check magnetic switch
int magnetState = digitalRead(magnetPin);

// HIGH means switch is open, LOW means switch is closed 
if(magnetState == HIGH) {

    // insert code to do something if switch is open

} else {

    // optional: insert code to do something else if switch is closed

}
```

Think about what your Photon app should do if the magnetic switch detects it is open:

* turn on a light
* make a sound with the speaker (a tone with a frequency of 2000Hz works well)
* send a notification to your web app
* etc.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/magnetic-switch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
