Magnetic Switch

A magnetic switch 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 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.

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:

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

// 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.

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

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.

// 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.

Last updated