Particle Cloud and Events
Table of contents
- Using a PIR
- Step 1: Wiring the PIR
- Step 2: Setting up the Sketch
- Step 4: Compiling and sending to your Particle
- Step 5: Check the Event
- Find out more
PIR’s or Pyroelectric/Passive InfraRed Sensors allow you to detect motion. They detect infrared radiation (given off as heat from humans for example) to find out if a person or other living thing is within the range of the sensor. They’re what are used in most home security systems to detect motion and intruders.
They’re low cost and reasonably easy to use, but more complicated than the sensors we’ve worked with before.
You can read more about them here: https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/overview
Using a PIR
We’re going to create a simple motion detector that will publish an event when someone moves in front of it.
You will need
-
Particle Microcontroller
-
A LED
-
A PIR sensor
Step 1: Wiring the PIR
It has three pins, that need to be wired specifically. Wire the + or RED wire to power, wire the black or terminal marked - to Ground and wire (normally the middle) terminal to a digital Pin which we’ll use to read from.
PIR’s will tell us if motion is detected or not, by allowing power to flow or turning it off. They give us a binary (two options) signal - HIGH or LOW. This is why we wire them to the digital pins.
The circuit, wiring and components are as follows:
Step 2: Setting up the Sketch
int inputPin = D4; // choose the input pin (for PIR sensor)
int ledPin = D2; // LED Pin
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int calibrateTime = 10000; // wait for the thingy to calibrate
void setup()
{
pinMode( ledPin, OUTPUT );
pinMode(inputPin, INPUT); // declare sensor as input
}
void loop()
{
// if the sensor is calibrated
if ( calibrated() )
{
// get the data from the sensor
readTheSensor();
// report it out, if the state has changed
reportTheData();
}
}
void readTheSensor() {
val = digitalRead(inputPin);
}
bool calibrated() {
return millis() - calibrateTime > 0;
}
void reportTheData() {
// if the sensor reads high
// or there is now motion
if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an eent
if (pirState == LOW) {
// we have just turned on
Particle.publish("designingiot/s19/motion");
// Update the current state
pirState = HIGH;
setLED( pirState );
}
} else {
if (pirState == HIGH) {
// we have just turned of
// Update the current state
pirState = LOW;
setLED( pirState );
}
}
}
void setLED( int state )
{
digitalWrite( ledPin, state );
}
Step 4: Compiling and sending to your Particle
Compile and move about!
Step 5: Check the Event
Open the Particle Console and you should see a new event published with the name designingiot/s19/motion