First lesson for the day: Don’t fry your Pi.
So, 16 mA at 3.3-volts DC is the generally accepted, industry-standard safe power dissipation of an output pin on the Raspberry Pi. You also shouldn’t pull more than 40 mA from all outputs simultaneously. That’s just a couple of regular LEDs, with appropriate current limiting resistors. Go over the limit and you your Pi may end up as a toasted breadboard.
Fortunately, managing larger loads is easy using a ULN2803 8-channel Darlington driver integrated circuit. Hook a Pi output to an input on one of these babies and you can turn 500 mA, 50-volt loads on and off without a sweat. There are 8 input/output pairs on the chip. You can even run them in parallel for higher single-output current requirements. That could be some healthy LEDs.
What about a larger, higher current DC motor? Or an AC motor? Handling these loads is getting into industrial-control territory.
The newly released OPTO 22 digital I/O system for the Raspberry Pi might be just what you need for that upcoming home-factory automation project, you have planned for the garage.
What’s an OPTO 22?
While wandering around the recent Embedded Linux Conference in Portland, I ran into the guys at the OPTO 22 booth.
The company has been making industrial control systems for the last 40 years. They long-ago introduced software to replace the traditional ladder-logic of past PLCs (programmable logic controllers). It was also the first company to provide both wired and wireless networking on their control systems. The company provided some of the systems used by James Cameron, on his historic journey to the bottom of the Mariana Trench.
While the OPTO 22 control modules and boards have interfaced with PCs since the early 80’s. Recently, it introduced an industrial control board that works with the Raspberry Pi.
The company offers a starter kit including the interface board, an input/output module mounting rack and an assortment of 4 input and output modules for $129. There are four extra spaces for additional control modules. You have to provide your own Raspberry Pi and 5-volt power supply.
I never could get the hang of ladder-logic, which is a commonly used way to program industrial control systems. I briefly fiddled with it on a chemical plating line, back when I was a manufacturing engineer for a huge defense contractor. I thought the language was clunky and hard to use. Traditional general-purpose programming languages like C and Python suite my style of thinking much more so than ladder logic.
I found it very interesting that OPTO 22 chose to combine industrial controls with a nano-Linux single board computer like the Raspberry Pi. Another company, Inductive Automation, seems to think there is a bright future, as well, for Linux on industrial control systems. One of their products, Ignition runs on Linux machines.
Of course, OPTO 22 walks you through building a Node-Red graphical platform for programming the Pi, that you might want to explore.
For my use, Python fits the bill nicely and comes standard on the Pi. I cobbled the code together on the command line using the vi editor, remotely (over ssh) from my Linux notebook.
The code was fairly straightforward.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) # use pin numbering
input_state = GPIO.input(40)
if input_state == True:
print('No voltage On Pin')
print('Voltage On Pin')
I executed the code with following command line. Note that you have to use ‘sudo’ because a normal user can’t set the Pi’s pins directly due to permissions, using Python.
pi% sudo python plc1.py
After importing the necessary libraries, we spell out which are inputs and outputs using GPIO.setup. Notice that I used the GPIO.BOARD number system for the pins. You can use either the BOARD or BCM scheme in your code. There’s a handy terminal layout related to the Raspberry Pi pins on OPTO 22’s website.
After setup, we just enter a loop and blink the LEDs on a couple of relay modules (pins 38 and 35) one second on and one second off. The input_state reads the input from the white module (input – 10 to 32 volts DC), every time through the loop. The module could sense a 9-volt source that I used for testing. When there was no connection between the plus side of the battery pack and terminal 1 on the input/output board, “No Voltage On The Pin” printed to my screen after each loop.
Touching the plus side to the terminal gave me a read out of “Voltage On The Pin,” in my terminal. It also turned on the LED on the module. You could just as easily send an MQTT message to a server, log a value in a file or change a readout on a web page. OPTO offers lots of integration possibilities with Linux and the Raspberry Pi, as the computing platform.
Keep in mind that input sensing time increases for each line of code within a control loop. We are using a multi-user, multi-tasking Linux machine, after all. Critical timing issues between processes can get away from you, without careful code design. Fortunately, a half second here or there probably won’t matter too much in a typical industrial control setting. Just wanted you to be aware.
One screwy thing was that the Raspberry Pi read the value of true when there WASN’T any voltage on the input module pin and false when there WAS a voltage. I remember a similar anomaly when reading voltages in past projects with the Pi’s input pins. I don’t know if there is an inverting function built into the Python library or something else going on. I’ll have to do some research. In any event, I simply used true to mean no voltage present.
As you can see in the graphic, the black relay module (pin 35) was hooked up to an 110-volt, 60-watt regular light bulb, which turned on for one second, then off for one second. The relay module was wired in series with the light. Sixty watts is about a half amp, so it’s well within the 3-amp limit for the module. Worked great. For very large loads, like a 30 HP motor, you’d use an industrial contactor, to switch the power on and off. Of course, you’d drive the contactor, using the black 110-volt, 3-amp relay module.
Putting Linux to work in industrial control systems, seems like a no-brainer when you think about it. Sure, the industry will have to refine a bit the shielding, isolation and hardening of single-board Linux computers for the harsh industrial environment. It will happen, I think. Also, as companies transition to Linux, there will be a need for people who can adapt from the old ladder-logic days to modern programming environments like Node-Red, Python and others.
Think of the physical computing stack and just ramp it up to industrial strength.
The New Stack is a wholly owned subsidiary of Insight Partners. TNS owner Insight Partners is an investor in the following companies: Bit.