In this project you're going to build a motion activated RGB LED using a PIR Parallax Motion sensor. You can set the RGB LED to a default state and change its color or animation (like blinking or pulsing) when the motion sensor is triggered.
A PIR Sensor detects motion by measuring changes in the infrared (heat) levels emitted by surrounding objects. When motion is detected, the module outputs a high signal on its output pin. The drivers to use this sensor on a Netduino int in a single NuGet package, so you'll have to add it your project to have access to a simple API to just create a object and start using it right out the box.
Netduino.Foundation is a platform for quickly and easily build connected things using the.NET MicroFramework on Netduino. Created by Wilderness Labs, it's completely open source and maintained by the Netduino community.
If you're new in Netduino development, I suggest you go to the Getting started with Netduino project to properly set up your development environment.
Step 1 - Assemble the circuitFor this project, wire up your breadboard and Netduino as shown in the Fritzing diagram:
Create a Netduino project in Visual Studio 2015 for Windows or the latest Visual Studio for Mac; name the project NightLight.
Step 3 - Add the Netduino.Foundation NuGet PackageWindows
Right-click on your NightLight project and click Manage Nuget Packages. In the Browse tab, search for Netduino.Foundation; it should be the first search result. Click the Install button.
Now search for Netduino.Foundation.ParallaxPIR and add it to your project.
macOS
Alt-click on your NightLight project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for the Netduino.Foundation package and click Add Package to add it to your project.
Now search for Netduino.Foundation.ParallaxPIR and add it to your project.
Step 4 - Write the code for the NightLight ProjectAdd App Class
For this project, we implement a common App software pattern that manages all the peripherals and main logic.
Add a new App class to your project, and paste the following code to draw Circles, Rectangles and lines:
using Netduino.Foundation.LEDs;
using Netduino.Foundation.Sensors.Motion;
using N = SecretLabs.NETMF.Hardware.Netduino;
namespace NightLight
{
public class App
{
protected RgbPwmLed rgbLed;
protected ParallaxPIR parallaxPIR;
public App()
{
InitializePeripherals();
}
private void InitializePeripherals()
{
rgbLed = new RgbPwmLed
(
N.PWMChannels.PWM_PIN_D11,
N.PWMChannels.PWM_PIN_D10,
N.PWMChannels.PWM_PIN_D9,
2.1f,
3.0f,
3.0f,
false
);
rgbLed.SetColor(Netduino.Foundation.Color.Green);
parallaxPIR = new ParallaxPIR(N.Pins.GPIO_PIN_D8) ;
parallaxPIR.OnMotionStart += ParallaxPIRMotionStart;
parallaxPIR.OnMotionEnd += ParallaxPIRMotionEnd;
}
private void ParallaxPIRMotionStart(object sender)
{
rgbLed.StartPulse(Netduino.Foundation.Color.Red);
}
private void ParallaxPIRMotionEnd(object sender)
{
rgbLed.SetColor(Netduino.Foundation.Color.Green);
}
public void Run()
{
}
}
}
In the InitializePeripherals method, we first create a RgbPwmLed object (var rgbLed) and we immediately set it light up with a green hue. After that, instantiate a ParallaxPir object and we register two self-explanatory events: OnMotionStart and OnMotionEnd.
- ParallaxPIRMotionStart event handler will make the RGB LED to start pulsing with a Red hue.
- ParallaxPIRMotionEnd event handler will make the RGB LED to go back to its initial state (LED back to green).
Program Class
Finally, create a new App class object and invoke the Run method. Your code should look like this:
using System.Threading;
namespace NightLight
{
public class Program
{
public static void Main()
{
App app = new App();
app.Run();
Thread.Sleep(Timeout.Infinite);
}
}
}
Step 5 - Run the projectClick the run button in Visual Studio to see the project in action! Leave the circuit alone somewhere for 30 seconds so the sensor calibrates the environment. Now when you swipe your hand near the sensor, or even walking up to it, the OnMotionStart event should be triggered. It should look like to the following gif:
Feel free to play around with more colors and animations for the RGB LED. If the motion sensor is not behaving how you're expecting, you can try changing the sensitivity, time delay potentiometers and trigger methods to adjust it just how you want it. You can read more about that here.
Check out Netduino.Foundation!This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Netduino.Foundation.
- It comes with a Huge Peripheral Driver Library with drivers for the most common sensors and peripherals available in the market.
- All the peripheral drivers are simplified with built-in functionality, exposed by a clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and always excited to help new-comers and discuss new projects.
Comments