Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria
Basics about analog inputs of Raspberry PLC
Learning how to work with the analog inputs of the Raspberry Pi PLC
On this post, we are going to explain how to do the basics in order to work with the analog inputs of Industrial Shields Raspberry Pi programmable logic 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.
We recommend you to read the following posts to understand the programming of this blog. We have used the following blog posts for this example:
To work with analog outputs, you will need any of our industrial controllers for industrial automation:
On all Industrial Shields PLCs, analog inputs can work at:
0 Vdc - 10 Vdc input
Each of them has a particular drawing in the case of the PLC:
0 - 10 Vdc Analog Input
Not all the inputs must be connected in the same way. While non-isolated inputs must be referenced to the same ground as the PLC, isolated inputs can be connected to input grounds, allowing the PLC systems to be isolated. Anyway, the optoisolated input can be connected to the PLC ground aswell.
The following pictures show how to connect the different inputs to the PLC for industrial automation:
5 - 24 Vdc Input
How to work with Bash Scripts
> cd /home/pi/test/analog
> cd /home/pi/test/relay
The get-digital-input script will show the value of the selected input pin. Only the pin we are going to work with will be provided. The return value will be in the range of 0 to 4096 (10 Vdc). In order to call the function, we will do the following:
> ./get-analog-input <input>
Example for the I0.0 input returning a True value:
> ./get-analog-input I0.12
How to work with Python
The bash commands are the base for working easily with the industrial 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 work with the Nano editor included by default and Python3.
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.
In this example you will are going to read the input given of the pin I0.12 of the Raspberry Pi PLC. In order to do it, you will implement a loop that will be constantly reading the input value.
import subprocess import time def str2dec(string): return (string[0:-1]) def adc(value): return (10*int(str2dec(value)))/4096 if __name__ == "__main__": print("Start") while True: try: x = subprocess.run(["./get-analog-input","I0.12"], stdout=subprocess.PIPE, text=True): print(adc(x.stdout)) time.sleep(1) except KeyboardInterrupt: print("\nExit") break
In order to execute the Python program, you will call it as the follow:
> python3 analog_inputs.py
To exit the program, just press ^C.