PIR sensor as a switch (without it's state change)

Hello.

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.

i want to turn on and off the led by reading only the HIGH value of the sensor since that detects movement

Do you mean that the LED should turn on the first time the input goes HIGH and off again when the input goes HIGH a second time ?

If so, look at the StateChangeDetection example in the IDE to see how to detect when an input goes HIGH (or LOW)

proximity sensor != PIR...

const byte LedPin = 2;         // led or relay
const byte SensorPin = 6;        // PIR sensor (infra red)

bool lastSensorState;

void setup() {
  pinMode(LedPin, OUTPUT);   
  pinMode(SensorPin, INPUT);   
  lastSensorState = digitalRead(SensorPin);

}

void loop(){
  //check for going high
  if (digitalRead(SensorPin) && !lastSensorState) {           // verify movement
    digitalToggle(LedPin);
    lastSensorState = !lastSensorState;
  }
}


inline void digitalToggle(byte pin){
  digitalWrite(pin, !digitalRead(pin));
}

@UKHeliBob

I did, but the sensor will also go off by itself.

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.

@septillion
It's not doing anything....:-??

I indeed goofed up...

const byte LedPin = 13;         // led or relay
const byte SensorPin = A1;        // PIR sensor (infra red)

bool lastSensorState;

void setup() {
  pinMode(LedPin, OUTPUT);   
  pinMode(SensorPin, INPUT);   
  lastSensorState = digitalRead(SensorPin);
}

void loop(){
  //check for going high
  if (digitalRead(SensorPin) != lastSensorState) {           // verify movement
    lastSensorState = !lastSensorState;
    
    if(lastSensorState){
      digitalToggle(LedPin);
    }
  }
}

inline void digitalToggle(byte pin){
  digitalWrite(pin, !digitalRead(pin));
}

Mother of god... it works...

Thank you :smiley: