Writing the Dimmer Code

Create a new project as before and open the Program.cs file in the main code editing window.

Now, type the following:

PWM led = new PWM(Pins.GPIO_PIN_D5);1
AnalogInput pot = new AnalogInput(Pins.GPIO_PIN_A0);2
pot.SetRange(0, 100);
int potValue = 0;3
1

This line of code creates a PWM object using our digital pin D5. This lets you control the intensity of the LED by changing its duty cycle (percentage of time turned on vs. off).

2

The second and third lines of code set up the potentiometer as before, but this time with a range of 0 through 100. This will give you a percentage that you can use to set the LED’s intensity.

3

This line creates a variable named potValue, which you’ll use to store and retrieve the potentiometer’s current position.

Now you need to write some code to change the LED’s intensity based on the value of the potentiometer. Add the following:

while (true) { // read ...

Get Getting Started with Netduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.