mort8088 Just another WordPress site

Raspberry Pi – GPIO & C# (2/9) – LED On & Off

In this tutorial we're going to wire up two LEDs and turn them on and off.  This is a headed application also be aware that the GPIO APIs are only available on Windows 10 IoT Core, so this tutorial cannot run on your desktop.

What's in the pack

youtube-workshop-kit-640x480You'll be using the components of the Raspberry Pi YouTube Workshop Kit  in the pack is the following

  • 1 x Transparent Half-Size Breadboard
  • 15 x Male to Female Jumper Wires
  • 5 x Male to Male Jumper Wires
  • 1 x Ultra Bright 5mm Red LED
  • 1 x Ultra Bright 5mm Blue LED
  • 2 x 330Ω Resistor
  • 2 x 4.7kΩ Resistor
  • 1 x 1µF Capacitor
  • 1 x PCB Mount Push Button
  • 1 x PCB Mount Buzzer
  • 1 x DS18B20+ Programmable Resolution 1-Wire Digital Thermometer
  • 1 x Light Dependent Resistor
  • 1 x HC-SR501 PIR Motion Sensor

Wiring the circuit

breadboard connectedBreadboard

Start with the breadboard you'll see that there are rows and columns of holes these are where you plug in the wires  and components to connect them together, the holes are joined inside the board by metal strips so you can plug in one thing per hole and have it connect to other things. the holes are joined together as shown in the picture.

You might also notice on the board that there are numbers and letters around the edges to give you a way to co-ordinate the holes, I'll be using these co-ordinates so you know where to plugin the components.


First off we'll add the LEDs, LED is an acronym for Light Emitting Diode  you'll notice on the LED that one leg is longer than the other. This is because power can only flow one way through the component the long leg is the Positive(+) side so make sure you get it the right way round.

Add the first LED to the board with the long leg in hole F4 and the short in F5


Add the second LED with its long leg in hole F1 and the short in F2



Resistors restrict or limit the flow of current in a circuit. The ability of a material or component to resist current flow is measured in ohms. There are three main types of resistor:

  • fixed resistors
  • variable resistors
  • special resistors, such as thermistors and light-dependent resistors (LDRs)

You'll be using  fixed resistors in this tutorial, these are the most common type of resistor.  Resistors can be used to protect other components (such as an LED) from being damaged by too much current.

Find the two 330 ohms resistors in your pack


Resistors aren't flow specific like the LEDs so it doesn't matter which way round they go. Insert one of the legs of one resisters  in hole G5 and the other end in the blue rail line nearest.


Now add the second resistor from G2 to the blue rail


RP2_PinoutConnect to the Pi

To connect to the Pi I used a black wire to connect pin 6 to the blue rail at the bottom of the board. This is the ground connection of the circuit.


And a blue wire from Pin 13 (GPIO 27) to the blue LEDs long leg in column 1


And  a yellow wire from Pin 15 (GPIO 22) to the red LEDs long leg in column 4


The Code

New Project

For how to create a new project see Part 1 - Hello World. Once you get to "Add content to MainPage.xaml" come back here.

Add WPF bits

  1. From Solution Explorer, double click the ‘MainPage.xaml’ file.
  2. Locate the <Grid> tag in the XAML section of the designer, and add the following mark-up.
    • MainPage.xaml:

Add code behind

  1. From Solution Explorer, open the ‘MainPage.xaml.cs’ file, (Select ‘MainPage.xaml’ and press F7).
  2. At the top of the file add a line to indicate we want to use the GPIO class library.
    • MainPage.xaml.cs
  3. In the MainPage class you'll need a few variables to store some information, add the following block.
    • MainPage.xaml.cs
    • LED1_PIN & LED2_PIN are constants that you'll use the reference the required GPIO pins on the Pi
    • _pin1 & _pin2 will hold the state of those pins
    • and the three SolidColorBrushs are used to change the draw colour of the Ellipse object
  4. Next we need to Initialize the GPIO pins, so add the following method to the class
    • MainPage.xaml.cs
  5. Add a call to the new init method so that it is called when the application starts
    • MainPage.xaml.cs
  6. Go back to the MainPage.xaml tab.
  7. Double click on the "LED - 1" Button in the design surface.
    Visual Studio will add a Click property to the Button XAML tag and generate the "butLED1_Click" method in ‘MainPage.xaml.cs’.

    • MainPage.xaml:
  8. Add the following code to this method.
    • MainPage.xaml.cs:
  9. Go back to the MainPage.xaml tab.
  10. Double click on the "LED - 2" Button in the design surface.
    Visual Studio will add a Click property to the Button XAML tag and generate the "butLED2_Click" method in ‘MainPage.xaml.cs’.

    • MainPage.xaml:
  11. Add a the following code to this method.
    • MainPage.xaml.cs:

Deploy the app to your Raspberry Pi

Set the Solution Platform to ARM and connect to the Remote Machine and press F5 to deploy to your Pi if you've followed the steps then you'll see a screen with two buttons on it when you click them the LEDs will light up click them again and they'll switch off.


In the photos of my board I use a Sintron T-Cobbler Plus for the Raspberry Pi B+ connected to a second breadboard because it looks neater. There is no practical difference to using it or connecting directly to the Pi's GPIO header.

Raspberry Pi - GPIO & C# Tutorial Index

  • Introduction
  • 1 - Hello World
  • 2 - LED On & Off
  • 3 - LED Blink (Coming Soon)
  • 4 - Push Button (Coming Soon)
  • 5 - User Inputs (Coming Soon)
  • 6 - SOS Buzzer (Coming Soon)
  • 7 - Temperature Sensor (Coming Soon)
  • 8 - Light Dependent Resistor (Coming Soon)
  • 9 - Passive Infrared Sensor (Coming Soon)
All C# Code is available for Download Here

I am in no way affiliated with or sponsored by or RaspberryPiIVBeginners