Raspberry PI GPIO Simulator
Download full source code.
When programming the GPIO of a Raspberry Pi, it is not always possible to have a Pi at hand with appropriate wires and connected devices. Last week I was working on my Morse code application for the Pi while on the train. Running it locally on my laptop leads to a runtime error because my laptop doesn’t have a GPIO.
I looked on the web for GPIO simulator and found a few old, not maintained projects which probably won’t work with .NET.
How about creating one with an LLM? As you will guess, this was easy. It took a few back-and-forths and some tweaks, but I now have a GPIO simulator that I can include in my .NET Raspberry Pi projects when testing.
In fact, you can leave the simulator project in place even when deploying. The simulator’s code will be bypassed when running on the Pi.
Here’s how it looks -
It’s available on NuGet - https://www.nuget.org/packages/RaspberryPi.GpioSim/.
It is simple to use. Add the RaspberryPi.GpioSim Nuget package to your project -
dotnet add package RaspberryPi.GpioSim Then add the using RaspberryPi.GpioSim reference to the project. Normally you would do something like line 7, using var pinController...., but instead use lines 9 and 10. Then write the code as normal.
1using System.Device.Gpio;
2using RaspberryPi.GpioSim;
3
4// Real GPIO on a Raspberry Pi, the simulator everywhere else.
5// Override with --simulate or --real; simulator options: --display board|log, --script <file>.
6
7// using var piController = new GpioController(); // use the line below instead
8
9using var hw = GpioHost.Create(args);
10var piController = hw.Controller;
11
12// normal GPIO code from here on
13var gpio24 = piController.OpenPin(24, PinMode.Output);
14
15// ...Here is the repository - https://github.com/bryanjhogan/RaspberryPi-GPIO-Simulator.
Download full source code.