-
Notifications
You must be signed in to change notification settings - Fork 22
Using LibGpio
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
- Create a folder in your project called "ReferencedAssemblies"
- From the Pi# download, locate "PiSharp.LibGpio.dll"
- Add this to the "ReferencedAssemblies" folder, using Visual Studio
- Right click on the project and click "Add Reference"
- Click browse and select "PiSharp.LibGpio.dll"
- Import the namespaces "PiSharp.LibGpio" and "PiSharp.LibGpio.Entities"
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;
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);
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);
The value may be read from input pins using the LibGpio.Gpio.ReadValue() method. For example:
var value = LibGpio.Gpio.ReadValue(BroadcomPinNumber.Eighteen);