-->
Page 1 of 1

NodeMCU and PIR sensor problem

PostPosted: Sat May 20, 2017 1:48 pm
by lesept
Hello
This is almost my first attempt of using the ESP8266. I have a NodeMCU 0.9 and I want to test it with a PIR sensor. Ultimatey, I'll send an intrusion alarm to a RPi...
But right now, I just want to have the PRI sensor work properly. I have done this before on the RPi so I know how it works. But there is something wrong with the NodeMCU.

I use the sketch that can be found everywhere on the net (see below).
Code: Select all/*
 * PIR sensor tester
 */
 
int ledPin = 4;                 // choose the pin for the LED
int inputPin = 14;               // 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
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  Serial.println(val);
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    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;
    }
  }
}

The LED is connected to D2 pin and the signal of the PIR sensor to the D5. I'm not sure about which pin is 5V and which is ground on the sensor.
When I run the sketch, the value of the sensor remains high forever and the LED is lit. I can't have any movement detection. What's wrong ?
Thanks for your help