Skip to content

Motion Detection with PIR Sensor: A Simple Starting Point

Now, about this PIR sensor matter, you know, that small but clever friend that activates when movement occurs… I’ve experienced it too, like when you’re working on a project, and suddenly you think, “Maybe we should do something if it detects motion?” That’s when PIR sensors come into play. Truly, they are small but very useful. They are used in many places, from security systems to automatic lighting. Imagine, a light that unnecessarily stays on when you’re not in the room, or a lamp that suddenly turns on as you enter your home… All these happen thanks to this small sensor.

The principle behind this is not complicated at all. Think of it like this: “Oh my God, how difficult could it be!” but it’s not that tough. PIR sensors operate on the “Passive Infrared” principle. That is, they do not emit any signals themselves but detect the infrared radiation in the environment. Every living thing emits heat, right? These sensors detect those changes in heat. So, when you move, the heat radiating from your body triggers the change in temperature in the environment, and the sensor says, “Hey, someone’s here!”. Isn’t that great? It’s like you’ve installed a watcher in your house.

Of course, in practice, there can be some minor issues. For instance, sometimes the sensitivity is too high, and even your murmurs can trigger it, or vice versa, when you pass by, it doesn’t react. This can be a bit frustrating. I’ve experienced it myself in my project, like when I couldn’t set the sensitivity properly, and even when the door opens and closes, it triggered. Anyway, you need to play around with these settings.

So, how do we do this? Well, it’s not very complicated. Using popular platforms like Arduino is quite straightforward with these sensors. Usually, they have three pins: VCC (power), GND (ground), and OUT (signal output). From this output pin, you read the digital signal to understand if there’s movement. It works on a very simple logic: if the signal is ‘HIGH’, then there is movement; if it’s ‘LOW’, then there’s no movement. I believe many projects can be realized just with this simple logic.

Also, some models of these sensors include potentiometers for sensitivity and timing adjustments. You can turn these to set how sensitive the sensor is and how long the output signal stays active after detecting movement. With these settings, it’s possible to prevent unnecessary triggers. Sometimes, you set up something, and it doesn’t behave exactly as you want; you need minor adjustments. These potentiometers are for those fine-tuning needs.

Now, let’s look at how to code this with Arduino. Typically, the logic is: read from the sensor, if the signal is high (movement detected), turn on an LED or print a message. If the signal is low, everything is normal. That’s it. We can make this more complex, like sending notification to your phone or sounding an alarm when movement is detected. But for a beginner, this is the basic logic.

Here’s a sample code, and for clarity, let’s compare it with an incorrectly written version:

// WRONG (Simple reading – turns off immediately after movement stops)

const int pirPin = 2; // PIR sensor output pin const int ledPin = 13; // LED pin

void setup() {   pinMode(pirPin, INPUT);   pinMode(ledPin, OUTPUT);   Serial.begin(9600); }

void loop() {   int pirState = digitalRead(pirPin); // Read PIR sensor value

  if (pirState == HIGH) { // If movement detected (signal high)     digitalWrite(ledPin, HIGH); // Turn on LED     Serial.println("Movement Detected!");   } else { // If no movement     digitalWrite(ledPin, LOW); // Turn off LED   }   delay(100); // Short wait }

In this code, there is a problem: when movement is detected, the LED turns on, but when it stops, the LED turns off instantly. So, even if you leave the room, the light turns off immediately, which might not be practical or desirable. This is where we make it better.

Now let’s see the more correct and useful version. We’ll utilize a timer mechanism. When movement is detected, we turn on the LED and update a variable; even if movement stops, that variable keeps the LED on for a certain period. This way, the LED stays on for a set duration after movement stops.

// CORRECT (With Timer – stays on a while after movement stops)

const int pirPin = 2; // PIR sensor output pin const int ledPin = 13; // LED pin unsigned long previousMillis = 0; // To store the last time movement was detected const long interval = 5000; // How long the LED remains on (milliseconds) - 5 seconds

bool ledState = LOW; // Current state of the LED

void setup() {   pinMode(pirPin, INPUT);   pinMode(ledPin, OUTPUT);   Serial.begin(9600); }

void loop() {   int pirState = digitalRead(pirPin); // Read sensor value

  if (pirState == HIGH) { // If movement is detected     ledState = HIGH; // Turn on LED     previousMillis = millis(); // Record the last detection time     Serial.println("Motion Detected!");   }

  // If LED is on and the interval has passed, turn it off   if (ledState == HIGH && millis() - previousMillis >= interval) {     ledState = LOW; // Turn off LED     Serial.println("Time's up, LED turned off.");   }

  digitalWrite(ledPin, ledState); // Set LED state }

As you can see, this code uses variables like `previousMillis` and `interval` to create a timing mechanism. When movement is detected, `previousMillis` gets updated, and the LED remains on for the specified `interval`. This truly offers a more logical solution. Sometimes, a small detail can completely change a project’s atmosphere, and this is one of those. With this code, even after movement stops, the lights remain on for a certain period, increasing security and avoiding unnecessary flickering.

By the way, you can often find these sensors on sale online. Searching here reveals many different models and prices. I usually prefer to buy these basic sensors in bulk. It keeps them handy for future projects.

In conclusion, working with PIR sensors for motion detection is both fun and very practical. Once you understand the basic logic, you can easily adapt small code adjustments for your projects. Sometimes, a new project idea pops into your mind, and these sensors are an excellent starting point to turn those ideas into reality. Don’t hesitate to try, because you won’t know until you experiment!

Also, these sensors typically operate at 5V, powered directly from the Arduino’s 5V pin. Some models may operate at 3.3V, but the most common ones are compatible with 5V, so you might not need an additional power source.

If you want to take it further, you could combine these sensors with Wi-Fi or Ethernet modules to develop a smart home system. Imagine receiving a notification on your phone if motion is detected while you’re away. Thanks to technology, our lives are becoming easier and safer, aren’t they?

In summary, these sensors provide simple yet effective solutions. You can use them for various purposes, from building your own alarm system to automatic outdoor lighting. Creative projects are limitless, who knows what you might come up with?

Lastly, keep in mind that some of these sensors are sensitive to ambient light changes, which can sometimes cause night-time motion detection even if you don’t notice it during the day. This reflects sensor quality and settings. Sometimes, a sensor may be overly sensitive, or the opposite, depending on its quality and calibration.

I hope this simple explanation helps you take your first steps with PIR sensors. As always, the best way to learn is by doing. So, get a sensor, try it yourself, and experiment 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.