PIR Motion Sensor

This documentation includes installation guidelines and sample code for your hardware.

Windows 10 IoT Core Library

PIRSensor library is responsible for reading motion state from the PIR sensor.

To use the library, add the following using statement to the top of your C# code.

using TurtaIoTHAT;

Then, create an instance of the PIRSensor class.

PIRSensor pirSensor

Now you're ready to access the library by calling the pirSensor instance.

Initialization

PIRSensor

Initiates the AM312 PIR sensor to detect human motion.

PIRSensor()
  • Parameters

    • None

Interrupts

PIR Motion Detect State Changed

Notifies on PIR motion detect state change.

event PIRMotionDetectEventHandler PIRMotionDetectStateChanged
  • Event Args

    • Bool Motion: True if motion is detected. False if not.

Disposal

To clean up the resources used by the class, call the Dispose() method it when the application is closing.

void Dispose()

This method cleans the resources used by the class.

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There is one example of this library.

Motion Detect Sample App

This application demonstrates reading motion state from the PIR sensor.

To run the example:

  1. Open the sample project you downloaded.

  2. Make sure "ARM" is selected from the solution platforms selection.

  3. Deploy the sample to the Raspberry Pi using the "F5" key.

Sample Code

MainPage.xaml.cs
using Windows.UI.Xaml.Controls;
using System.Diagnostics;
using TurtaIoTHAT;

namespace PIRMotionDetectSampleApp
{
    public sealed partial class MainPage : Page
    {
        // PIR Motion Detect Sensor
        static PIRSensor pirSensor;

        public MainPage()
        {
            this.InitializeComponent();

            // Initialize PIR Motion Detect Sensor and set event handler
            Initialize();
        }

        private void Initialize()
        {
            // Initialize PIR Motion Detect Sensor
            pirSensor = new PIRSensor();

            // Subscribe to PIR Motion Detect State Changed event
            pirSensor.PIRMotionDetectStateChanged += PIRMotionDetectStateChanged;
        }

        private void PIRMotionDetectStateChanged(object sender, PIRMotionDetectEventArgs e)
        {
            // Write motion state to output / immediate window
            Debug.Write("PIR Interrupt: ");
            Debug.WriteLine(e.Motion ? "Active" : "Passive");
        }
    }
}

Result

When you run the sample, it prints the motion state changes to the Visual Studio Output / Immediate Window.

PIR Interrupt: Active
PIR Interrupt: Passive

To stop the application, use the "Stop" button on the Visual Studio.

Last updated