Sensor and presence

I tried to use PIR sensor for my light switch with 220v relay and work but i want that pir sensor work like Ultrasonic Rangefinder but i cant get bigger distance from ultrasonic.

I need sensor or solution for arduino, i want when folks open doors, sensor turn on light and keep light
turned on until sense people presence, when folks go to next room and close doors, light is turning off.

Sensor will be on the ceiling, near light bulb.

Any advice?

PIRs are very good if there's movement. Do the people move in the room? If they sit still then they become invisible to the PIR.

From that description, I fail to see how measuring distance can help.

Simply wait a short fixed time after the PIR sensor is triggered before turning the light off.

This is what most sensor lights do.

Weedpharma

I understand but i want that whenever is my presence in the room, light is on

PIR do the switch then wait some amount of time to turn off.

I want permanent switch off when sensor sense that i'm not in the room,

PIR turn on light when im in the room then after let's say 10 seconds turn off light and if i move then, PIR turn on light again.

I want that whenever i'm in the room light is on.

Have you already written a program that does not work as you wish?

Why not share it with us so we have at least some idea what you are doing?

Weedpharma

const int ledPin= 13;
const int inputPin= 2;

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
}

void loop(){
int value= digitalRead(inputPin);

if (value == HIGH)
{

digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, LOW);
}
}

This adafruit sketch is a good example of PIR code. Use of state variables allows it to distinguish between "new" and "old, continuing" motion:

/*
 * PIR sensor tester
 https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
 */
 
int ledPin = 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
 
void setup() {
  pinMode(ledPin, 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) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    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 {
    digitalWrite(ledPin, LOW); // turn LED OFF
    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;
    }
  }
}

You may also want to make the delay greater than 1 second. You need to hold the light on for say 30 seconds after last movement detected to allow for lack of movement while in the room. Or you could just make sure that whenever you are in the room, you are moving vigorously!

Weedpharma

Thank you for help.

I want that we focus exactly on what i'm asked in my question.

Is there any way, maybe with some other sensor than PIR that solve my qustion?

I repeat, i want that when i'm in the room, even when i don't move the light is staying on because sensor is always know that i'm in that room, when i go out of that room the light is turn off.

This is great example of what i want but i want angles catching, specifications and longer distance sensing to do like pir sensor.

You could try ultra sonics. If you know what the distance is to the wall in empty room, anything less means you are in the room.

Weedpharma

you mean like this one ultrasonic sensor in youtube example that i put link up in my previous reply?

Yes. Depending on size of the room, you may need more than one.

Weedpharma

can somebody tell me from personal experience, what is the best distance and angle that Ultrasonic Sensor can achieve?