DEV Community

Dave
Dave

Posted on

Further Thoughts on Raspberry Pi GPIO as Controller

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:

INPUT 6

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

OUTPUT 4 ON

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

OUTPUT 4

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
NOT 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
OUTPUT 6 ON
OR 6 2
SLEEP 10
OUTPUT 3 OFF

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.

Top comments (4)

Collapse
 
chrisbeardy profile image
chrisbeardy

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.

Collapse
 
tardisgallifrey profile image
Dave

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.

Collapse
 
chrisbeardy profile image
chrisbeardy

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:
Piextend
Revolution pi
Unipi
Controllino

Collapse
 
cosmic profile image
JC5

For PLC type programming of arduino you could also check out soapboxautomation.com/support-2/so... that I stumbled on while researching Twincat...