Basics about digital inputs of a Raspberry PLC

Learning how to work with the digital inputs of the Raspberry Pi PLC
February 3, 2021 by
Basics about digital inputs of a Raspberry PLC
Boot & Work Corp. S.L., Marti Guillem Cura

Introduction

In this post, we are going to explain how to do the basics to work with the digital inputs of Industrial Shields Raspberry Pi PLC controllers. By reading this post, you will be able to understand how to connect, configure and work with the inputs of your industrial Raspberry Pi PLC controller.


Previous readings

We recommend you read the following posts in order to understand the program of this blog. We used the following blog posts to do this example:


Requirements

In order to work with digital outputs you will need any of our industrial Raspberry Pi based PLC controllers for industrial automation:


Input types

There are two different types of inputs in the RAspberry Pi industrial PLC devices:

  • 5 Vdc - 24 Vdc input

  • 5 Vdc - 24 Vdc optoisolated input

Each one has a particular draw in the case of the PLC:

 Input types

5 - 24 Vdc Optoisolated Input

 Input types

5 - 24 Vdc Input

Hardware

Not all the inputs must be connected in the same way. While the non-isolated inputs must be referenced to the same ground as the PLC, the isolated inputs can be connected to the input grounds, allowing to isolate systems from the PLC. Anyway, the optoisolated input can be connected to the PLC ground as well.

The following images show how to connect the different inputs to the PLC for industrial automation:

5 - 24 Vdc Optoisolated Input

5 - 24 Vdc Optoisolated Input

5 - 24 Vdc Input

5 - 24 Vdc Input

Software

How to work with Bash Scripts

Raspberry Pi PLC has default bash scripts to work with the inputs. All the inputs and outputs scripts must be executed from the correct path. It depends on the shield type of the I/O executed. In function of the shield of the I/O that you need to activate, you must execute the scripts from a specific path:
  • Analog/Digital Shields

> cd /home/pi/test/analog
  • Relay Shield

> cd /home/pi/test/relay

The get-digital-input script will show the value of the selected input pin. It will only be provided the pin with which we are going to work. In order to call the function, we will do the following:

> ./get-digital-input <input>

Example for the I0.0 input returning a True value:

> ./get-digital-input I0.0
1

How to work with Python

The bash commands are the basis to work easily with the Raspberry PLC. In order to work with python files, if you want to interact with the IOs of the PLC, you will have to call these scripts.

To edit the files you will be working with the Nano editor included by default and Python3.

nano digital_inputs.py

Python allows you to execute a shell command that is stored in a string using the subprocess library. In order to work with it, you will have to import it at the start of the file.

import subprocess

In this example, you will be reading the input given of the pin I0.0 of the Raspberry Pi PLC. In order to do it, you will implement a loop that will be constantly reading the input value. If it detects voltage, it will print a True value.

import subprocess
import time
print("Start")
while True:
try:
x = subprocess.run(["./get-digital-input","I0.0"], stdout=subprocess.PIPE, text=True)
if '1' in x.stdout:
print(True)
time.sleep(1)
else:
print(False)
time.sleep(1)
except KeyboardInterrupt:
print("\nExit")
break

 In order to execute the Python program, you will call it as follows:

> python3 digital_inputs.py

To exit the program, just press ^C.

Questions about our products? Can you share your knowledge with other users?

Visit our forum. Any comments, topics or questions are welcome!


​Search in our Blog

Basics about digital inputs of a Raspberry PLC
Boot & Work Corp. S.L., Marti Guillem Cura February 3, 2021

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>