Beginner needs Help with Netduino & PIR Motion Detection!
#1
Posted 28 August 2011 - 08:36 AM
#2
Posted 28 August 2011 - 10:25 AM
#3
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
Posted 28 August 2011 - 10:54 AM
I have just added the code:
port.EnableInterrupt();
after I setup the Interupt event handler but it still doesnt seem to be working!
#5
Posted 28 August 2011 - 11:10 AM
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.port.EnableInterrupt();
#6
Posted 28 August 2011 - 11:25 AM
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.
static void port_OnInterrupt(uint data1, uint data2, DateTime time) { bool val = port.Read(); ... }
#7
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
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
Posted 28 August 2011 - 04:47 PM
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
Posted 28 August 2011 - 06:20 PM
I missed it in the first post, but IMHO your wiring is not correct: brown is GND and black is Alarm....I have not got a clue what I am doing wrong.
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
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
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
Posted 29 September 2012 - 05:46 AM
#14
Posted 20 December 2012 - 12:16 AM
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
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
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