PIR Motion Sensing LED Display

Building a PIR Sensor-Based Motion Detector with Arduino — NaveTECH & UNIR Series — Episode # 08

J3
Jungletronics
6 min readJan 24, 2024

--

We will showcase two distinct codes for motion detection using a PIR sensor.

The first employs a straightforward state-based methodology, efficiently detecting and responding to alterations in the PIR sensor’s output.

In contrast, the second code leverages interrupt, meticulously monitoring the PIR sensor for changes in motion, capturing precise timestamps at the onset and cessation of motion, and calculating the duration of the detected motion. An LED serves as a visual indicator, illuminating to denote the presence of motion. Additionally, the time lapse between motion and no motion is meticulously recorded and printed to the serial monitor, providing valuable insights for debugging or logging purposes.

Introduction:

In a world increasingly intertwined with technology, the ability to sense and respond to environmental changes is paramount. The Motion Sensing LED Display project leverages the power of Arduino and a PIR (Passive Infrared) sensor to create an intelligent motion detection system. This project not only showcases the capabilities of microcontroller-based systems but also provides a practical application for monitoring and responding to motion in a given environment.

In this project, we employed two essential hardware components:

Adafruit offers an excellent tutorial for this particular sensor type; you can explore it here. This hobbyist sensor utilizes the BISS0001 (“Micro Power PIR Motion Detector IC”), an exceptionally cost-effective chip. The BISS0001 processes the sensor’s output, performing minor adjustments, and generates a digital output pulse from the analog sensor data. This budget-friendly chip proves to be a valuable component in transforming raw sensor signals into usable digital information for motion detection applications.
This particular PIR sensor is configured with a pre-defined time duration for which it expects the LED to remain on (set to approximately 7000 milliseconds in the context of my experimentation). For additional information, please consult this media file. PIR sensors are highly suitable for a wide range of foundational projects or products that necessitate the detection of individuals entering or exiting an area, or approaching it.

How does it work:

image from this page. The PIR (Passive Infrared) sensor is adept at detecting movement within a specified range, typically encompassing both animal and human motion. Comprising a pyroelectric sensor, it possesses the capability to discern varying levels of infrared radiation. Crucially, the PIR sensor operates passively, refraining from emitting any energy but instead adeptly receiving it from its surroundings. In essence, as a human or animal traverses the sensor’s field, it interrupts the initial slot of the PIR sensor, inducing a positive differential change between the two halves of the sensor. Conversely, when the human or animal departs from the sensing area, the sensor registers a negative differential change between its two halves. This distinctive mechanism allows the PIR sensor to effectively capture and interpret infrared variations, enabling it to discern the presence or absence of movement within its detection zone.
Here is the Fritzing schema for your convenience.

Development:

The development of this project revolves around the utilization of a PIR sensor, which detects changes in infrared radiation associated with motion. The Arduino microcontroller interprets these signals, triggering appropriate responses. A connected LED serves as a visual indicator, illuminating when motion is detected and turning off when the motion subsides. The interrupt-driven design efficiently captures these changes in real-time, enabling swift responses without the need for constant polling.

Furthermore, the project introduces the concept of time lapse calculation. By recording timestamps at the onset and conclusion of motion, the Arduino calculates and displays the duration of detected motion. This additional feature enhances the project’s versatility, allowing users to not only detect motion but also gather insights into its temporal characteristics.

The simplicity and effectiveness of this motion sensing system make it an excellent starting point for those venturing into the realms of Arduino-based projects. The code structure and hardware integration showcase fundamental principles of embedded systems and sensor interfacing, providing valuable insights for beginners and serving as a foundation for more complex applications.

Here is the first code (polling approach)

This Arduino code is crafted to connect with a PIR (Passive Infrared) sensor for motion detection, employing the polling technique. The following provides an explanation for each segment of the code:

1 . Variable Declarations:

. ledPin: Specifies the pin connected to the LED (Light Emitting Diode) that will be used to indicate motion. In this case, it's connected to digital pin 13.

. inputPin: Specifies the pin connected to the PIR sensor, which detects motion. In this case, it's connected to digital pin 2.

. pirState: Represents the current state of motion. It is initially set to LOW, assuming no motion is detected.

. val: A variable for reading the status of the PIR sensor.

2 . Setup Function:

. Configures the ledPin as an output, indicating that it will be used to control an LED.

. Configures the inputPin as an input, indicating that it's connected to the PIR sensor.

. Initializes serial communication at a baud rate of 9600 for debugging purposes.

3 . Loop Function:

. Repeatedly executes the following code in a loop. Reads the current state of the PIR sensor using digitalRead and stores it in the val variable.

. If motion is detected (val == HIGH): Turns on the LED (ledPin) to indicate motion by setting it to HIGH using digitalWrite.

. If the previous state (pirState) was LOW (no motion detected), it prints Motion detected! to the serial monitor using Serial.println.

. Updates pirState to HIGH. If no motion is detected (val == LOW): Turns off the LED (ledPin) to indicate no motion by setting it to LOW.

. If the previous state (pirState) was HIGH (motion was detected), it prints Motion ended! to the serial monitor. Updates pirState to LOW.

The code utilizes a simple state-based approach to detect and respond to changes in the PIR sensor’s output. It turns the LED on when motion is detected and turns it off when motion ends. The serial monitor is used to print messages indicating the state changes for debugging purposes. This code is suitable for basic motion detection applications using Arduino and a PIR sensor.

And here is the second code (interruption approach)

This Arduino code is designed to interface with a PIR (Passive Infrared) sensor to detect motion using interruption instead of polling techniques. Let me break down the key components and functionality:

  1. Variable Declarations:
  • ledPin: Specifies the pin connected to the LED (Light Emitting Diode) that will be used to indicate motion.
  • inputPin: Specifies the pin connected to the PIR sensor, which detects motion.
  • pirState: A volatile variable representing the current state of motion. It is set to LOW initially, assuming no motion is detected.
  • val: A volatile variable for reading the status of the PIR sensor.
  • motionStartTime: An unsigned long variable to store the timestamp when motion is detected.
  • motionEndTime: An unsigned long variable to store the timestamp when motion ends.
  • timeLapse: An unsigned long variable to store the duration between motion and no motion.

2. Setup Function:

  • Configures the ledPin as an output, indicating that it will be used to control an LED.
  • Configures the inputPin as an input, indicating that it's connected to the PIR sensor.
  • Attaches an interrupt to the inputPin that triggers the interrupt_routine function whenever the state of the input pin changes.
  • Initializes serial communication at a baud rate of 9600 for debugging purposes.

3. Loop Function:

  • The loop function is left empty, indicating that it doesn't perform any specific tasks in a continuous loop. The main work is done in the interrupt routine.

4. Interrupt Routine (interrupt_routine):

  • This function is called whenever there is a change in the state of the PIR sensor (inputPin).
  • Reads the current state of the PIR sensor using digitalRead and stores it in the val variable.
  • If motion is detected (val == HIGH):
  • Turns on the LED (ledPin) to indicate motion.
  • If the previous state (pirState) was LOW (no motion detected), it prints "Motion detected!" to the serial monitor.
  • Records the start time of motion using millis().
  • Updates pirState to HIGH.
  • If no motion is detected (val == LOW):
  • Turns off the LED (ledPin) to indicate no motion.
  • If the previous state (pirState) was HIGH (motion was detected), it prints "Motion ended! lapse time = " along with the time lapse between motion start and end.
  • Records the end time of motion using millis().
  • Calculates and prints the time lapse.
  • Updates pirState to LOW.

This code essentially monitors the PIR sensor for changes in motion, records timestamps when motion starts and ends, and calculates the duration of motion. The LED is used to visually indicate the presence of motion. The time lapse between motion and no motion is printed to the serial monitor for debugging or logging purposes.

Conclusion:

In conclusion, the Motion Sensing LED Display project embodies the fusion of hardware and software to create an intelligent motion detection system. The incorporation of a PIR sensor and Arduino microcontroller allows for a seamless response to environmental changes, while the time-lapse calculation adds a layer of sophistication to the project. Whether utilized for home automation, security applications, or simply as an educational endeavor, this project exemplifies the potential of embedded systems to enhance our interaction with the physical world. As we continue to explore the vast possibilities of microcontroller-based projects, the Motion Sensing LED Display stands as a testament to the innovation and practicality achievable through such endeavors.

That’s all folks!

👉GitHub (Projects #37 and #38)

Credits & References

Arduino Interrupts with PIR Motion Sensor by makersportal.com

PIR Motion Sensor by learn.adafruit.com

Parallax Motion Sensor from Radio Shack by neufeld.newton.ks.us

PIR Sensor Working Principle by robu.in

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️