CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for Magnetic Switch
  • Library
  • Global Variables
  • setup( ) function
  • Check If Switch Closed or Open
Export as PDF
  1. References for Wiring & Coding

Magnetic Switch

PreviousMotion SensorNextPotentiometer

Last updated 7 years ago

A 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 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

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:

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.

magnetic switch
reed switch
Magnetic Switch