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 Motion Sensor
  • Library
  • Global Variables
  • setup( ) function
  • Check for Motion
Export as PDF
  1. References for Wiring & Coding

Motion Sensor

PreviousServo MotorNextMagnetic Switch

Last updated 6 years ago

The 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 , 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.

motion sensor
male JST connector
Motion Sensor