DEV Community


Further Thoughts on Raspberry Pi GPIO as Controller

tardisgallifrey profile image Dave ・3 min read

I wrote a post on a method to organize Raspberry Pi GPIO so that it acts more like a controller than just a single board computer with I/O. To summarize that post, my thoughts were to establish a way to configure I/O pins, run a line-by-line control program using those pins, and repeat the control program ad infinutum. This is more closely related to how I use industrial controllers in the electrical and HVAC field.

Today, I'll flesh that idea out just a bit more (as it came to me on my morning run). It consists of creating discrete, small utility programs that handle interaction with an individual pin then we use those small programs to build larger control programs.

In the first step, my idea is to create two basic programs called something similar to OUTPUT and INPUT. I would build these in C, but you're welcome to use what you have facility using. Calling one of these will either read or write to a single named GPIO pin thus:


should respond with to STDOUT with 1 or 0, dependent upon the pin's condition. Also:


would set pin 4 high (OFF would set it low). Using:


would respond to STDOUT with the pin's current value.

In addition, we could add other small programs to allow for logic conditions between two pins. Such as:

AND 1 3
OR 1 3
XOR 1 3

The results for these would be a TRUE (1) or FALSE (0) to STDOUT.

The pin number and condition (ON or OFF) would be command line arguments to the program. That would make the programs useful for all the digital GPIO pins.

Once these small programs are built and tested, which would be fairly simple to accomplish, then we could use C or Perl (or another program with the same ability) to run these programs from a file. What I'm thinking about specifically is the backtick feature of Perl (ls -l) that runs a system command (like our small programs) and then captures the output of the command in a variable. It would be something like this:

my $value =INPUT 6;

and should result in a 1 or 0 being saved in $value for use elsewhere in the Perl script. The idea then, is to build a Perl script that would read from a file like this:

AND 1 3
OR 6 2

The output lines would take effect if the condition line before them resolved to TRUE, else they would be skipped. There is also room for utility programs such as SLEEP, WATCHDOG, and possibly others I can't think of at the moment. The Perl script would run these inside some type of while() loop so that they are continually repeated. A loop timer could be a command line argument into the script as well.

Since I just thought of this today, I haven't had time yet to start the project. I will have to see if this is a reasonable method or not. There are also other conditional programs that could be added such as those for analog sensors (temperature, humidity, pressure, etc.). While this may seem like it removes a lot of capability, but I don't think so. A Perl script that runs a control program can be built as a systemd service so that it continues to run in the background while other tasks are run such as trending logs of the I/O pins and values and running displays.

Let me know what you think. I'll be coming back to update this as I get time to work on the project.

Discussion (4)

Editor guide
chrisbeardy profile image

You may be looking for something more simple, but did you know you can run many industrial PLC runtimes on raspberry pi, such as Codesys and Axel LogicLab, amongst others.

tardisgallifrey profile image
Dave Author

No, I didn't. I am looking for simple, but I am interested in other tech as well. Any wheel I don't have to reinvent is good. I'm several years out of the PLC industry, so my knowledge base there is behind.

chrisbeardy profile image

Yer, it's pretty good. A codesys runtime can be brought for £50 but but it's easily prograable in any IEC language.

You can even get commercial products based on it too. Which then look more like PLC and have relay modules 24V etc so can slot right in as a cheap PLC. And you still have a pi at the heart to do anything else you want.

Such as:
Revolution pi

cosmic profile image

For PLC type programming of arduino you could also check out that I stumbled on while researching Twincat...