Hmm, not entirely sure why that wouldn’t work. You could try giving it some time to calibrate before polling it. Could you try this code:
/*
* Connected sensor
* Spark.publish() + PIR motion sensor = awesome
* Thanks to Adafruit for the reference and inspiration
*/
int inputPin = D6; // 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);
}
void reportTheData() {
if (val == HIGH) {
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
Spark.publish("PIR_SENSOR_TRIGGERED!");
// We only want to print on the output change, not state
pirState = HIGH;
RGB.control(true);
RGB.color(0,255,0);
delay(2500);
RGB.control(false);
}
}
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;
}
}
}
It’ll output over Serial, will blink the onboard LED and will send out a SSE. Should give you some visual confirmation. @BulldogLowell’s suggestion is also worthwhile trying, although the sensor should work with 3.3V as well, according to the datasheet, but you never know…
Let me know if that worked for you.