Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Beginner needs Help with Netduino & PIR Motion Detection!


  • Please log in to reply
15 replies to this topic

#1 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 08:36 AM

Hello All, I am hoping someone will be able to help me with my problem here. I am a beginner and am trying to wire up a PIR Motion sensor but am not having any luck at all.. I have attached pictures of my code and my wiring setup of the Netduino and PIR sensor for info. Here is my problem: In my code I have set up an interupt port so that it tells me when the voltage changes and I have set it up to digital input port 0. When it detects movement I was expecting the "port_OnInterrupt" function to be called but I am getting nothing!!!! I have even tried putting a breakpoint in there but am getting nothing! I have absolutly no experience with electrics so I am wondering if I have wired the sensor up wrong in some way. I have wired the red wire of the PIR to the 5V pin on the netduino. I have wired the black wire of the PIR to the Gnd pin on the netduino I have wired the brown wire of the PIR to the Digital Input port 0 pin on the netduino. As I understand it when it detects movement it sends a signal down the brown wire which is why I was expecting a change of signal in my code on the Digital Input port 0. Any help with this would be greatly appreciated. Thanks

Attached Files



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 28 August 2011 - 10:25 AM

Hi waxie, Could you please post your code? What seting are you using when you set up your interrupt -- high edge, low edge, or both? Also, have you enabled the interrupt (using port.EnableInterrupt)? Welcome to the Netduino community, Chris

#3 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 10:41 AM

Hi waxie,

Could you please post your code?

What seting are you using when you set up your interrupt -- high edge, low edge, or both? Also, have you enabled the interrupt (using port.EnableInterrupt)?

Welcome to the Netduino community,

Chris


Thank you Chris. Its nice to be here.

For the interrupt I didnt know which setting to use so I have used both (InterruptMode.InterruptEdgeBoth). You mentioned enabling the interrupt. I didnt know you needed to do that. Perhaps that is where I am going wrong because I dont think I have done that.

Here is my full code:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusApplication_MotionDetection
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        static InterruptPort port = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
 
        public static void Main()
        {
            // write your code here

            port.OnInterrupt += new NativeEventHandler(port_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void port_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            bool val = port.Read();

            if (val == true)
            {
                led.Write(true);
            }
            else
            {
                led.Write(false);
            }

            Thread.Sleep(1000);
        }

    }
}

Thanks in advance.

Adrian.

#4 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 10:54 AM

Hi there,

I have just added the code:

port.EnableInterrupt();

after I setup the Interupt event handler but it still doesnt seem to be working!

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 28 August 2011 - 11:10 AM

Is the sensor same as the one sold by Sparkfun? If yes, it requires a pull-up resistor on its alarm pin - so you can either try enabling the internal pull-up resistor by passing Port.ResistorMode.PullUp to the interrupt port constructor, or use an external one (connect 4k7 ~ 10kΩ resistor between the alarm pin and power supply). Alarm is active low, so InterruptMode.InterruptEdgeLow should work fine.

port.EnableInterrupt();

Technical note: EnableInterrupt() is called automatically when a handler is being added to OnInterrupt event, in your case it really does nothing. You'd need to call DisableInterrupt() method first for it to take effect.

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 28 August 2011 - 11:25 AM

static void port_OnInterrupt(uint data1, uint data2, DateTime time)
{
    bool val = port.Read();
    ...
}

The above code has a small issue - if you are interested in the port state at the time the interrupt happened, use the value of data2 parameter (the handler prototype is in fact ...(uint portId, unit state, DateTime time)). I am not familiar with the sensor, so I don't know the duration of the alarm pulse, but if it is only several microseconds long, the Read() method call may miss it and return incorrect value, because in .NET Micro framework the interrupts are queued and it may take a while (tens of microseconds) before handlers are executed.

#7 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 11:59 AM

Is the sensor same as the one sold by Sparkfun? If yes, it requires a pull-up resistor on its alarm pin - so you can either try enabling the internal pull-up resistor by passing Port.ResistorMode.PullUp to the interrupt port constructor, or use an external one (connect 4k7 ~ 10kΩ resistor between the alarm pin and power supply). Alarm is active low, so InterruptMode.InterruptEdgeLow should work fine.


Technical note: EnableInterrupt() is called automatically when a handler is being added to OnInterrupt event, in your case it really does nothing. You'd need to call DisableInterrupt() method first for it to take effect.


Hi there.. I have just looked at the sparkfun one and yes I believe it is. Im in the UK and brought mine from www.coolcomponents.co.uk. This is the one I brought: this one

Wow, I have learnt so much from this post. I didnt know about the pull up resistor on the Netduino. Also I will try to change my interrupt mode to InterruptEdgeLow. Also thanks about for explaining about the EnableInterrupt call too.

I'll make these changes and let you know how I get on.

#8 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 12:02 PM

The above code has a small issue - if you are interested in the port state at the time the interrupt happened, use the value of data2 parameter (the handler prototype is in fact ...(uint portId, unit state, DateTime time)). I am not familiar with the sensor, so I don't know the duration of the alarm pulse, but if it is only several microseconds long, the Read() method call may miss it and return incorrect value, because in .NET Micro framework the interrupts are queued and it may take a while (tens of microseconds) before handlers are executed.


Again thank you for this valuable information. Im a complete newbie here and I only got the Netduino board yesterday in the post and this is my first project!

Im loving it though!

Thanks again.

#9 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 04:47 PM

Hi there guys. Hmmm, Im still not getting what I would expect from my mini project here. I have made the changes to my code suggested by CW2 but im still scracthing my head over this simple app! - My problem is that when I deploy my code the interrupt never fires. If I wiggle and take out the middle brown wire to the PIR then the interrupt fires lots of times but not when I wave my hand across the sensor! I am thinking I must be doing something stupid with regards to the wiring setup of the PIR sensor but I have not got a clue what I am doing wrong. Here is my current code I am using:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusApplication_MotionDetection
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        static InterruptPort port = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
 
        public static void Main()
        {
            // write your code here
            Debug.Print("Application Start!");

            port.OnInterrupt += new NativeEventHandler(port_OnInterrupt);

            port.EnableInterrupt();

            Thread.Sleep(Timeout.Infinite);
        }

        static void port_OnInterrupt(uint portId, uint state, DateTime time)
        {
            Debug.Print("Interrupt fired");

            led.Write(true);

            Thread.Sleep(1000);

            led.Write(false);

            Thread.Sleep(1000);

            port.ClearInterrupt();
        }

    }
}

Can anyone offer any suggestions for me as im stuck with this one?

Thanks in advance.

#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 28 August 2011 - 06:20 PM

...I have not got a clue what I am doing wrong.

I missed it in the first post, but IMHO your wiring is not correct: brown is GND and black is Alarm.

Note: You don't need neither EnableInterrupt() (as per my previous post), nor ClearInterrupt() calls (it has effect only for level interrupts, not edge).

#11 waxie

waxie

    Member

  • Members
  • PipPip
  • 13 posts

Posted 28 August 2011 - 07:00 PM

I missed it in the first post, but IMHO your wiring is not correct: brown is GND and black is Alarm.

Note: You don't need neither EnableInterrupt() (as per my previous post), nor ClearInterrupt() calls (it has effect only for level interrupts, not edge).


Doh!!! - I assumed the black was the ground wire but you are right! It now works.. Thanks alot for your info. Excellent stuff.

I have also cleaned up the code and removed references to EnableInterrupt() and ClearInterrupt()

#12 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 12 September 2012 - 05:55 AM

Doh!!! - I assumed the black was the ground wire but you are right! It now works.. Thanks alot for your info. Excellent stuff.

I have also cleaned up the code and removed references to EnableInterrupt() and ClearInterrupt()


Hi Waxie, I see you are not using a resistor in your example provided which differs that this other sample that I found:

http://forums.netdui...ch__1#entry6912

Are your readings reliable all the time?


Do you mind posting your final code for this functionality?

#13 Blues74

Blues74

    New Member

  • Members
  • Pip
  • 1 posts

Posted 29 September 2012 - 05:46 AM

+1 for posting final code.

#14 Jamiewarren

Jamiewarren

    New Member

  • Members
  • Pip
  • 1 posts

Posted 20 December 2012 - 12:16 AM

Hi Everyone
Hopng someone can help me as i'm going mad! i have exactly the same PIRas the original poster, purchased from coolcomponents in the uk. i have wired it all up, being careful to use the brwon wire for ground and black for alarm (D0), red to 5v.

i have tried two different examples one from this post and the other from Dave vanderwekke in this post (changing it to use a pull up resistor), and in both cases i get nothing, the event is never fired. I just dont get it, the code is really simple, i even purchased a second PIR because i was so convinced my code is correct so it must be the PIR...but still no joy. I hate posting when this question has been answered but i am banging my head against the wall!

code for both examples as follows

simple example:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace motionDetector
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        static InterruptPort PIR = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

        public static void Main()
        {
            // write your code here
            Debug.Print("Application Start!");
            Thread.Sleep(3000);
            PIR.OnInterrupt += new NativeEventHandler(motion_OnInterrupt);
            Thread.Sleep(-1);
        }

        static void motion_OnInterrupt(uint portId, uint state, DateTime time)
        {
            Debug.Print("Interrupt fired");

            led.Write(true);

            Thread.Sleep(1000);

            led.Write(false);

            Thread.Sleep(1000);

        }


    }
}

slightly more elegent example
PIRSensor class
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;


namespace MotionDetection2
{
    public delegate void PIRTriggeredEventHandler(bool triggered, DateTime time);

    public class PIRSensor
    {
        private InterruptPort _sensor;

        public event PIRTriggeredEventHandler SensorTriggered;

        /// <summary>
        /// Initializes a new instance of the <see cref="PIRSensor"/> class.
        /// </summary>
        /// <param name="portId">The port id.</param>
        public PIRSensor(Cpu.Pin portId)
        {
            _sensor =
            new InterruptPort(
                    portId,
                    false,
                    Port.ResistorMode.PullUp,
                    Port.InterruptMode.InterruptEdgeLevelLow);

            _sensor.OnInterrupt +=
            new NativeEventHandler(
                    (data1, data2, time) =>
                    {
                        OnSensorTriggered(data1, data2, time);
                    }
            );

        }

        /// <summary>
        /// Called when [PIR triggered].
        /// </summary>
        /// <param name="data1">The data1.</param>
        /// <param name="data2">The data2.</param>
        /// <param name="time">The time.</param>
        protected void OnSensorTriggered(uint data1, uint data2, DateTime time)
        {
            var evt = SensorTriggered;
            if (evt != null)
                evt.Invoke(data2 == 1, time);
        }
    }
}

and then the program.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace MotionDetection2
{
    public class Program
    {
        public static void Main()
        {
            // write your code here

            PIRSensor pir = new PIRSensor(Pins.GPIO_PIN_D0);
             Thread.Sleep(2000); //Warmup Time
        
            pir.SensorTriggered += new PIRTriggeredEventHandler(pir_TriggeredEvent);

            Thread.Sleep(-1);
        }

        static void pir_TriggeredEvent(bool triggered, DateTime time)
        {

            Debug.Print("PIR: Triggered");

        }
    }
}
any help would be very very much appreciated!

thanks
jamie

#15 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 11 March 2013 - 01:56 AM

Doh!!! - I assumed the black was the ground wire but you are right! It now works.. Thanks alot for your info. Excellent stuff. I have also cleaned up the code and removed references to EnableInterrupt() and ClearInterrupt()

 

 

Hi Waxie,

 

You mind pasting your codes here?

 

I am actually wondering how do you test your device, is there anything around your sensor device? It is because when i try to test, i placed the device in a box and still the outcome stated 'Motion detected'.

 

 And,when u test the program does the outcome stated Motion Detected even though we did not hover above the device? 



#16 GillesLondon

GillesLondon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 June 2013 - 06:29 PM

Hopng someone can help me as i'm going mad! i have exactly the same PIRas the original poster, purchased from coolcomponents in the uk. i have wired it all up, being careful to use the brwon wire for ground and black for alarm (D0), red to 5v.

 

 

Exactly the same problem, I am also using the sensor from coolcomponents.

I have a Netduino Plus 2 with the firmware 4.3. Maybe there is an issue with this 4.3 beta Firmware, can you confirm what is your firmware?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.