PIR Sensor and Button Relay Control

I'm baby fresh to coding but, I'm looking to build a code to trigger a fan (relay) with a PIR sensor and a press button.

Idea is to kick the fan on if someone walks up to the outside of the door with PIR (forgot to mention fan is mounted above a door). Or to also trigger fan with button if opened from inside.

Basically building a commercial entrance fan for my house to help keep the bugs out.

All help is greatly appreciated.

Please read the forum guidelines. That page tells how to ask a good question and what we need to know in order to assist you.

What Arduino board?

Which motion sensor?

Which relay?

What fan? How is it powered?

What kind of switch?

How is the circuit powered?

If you have a wiring diagram, please post it. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies. Fritzing type diagrams are less than desirable.

Supply data sheets or other technical data for the relevant components.

1 Like

How long you want the fan to run after it's triggered?

LIke this? 5 sec fan

-jim lee

You can get some ideas about the circuit here: Connect a Relay and PIR Motion Sensor to an Arduino - Tutorial - YouTube

Hi @modivation_inc

welcome to the arduino-forum

Even if somebody should post a almost ready to use code. It will be very good if you know the fundamental things about programming arduinos

Take a look into this tutorial:
Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

does "commercial entrance fan" mean that you want to sell this device in series?

hm I guess if this shall be efficient you would need a huge fan which creates a somehow stiff breeze blowing against the person that wants to come in. :wink:

Hello modivation_inc

Welcome to the worldbest Arduino forum ever.

Consider this small fan control sketch to get started.

#define ProjectName "PIR Sensor and Button Relay Control"
//make names
enum Input {Button, Pir};
enum Output {Fan};
//variables
// --- configure your project here: 
constexpr uint32_t airingTimeByPir {1000};
constexpr uint32_t airingTimeByButton {2000};
constexpr uint8_t Inputs[] {A0, A1};  // configured as INPUT_PULLUP
constexpr uint8_t Outputs[] {9};
//-------------------------------------------------
//structures
struct BUTTON
{
  uint8_t pin;
  uint8_t stateOld;
};
struct TIMER
{
  uint32_t intervalMillis;
  uint32_t previousMillis;
  uint8_t control;
};
struct FANTIMER
{
  BUTTON button;
  TIMER airingTime;
  void run(uint32_t currentMillis)
  {
    uint8_t stateNew = digitalRead(button.pin) ? LOW : HIGH;
    if (button.stateOld != stateNew)
    {
      button.stateOld = stateNew;
      if (stateNew == HIGH)
      {
        airingTime.control = HIGH;
        airingTime.previousMillis = currentMillis;
      }
    }
    if (currentMillis - airingTime.previousMillis >= airingTime.intervalMillis && airingTime.control) airingTime.control = LOW;
  }
};
FANTIMER fantimers[]
{
  {Inputs[Button], LOW, airingTimeByButton, 0, LOW},
  {Inputs[Pir], LOW, airingTimeByPir, 0, LOW},
};
void setup()
{
  Serial.begin(115200);
  Serial.println(ProjectName);
  for (auto &fantimer : fantimers) pinMode(fantimer.button.pin, INPUT_PULLUP);
  pinMode(Outputs[Fan] , OUTPUT);
}
void loop()
{
  uint32_t currentMillis = millis();
  for (auto &fantimer : fantimers) fantimer.run(currentMillis);
  digitalWrite(Outputs[Fan],fantimers[Button].airingTime.control || fantimers[Pir].airingTime.control);
}

Have a nice day and enjoy coding in C++.



PIR Sensor

2 channel relay


Metro mini for mock up

Arduino uno rev3

momentary switch


this is basically the fan I'm using 110v 1a max

triggering the fan (relay) opening door from inside
The idea I have at the moment is, when the door is closed the momentary switch will be pressed and the fan (relay) needs to be off. Then when the door opens and releases the momentary switch the fan needs to kick on for roughly 15,000ms.

triggering fan (relay) from outside
The idea is to trigger fan (relay) with movement with the PIR sensor. Then door will be open, which will trigger the momentary switch, and that signal needs to be ignored.

A fan operating at a voltage of 110V means that your complete product must fullfill a lot of security requirements. Do you have a graduation / certificates in electricity / electronics?

more likely, safety.

Yes you are right. Safety is the word that suits much better in this case.

yes I have exp with electrical just not coding. I can build automotive wiring harnesses from scratch, built a homemade spot welder, and my own cctv set up. I understand and respect the danger of higher voltage.
I could make the device work easily without code just using mechanical systems but, I've decided to expand my know how and learn Arduino.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

This is the basic layout of what I'm doing. Please let me know if anything looks off.

reed switch D2
PIR sensor A1
Relay A2

And this constant worry about safety is killing progress. Why are the countries worried about safety the only ones in decline? Life is risk, success is risk, not excepting that fact is a mental illness.

rant about development of society

if you mean the USA my impression is:
a part of the rich try to make money by lawsuits that the riscs were not clearly enough explained on products.
IMHO the declining is caused by something else:
Another aspect is that real good education is not for free. Conseqeunce the folks get dumber and dumber. It is easier and more effective to shout a scary boooh! to get the dumb people run in the direction the rich wants them to run (and vote).
additionally each rich individual wants to keep his money and in summary this leads to a slowly increasing dumbness of the people. The next level is started by chatGPT.
But I'm straying away into politics.

back to programming

Aha. ..... And what is your question?

Question is, did I select the right pins for what I'm doing?
Also I'm working through a couple books about Arduino coding, as suggested.