What might be the code to make PIR Sensor work?

After watching this video from Particle on YouTube I wanted to create the same project. But I am not getting the right result. Does anyone know what will be the code written for this video ?

Hi
You can look at Arduino examples, like https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
The only change I see is use the correct pin number.

A quick searching for ‘PIR’ gave me this: Sprint 7: Particle.publish() released! Let’s build a cloud-connected motion detector

2 Likes

Hi @Moors7,

This is my code. It’s basically the same code from the above example. I have also corrected all 3 wires as mentioned above. But I am getting digitalRead(inputPin) always high even if there is no motion or body parts infront of the sensor.

  /*
 * Connected sensor
 * Particle.publish() + PIR motion sensor = awesome
 * Thanks to Adafruit for the reference and inspiration
 */

int inputPin = D0;              // choose the input pin (for PIR sensor)
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(inputPin, INPUT);     // declare sensor as input
  Serial.begin(9600);
}

void loop(){
  if (calibrated()) {
    readTheSensor();
    reportTheData();
  }
}

bool calibrated() {
  return millis() - calibrateTime > 0;
}

void readTheSensor() {
  val = digitalRead(inputPin);
  Serial.println("the val was: ");
  if(val==HIGH){
      Serial.println("it was high");
  }
     }

void reportTheData() {
  if (val == HIGH) {
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      Particle.publish("particle-hq/motion");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

This is my output:

the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high
the val was:
it was high

Do you know where I am doing wrong ? This is my first time with PIR sensor and I would love to use it. It looks simple but it is getting infinite inputs even if there is no motion or body parts in-front of it.

@Fabien In that case though I am not getting any input. I have changed my pin into D2. Even if I move my hands infront of it, it is not doing anything.

I see something weird though… When I remove the D2 pin, it says “Motion ended !” and when I put it back “Motion detected!”. Will that help in troubleshooting in any way ?

Well, you should check the datasheet of your PIR, to make sure the code is compatible with your particular device.
I would just connect power to the PIR, then measure the voltage on the output pin, to confirm if the “motion detected” is active at high or low level.
Also, my device has 2 potentiometer; one to adjust sensitivity; one to adjust the duration of output after motion is detected. The second one can last up to 5min between two detections; it makes debugging more difficult.
There is also a jumper to configure.
One more thing; my PIR works in 5V. I added a voltage divided with 2 resistors between the output and the Photon pin.

As Fabien mentioned, double-check the supply voltage for your PIR sensor. The one in the Electron Sensor Key, the HC-SR501, requires a minimum of 4.5V for the power in, so you need to use VUSB (assuming you’re powering your Photon by USB) instead of 3V3. That sensor has a 3.3V output, but even if it was 5V it would be safe to connect directly to any pin other than A3 or DAC when used as a digital input, pinMode INPUT.

@Fabien @rickkas7 Thank you so much for the suggestions. I got a new PIR sensor which does not have 2 potentiometer. After connecting my new PIR sensor I was able to get the correct output on the new PIR sensor.

It made me realize my old PIR sensor was set to wrong sensitivity and output latch delay. I played with the orientation until I got the right output and now I am getting the result as expected. Thank you so much for your suggestions and help.

2 Likes