3

I have a PIR sensor attached to a Amica nodeMCU board routing 5v from VIN and attached via USB port for testing. When motion is deteted it connects to the internet and sends data to IFTTT which i receive a notification on my phone.

When I power up, the PIR pauses for calibration and then immediately goes HIGH and fires the motion detected call which i receive on my phone. However From then on it never goes LOW but every 5-8 minutes sends another HIGH call through even if there is no motion.

Testing

  • Tried using separate 5v supply to PIR same thing happens
  • I have tried both retrigger modes (H & L) and experienced the same result
  • The same happens with the Adafruit featherwing huzzah board.
  • I have tested the PIR without a micro-controller and can confirm that it functions correctly -lighting an led
  • I have tested with an Arduino Nano with the same code and it functions correctly - lighting an led

My Code modified version of Arduino Playground PIR Project

//Sends IFTTT every 5 minutes it detects motion

#include <ESP8266WiFi.h>
//WiFi Settings

// Set up macros for wifi and connection.
#define SSID "my-network"    // SSID
#define PASS "mypassphrase"      // Network Password
#define HOST "maker.ifttt.com"  // Webhost
//-------------------------------
const char* streamId   = "test";
const char* privateKey = "mysecretkey";

//PIR Settings
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  


int  interval = 1000; // Wait between dumps
boolean lockLow = true;
boolean takeLowTime;  
int ledPin = 1;
int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int nPIR_detect;
int motion = 2;

int minSecsBetweenUpdates = 300; // 5 minutes
long lastSend = -minSecsBetweenUpdates * 1000l;
//-------------------------------

// Begin Setup
void setup(){
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(".");
    delay(1000);
    }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);
   nPIR_detect = 0;  

  // Test ESP8266 module.
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();
  }
}

void loop(){
 if(digitalRead(pirPin) == HIGH){
       digitalWrite(BUILTIN_LED, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         sendData(String(motion));
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(BUILTIN_LED, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
}

void sendData(String motion){

  //Send the motion to IFTTT value1
  Serial.print("connecting to ");
  Serial.println(HOST);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(HOST, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/trigger/";
  url += streamId;
  url += "/with/key/";
  url += privateKey;
  url += "?value1=";
  url += "motion";

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + HOST + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

}

boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    Serial.println("Connection");
    return true;
  }
  else{
    Serial.println("No Connection");
    return false;
  }
}

OQ: Why does my PIR sensor stay high with ESP8266 breakout board?

In essence, I never see the serial message "motion ended at.."

  • What do the measured voltage levels look like? Is your breakout board to 5v tolerant? – Dave X Mar 10 '16 at 23:42
  • 1
    How are you converting the 5v signal to 3.3v? – Gerben Mar 11 '16 at 17:01
  • @Gerben which 5v signal? (from where?) – Dom UIXNZ Mar 13 '16 at 21:38
  • @Gerben the pir converts 5v to 3v – Dom UIXNZ Mar 30 '16 at 0:12
2

I'm sure you have probably done both these, but just in case you haven't...

I see from your code you are using Pin 2 for the PIR sensor. I'm sure your not as dumb as I am every time I program a ESP8266, but check you have connected the PIR to pin 2, that's not the connection labelled D2 its GPIO2, which may or may not be labelled.

Assuming that's right then the next possible problem may be that GPIO2 is shared with TXD1, its possible that this is being used. So try moving to GPIO4.

  • Changing from GPIO2 to GPIO4 worked for me. – Felby Mar 5 '17 at 3:01
1

I have the same issue with Huzzah and a PIR. The PIR is noisy and I get a false high every 10-20s while the code runs without any problem on an UNO. I think it is a problem of HF coupling from wifi interfering with the PIR's amplification circuit. I will try shielding and caps/ferrites to get rid of the HF on the PIR or use another power supply and couple the signal line via optocoupler.

1

The solution I found to this problem is use a 10k resistor between the PIR sensor output and the input pin of the ESP8266.

Not sure if 10K is the best value, someone else may find a better value, but for now, for me its working great.

John

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.