This project shows how to fade a LED with the raspberry pi. We cover the hardware and software setup in which we connect the Grove Pi to a rotary angle sensor (a fancy name for a potentiometer). The Raspberry Pi reads the analog value and dims or fades the PWM to the LED which sets the LED’s intensity. The LED is dimmed using the potentiometer.
- Raspberry Pi running Windows IoT
- GrovePi Starter Kit
Setting Up The Hardware To Fade an LED with the Raspberry Pi
Simply plug the LED Module in Port D5 and the Rotary encoder in Port A0. That’s it.
- Download the GrovePi Windows IoT Drivers and open GrovePi.sln.
- Replace the code found in "SimpleDriver.CS" with the code listed below
- Deploy to your device. (Refer to this link If you have not previously setup visual studio to deploy to your Raspberry Pi)
SampleDriver.cs :
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using GrovePi;
using GrovePi.Sensors;
namespace Driver
{
public sealed class SimpleDriver : IBackgroundTask
{
private readonly IBuildGroveDevices _deviceFactory = DeviceFactory.Build;
public void Run(IBackgroundTaskInstance taskInstance)
{
var led = _deviceFactory.Led(Pin.DigitalPin5);
var rotaryAngleSensor = _deviceFactory.RotaryAngleSensor(Pin.AnalogPin2);
var maxValue = 255;
while (true)
{
try
{
var sensorValue = rotaryAngleSensor.SensorValue();
led.AnalogWrite((byte)(sensorValue > maxValue ? maxValue : sensorValue));
Task.Delay(500).Wait();
}
catch (Exception)
{
throw;
}
}
}
}
}
Comments