Netduino Pulse-Width-Modulation LED Project

About the project

Try Netduino.Foundation, a powerful platform to build connected things quickly and easily with the.NetMicroFramework and Netduino.

Project info

Difficulty: Easy

Platforms: MicrosoftNetduino

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

LED (generic) LED (generic) x 1
Netduino 2 Netduino 2 x 1
Netduino Plus 2 Netduino Plus 2 x 1
Netduino3 Netduino3 x 1
Netduino3 Ethernet Netduino3 Ethernet x 1
Netduino3 WiFi Netduino3 WiFi x 1

Software apps and online services

Microsoft Visual Studio 2015 Microsoft Visual Studio 2015

Story

This project shows you how to control the brightness of a generic LED using Pulse Width Modulation. We'll use the Pulse Width Modulation library included in Netduino.Foundation to make driving an LED quick and easy.

Pulse With Modulation is a way of controlling voltage digitally to emulate an analog signal. Netduino boards generate this signal as a square wave. The two key parameters under the control of the developer are the frequency and duty cycle. You can read more here.

In the above diagram, the time where the signal is high is the same as the time where the signal is low. The percentage of time the signal is on (high) is called the duty cycle. So, in the above, the signal is high 50% of the one cycle and so the duty cycle is 50%.

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.

Assemble the circuit

For this project, simply connect the LED's cathode (short pin) to the GND, and the anode (long pin) to the pin 11 on your Netduino.

PwmLed circuit diagram

Create a Netduino project

Create a Netduino project in Visual Studio 2015 for Windows or the latest Visual Studio for Mac; name the project PwmLed.

Add the Netduino.Foundation NuGet Package

Windows

Right-click on your PwmLed project and click Manage Nuget Packages . In the Browse tab, search for Netduino.Foundationit should be the first search result. Click the Install button.

Adding Netduino.Foundation nuget package

MacOS

Alt-click on your PwmLed 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.

Adding Netduino.Foundation nuget package

Control the LED

You'll use the Pulse Width Modulation library contained in Netduino.Foundation to control the brightness of your LED. Add the Netduino.Foundation namepace: add a using statement  for SecretLabs.NETMF.Hardware.Netduino. The code below uses an Alias named N.

In the Main method, create a new PwmLed object named pwdLed. In the constructor, specify the pin you're using to control the LED (pin 11) and the color of the LED (to select an appropriate voltage).

Using pwmLed, you can now invoke different APIs to light up your LED, such as StartBlinkStartPulse, or SetBrightness. These methods include parameters to control the LED behavior such as duration, high brightness and low brightness.

using System.Threading; using N = SecretLabs.NETMF.Hardware.Netduino; using Netduino.Foundation.LEDs; namespace Blinky { public class Program { public static void Main() { var pwmLed = new PwmLed ( N.PWMChannels.PWM_PIN_D11, TypicalForwardVoltage.Green ); // pulse the LED pwmLed.StartPulse(); // keep the app running Thread.Sleep(Timeout.Infinite); } } }

Run the project

Click the run button in Visual Studio to see your LED pulse! It should be similar to the following gif:

White LED in a Pulse animation with PWM

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.

References

Credits

Photo of Wilderness Labs

Wilderness Labs

Creators of Meadow. Makers of Netduino. We power connected things.

   

Leave your feedback...