SimpleGPIO 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleGPIO --version 1.0.0                
NuGet\Install-Package SimpleGPIO -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleGPIO" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleGPIO --version 1.0.0                
#r "nuget: SimpleGPIO, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleGPIO as a Cake Addin
#addin nuget:?package=SimpleGPIO&version=1.0.0

// Install SimpleGPIO as a Cake Tool
#tool nuget:?package=SimpleGPIO&version=1.0.0                

SimpleGPIO Build Status

A simple, low-ceremony GPIO library for all your IoT needs

Overview

SimpleGPIO takes a high-level, object-oriented approach to IoT programming, in the same way that high-level programming languages provide features that help abstract what's happening on the metal away from the code.

Installation

Simply add the SimpleGPIO library to your project from NuGet.

Initialization

Instantiate a new board to be able to access its GPIO header:

var pi = new RaspberryPi();

If you're using a dependency injection container, you can register the board as a singleton to be used elsewhere in the application:

services.AddSingleton<RaspberryPi>();

Accessing GPIO pins

GPIO pins can be accessed by both their physical location on the board, and/or their Broadcom identifier GPIO#.

var redLED = pi.Pin16;
var sameRedLED = pi.GPIO23;

Moving electrons

SimpleGPIO provides many ways to turn power on or off, depending on your preferences.

The simplest way is to use the built-in helper methods:

redLED.TurnOn();
redLED.TurnOff();

If you prefer assigning values:

redLED.Power = PowerValue.On;
redLED.Power = PowerValue.Off;

At the lowest level, you can directly set the voltage going out of the pin:

redLED.Voltage = Voltage.High; //on
redLED.Voltage = Voltage.Low;  //off

Power Modes

All of the above examples assume the default Direct power mode, where the positive terminal of the LED is connected to the GPIO pin, and the negative terminal is connected to the ground pin.

If, instead, you want to supply constant power by, e.g. the 3v3 pin, and the have the GPIO pin supply (or not supply) resistance, you can use the Differential power mode, where PowerValue.On == Voltage.Low and PowerValue.Off == Voltage.High:

var yellowLED = pi.Pin18;
yellowLED.PowerMode = PowerMode.Differential;
yellowLED.TurnOn();

Techno Dance Parties

There are some helper methods for toggling values. If power is currently on, toggling it will turn it off; if power is off, toggling will turn it on:

redLED.Toggle();

If you want to repeat the toggle at a given frequency, for a set amount of time, pass in the frequency and a TimeSpan as parameters:

redLED.Toggle(3, TimeSpan.FromSeconds(5));

This will flash the red LED 3 times per second, for 5 seconds.

Alternatively, you can toggle power a set number of times by passing in a number as the second parameter. The following will flash the red LED 3 times over 1.5 seconds:

redLED.Toggle(2, 3);

What about inputs?

Input components such as buttons can be declared the same way as output components, and the Power and Voltage can be read from the new variable:

var button = pi.Pin11;
var isPressed = button.Power == PowerValue.On;

The Direct Power Mode for an input component expects power from e.g. the 3v3 pin, so that electricity flows through to the GPIO pin when the button is depressed.

Reacting to Change

Three methods are provided on a pin that accept an Action as a parameter, so that when that pin's state changes, some subsequent steps can be performed:

var button = pi.Pin11;
var redLED = pi.Pin16;
var buzzer = pi.Pin18;

button.OnPowerOn(() => redLED.TurnOn());
button.OnPowerOff(() => redLED.TurnOff());
redLED.OnPowerChange(() => buzzer.Toggle(1, 1));

Whenever the button is pressed down, the LED will turn on. When the button is released, the LED will turn off. Whenever the LED turns on or off, the buzzer will beep for half a second (reminder why: because Toggle will complete a single cycle at 1Hz, which means 0.5s on, then 0.5s off).

Cleaning up

If you want to turn off everything that was turned on while your application was running, simply Dispose() of your RaspberryPi at the end of your code.

pi.Dispose();

This will turn off and close all open GPIO pins. As with all IDisposables, this also works if you wrap the RaspberryPi you're using in a using(){} block.

How can I help?

First, thank you for your enthusiasm! I'd love feedback on how you felt using this. If you had an awesome experience, let me know on Twitter. If you had any problems, feel free to file an issue.

If you're looking to contribute code, but don't have any ideas of your own, there are some specific things I'd love help with over in the Issues tab!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 429 11/9/2022
2.0.0 331 12/30/2021
2.0.0-beta.5 135 12/28/2021
2.0.0-beta.4 132 12/28/2021
2.0.0-beta.3 124 12/23/2021
2.0.0-beta.2 135 12/22/2021
2.0.0-beta.1 188 11/12/2021
1.1.0 1,309 8/19/2018
1.0.0 937 6/28/2018