hi guys .The problem is that the sensor always detects motion and the led lights up. what could be the problem?
Program:
int blue = 13; // choose the pin for the LED
int inputPin = 2; // 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 x = HIGH;
int pirReading;
//int pulseSpeed = 100 ;
void setup() {
pinMode(blue, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin);
// read input value
if (val == HIGH) {
if (x == HIGH) { // check if the input is HIGH
digitalWrite(blue, HIGH);
Serial.println("Motion 2");
}
else{
digitalWrite(blue, LOW);
Serial.println("Motion 1");
}
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 {
if (pirState == HIGH){
if (x==HIGH){
x=LOW;
}
else {
x=HIGH;
}
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
//delay(pulseSpeed);
}
}
i tried another program but it didn't work either:
const int PIRinput = 2; //PIR connected here
const int led = 13; //Led connected here
int motion = 0;
void setup() {
pinMode(led,HIGH); //led as output
pinMode(PIRinput,LOW); //PIR as input
}
void loop() {
//reading the PIR status and stores to variable motion
motion = digitalRead(PIRinput);
if (motion == HIGH) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}