Motion Sensor

The motion sensor included in your Photon kit uses passive infrared (PIR) light to detect movement.

The motion sensor has a built-in 3-wire female JST connector. If necessary, attach a male JST connector, and plug it into different numbered rows on a breadboard. Then use jumper wires to connect the rows to the Photon. To make it easier to remember which wire is which, use corresponding black, white, and red jumper wires to match the motion sensor wires.

Motion Sensor

Photon Pin

Black - Data

any I/O pin

White - Ground

GND

Red - Power (5V)

V-USB (5V)

NOTE: The motion sensor requires 5V of power, so connect it (directly or indirectly) to the V-USB pin.

The first 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:

Code for Motion Sensor

Library

The motion sensor does not require any special code library.

Global Variables

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

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

If you are using multiple motion sensors, then be sure to give each motion sensor 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 motionPin1 = D0;
int motionPin2 = D1;

setup( ) function

Within the setup() function, you have to include a statement to set the pin mode for the motion sensor data pin variable:

pinMode(motionPin, INPUT_PULLUP);

If you are using multiple motion sensors, be sure to set the pin mode for each motion sensor's data pin variable.

Check for Motion

Code for checking the motion sensor would be placed within the loop() function or within a custom function.

The motion sensor can be checked by using a digitalRead() statement.

If the sensor is currently detecting motion, digitalRead() will return a value of LOW.

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 motion is detected.

// check motion sensor
int motionState = digitalRead(motionPin);

// LOW means motion is detected
if(motionState == LOW) {

    // insert code to do something if motion detected

} else {

    // optional: insert code to do something else if no motion detected

}

Think about what your Photon app should do if motion is detected:

  • 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