Pi# Has moved.

Pi# is now hosted on GitHub, please visit https://github.com/andycb/PiSharp

This page will guide you though using LibGpio, the GPIO library for .NET.

Code examples are in C# but can be converted to Visual Basic.NET easily. VB.NET examples will be added at a later date.

Add LibGpio to the Project

  1. Create a folder in your project called "ReferencedAssemblies"
  2. From the Pi# download, locate "PiSharp.LibGpio.dll"
  3. Add this to the "ReferencedAssemblies" folder, using Visual Studio
  4. Right click on the project and click "Add Reference"
  5. Click browse and select "PiSharp.LibGpio.dll"
  6. Import the namespaces "PiSharp.LibGpio" and "PiSharp.LibGpio.Entities"

Use Test Mode

If you wish to use the Pi# Simulator, you will need to place the Library in test mode, this changes the location that it writes to from '/sys/class/gpio' to '/tmp/RasPiGpioTest' (or 'C:\RasPiGpioTest' on Windows). When in test mode no changes are made to the GPIO pins.

When running under Windows or Mac OSX, test mode to automaticaly assumed.

To use test mode, use the following code before you use any other function.

LibGpio.Gpio.TestMode = true;

Export Pins to Use

Before using a GPIO pin, it must first be "exported", that is you must configure it with a direction so the Raspberry Pi knows if it is an Input or Output pin

To do this, use the LibGpio.Gpio.SetupChannel() method, specifying a pin number (See Understanding Pin Numbers for infomation on pin numbering) and direction for each pin you are going to use. For example:

LibGpio.Gpio.SetupChannel(BroadcomPinNumber.Four, Direction.Output);
LibGpio.Gpio.SetupChannel(BroadcomPinNumber.Eighteen, Direction.Input); 

Writing to Output Pins

To change the value of an output pin use the LibGpio.Gpio.OutputValue() method, specifying the pin number and value to output. The value may either be True (pin is high) or False (pin is low). For example:

LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, true); 

Reading From Input Pins

The value may be read from input pins using the LibGpio.Gpio.ReadValue() method. For example:

var value = LibGpio.Gpio.ReadValue(BroadcomPinNumber.Eighteen); 

Last edited Mar 20, 2015 at 12:18 AM by andycb, version 17