In this sample, we will connect a Tri-color LED to Raspberry Pi 2. The LED will blink changing colors from Red, Blue, Green.
In this sample, we will connect a Tri-color LED to Raspberry Pi 2. The LED will blink changing colors from Red, Blue, Green.
This is a headed sample, so please ensure that your device is in headed mode by running this command:Ā setbootoption.exe headed
Ā (changing the headed/headless state will require a reboot).
Also, be aware that the GPIO APIs are only available on Windows IoT Core, so this sample cannot run on your desktop.
Components
You will need the following components :
- a Raspberry Pi 2
- aĀ 754-1615-ND Tri-color LED
- aĀ 330 Ī© resistor
- 2xĀ 100 Ī© resistors
- a breadboard and several male-to-female and male-to-male connector wires
Connect to your Device
Letās start by wiring up the components on the breadboard as shown in the diagram below.
Here is the schematic:
The pinout of the Tri-color LED is shown below and can be found in theĀ datasheet.
Connecting the LED
- Insert the Tri-color LED into the breadboard as shown in the breadboard diagram at the top of the page.
- Connect one end of the 330 Ī© resistor to the red lead of the Tri-color LED.
- Connect the other end of the 330 Ī© resistor to Pin 29 GPIO5 of the Raspberry Pi 2.
- Connect one end of a 100 Ī© resistor to the blue lead of the Tri-color LED.
- Connect the other end of the 100 Ī© resistor to Pin 31 GPIO6 of the Raspberry Pi 2.
- Connect one end of a 100 Ī© resistor to the green lead of the Tri-color LED.
- Connect the other end of the 100 Ī© resistor to Pin 33 GPIO13 of the Raspberry Pi 2.
- Connect the cathode (the longest leg) of the Tri-color LED anode (the longer leg) of the LED to Pin 6 GND of the Raspberry Pi 2.
Here is the pinout of the RPi2:
Deploy your app
You can find the entire code for this sampleĀ here. This sample is written in C#. Make a copy of the folder on your disk and open the project from Visual Studio.
Make sure you set the āRemote Debuggingā setting to point to your Windows IoT device. Refer to the basic āHello WorldāĀ sampleĀ if you need guidance. If youāre building for Raspberry Pi 2, selectĀ ARM
.
When everything is set up, you should be able to press F5 from Visual Studio. The RGBLED app will deploy and start on the Windows IoT device, and you should see the LED blink in sync with the simulation on the screen.
Letās look at the code
The code for this sample is pretty simple. We use a timer, and each time the āTickā event is called, we flip the state of the LED.
Timer code
Here is how you set up the timer in C#:
public MainPage()
{
// ...
this.timer = new DispatcherTimer();
this.timer.Interval = TimeSpan.FromMilliseconds(500);
this.timer.Tick += Timer_Tick;
this.timer.Start();
// ...
}
private void Timer_Tick(object sender, object e)
{
FlipLED();
}
Initialize the GPIO pin
To drive the GPIO pin, first we need to initialize it. Here is the C# code (notice how we leverage the new WinRT classes in the Windows.Devices.Gpio namespace):
using Windows.Devices.Gpio;
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
// Show an error if there is no GPIO controller
if (gpio == null)
{
redpin = null;
bluepin = null;
greenpin = null;
GpioStatus.Text = "There is no GPIO controller on this device.";
return;
}
redpin = gpio.OpenPin(REDLED_PIN);
bluepin = gpio.OpenPin(BLUELED_PIN);
greenpin = gpio.OpenPin(GREENLED_PIN);
// Show an error if the pin wasn't initialized properly
if (redpin == null || bluepin == null || greenpin == null)
{
GpioStatus.Text = "There were problems initializing the GPIO red/blue/green pin.";
return;
}
redpin.Write(GpioPinValue.High);
redpin.SetDriveMode(GpioPinDriveMode.Output);
bluepin.Write(GpioPinValue.High);
bluepin.SetDriveMode(GpioPinDriveMode.Output);
greenpin.Write(GpioPinValue.High);
greenpin.SetDriveMode(GpioPinDriveMode.Output);
GpioStatus.Text = "GPIO blue/red/green pin initialized correctly.";
}
Letās break this down a little:
- First, we useĀ
GpioController.GetDefault()
Ā to get the GPIO controller. - If the device does not have a GPIO controller, this function will returnĀ
null
. - Then we attempt to open the pin by callingĀ
GpioController.OpenPin()
Ā with theĀREDLED_PIN
,BLUELED_PIN
Ā andĀGREENLED_PIN
Ā value. - Once we have theĀ
redpin
,Ābluepin
Ā andĀgreenpin
, we set it to be High by default using theĀGpioPin.Write()
Ā function. - We also set the pins to run in output mode using theĀ
GpioPin.SetDriveMode()
Ā function.
Modify the state of the GPIO pin
Once we have access to theĀ GpioOutputPin
Ā instance, itās trivial to change the state of the pin to turn the LED on or off.
To turn the LED on, simply write the valueĀ GpioPinValue.High
Ā to the pin:
private void FlipLED()
{
if (LEDStatus == 0)
{
LEDStatus = 1;
if (redpin != null && bluepin != null && greenpin != null)
{
//turn on red
redpin.Write(GpioPinValue.High);
bluepin.Write(GpioPinValue.Low);
greenpin.Write(GpioPinValue.Low);
}
}
else if (LEDStatus == 1)
{
LEDStatus = 2;
if (redpin != null && bluepin != null && greenpin != null)
{
//turn on blue
redpin.Write(GpioPinValue.Low);
bluepin.Write(GpioPinValue.High);
greenpin.Write(GpioPinValue.Low);
}
}
else
{
LEDStatus = 0;
if (redpin != null && bluepin != null && greenpin != null)
{
//turn on green
redpin.Write(GpioPinValue.Low);
bluepin.Write(GpioPinValue.Low);
greenpin.Write(GpioPinValue.High);
}
}
}
Source: RGB LED Sample