I'm trying to use a PIR sensor as a switch but the internal on/off timer messes up with my project and turns off my damn LED.
The project is simple (as an ideea) and it's like this.
There is a lamp involved and a proximity sensor.
When I move my hand under the sensor I want the lamp to turn on. (After this movement there are some seconds where the sensor turns off by itself - and also will modify it's state from HIGH to LOW).
After let's say 3 seconds, I want to turn off the lamp and I swipe with my hand under the sensor again. This time I want the lamp to go off.
I can't manage to use the sensor as a switch and almost everytime after it's timer runs out, it switches off my light or leave it turned on.
The code (one of the failed versions)
int led = 2; // led or relay
int sensor = 6; // PIR sensor (infra red)
int val = 0; // sensor status
void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
}
void loop(){
val = digitalRead(sensor);
if (val == HIGH) && digitalRead(led) == LOW) { // verify movement
digitalWrite(led, HIGH); // turn LED ON
delay(100);
}
else{
if (val == HIGH && digitalRead(led) == HIGH)
digitalWrite(led, LOW);
delay(100);
}
}
With this code it stays ON... so... what am I doing wrong?
So the "some seconds where the sensor turns off by itself" is that based on your code? I would think you dont want to have any time involved if you want to leave the light on until you move your hand under it again.
But if you do, increase it to a longer period, i have within my home PIR light switches that control my most common areas, since everyone seems to forget to turn off the lights in the kitchen or hall, i have them in there.. they are programmed to turn off after no movement for 5 minutes.. as long as there is movement, the light will stay on, if no movement, then 5 minutes the lights will turn off..
If you are controlling the timing, adjust accordingly or use it to trigger the on / off and forget about the timing.
So the "some seconds where the sensor turns off by itself" is that based on your code? I would think you dont want to have any time involved if you want to leave the light on until you move your hand under it again.
That is based on the sensor/hadrware.
I want the sensor to act as a simple movement switch.
However i want to turn on and off the led by reading only the HIGH value of the sensor since that detects movement and ignore it's LOW value.
So I will have a HIGH signal (induced by me, the wanted signal) and a LOW signal induced by the sensor after a few seconds when it does not detect any movement.