Introduction
In this post, you will learn how to do the basics to work with analog outputs of Industrial Shields Raspberry Pi programmable logic controllers. By reading this post, you will be able to understand how to connect and configure the analog outputs 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 make this example:
Requirements
In order to work with digital outputs, you will need any of our industrial controllers for industrial automation:
Configuring the switches
Most of the digital outputs are always connected to the internal Raspberry, but in some cases, users can choose between a special peripheral configuration or a GPIO by changing the position of the Dip Switches.
Each switch can select only one configuration. For example, in this case, you can see the GPIOs configuration of a Raspberry Pi based PLC 21+. If you put the switch to the right position (ON) in the lower one, the output Q0.0 will be activated and you will be able to work as digital. If the switch is in the left position (OFF), you will activate the output as analog. Notice that each switch has two different configurations: you must select the right (ON) or the left (OFF) option.
A0.0 Disabled - Q0.0 Enabled
A0.0 Enabled - Q0.0 Disabled         Â
Hardware
The image below shows how to connect a digital output to the PLC:
Software
How to work with Bash Scripts
Raspberry Pi PLC has default bash scripts for working 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 set function will initialize the pin. You will provide the pin with which you are going to work and the value that will be set. For the analogical option, the value will work in a range from 0 to 4095, this being the maximum possible value (10 Vdc).
By default, if no value option is provided, it will be initialized as 50% for the Analog outputs. If any other options are chosen, an error code will warn you. In order to call the function, you should do the following:
> ./set-digital-output <output> <value>
Analogic pins can both work as digital or analog. In this case, if you have used these pins before in either digital or analogic and you want to switch their mode, you must call the set function, providing a stop to the value option; otherwise, there will be a system error. If a reboot is done, it is not necessary to do it.
The pins which can operate with both Analog/Digital configurations are:
Q0.5 | Q1.5 | Q2.5 |
Q0.6 | Q1.6 | Q2.6 |
Q0.7 | Q1.7 | Q2.7 |
Example:
> ./set-analog-output A0.5 2048
> ./set-analog-output A0.5 4096
> ./set-analog-output A0.5 0
> ./set-analog-output A0.5 stop
> ./set-digital-output Q0.5 1
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.
> nano analog_outputs.py
Python allows you to execute a shell command that is stored in a string using the os.system() function. In order to work with it, you will have to import its library at the start of the file. Also, you will include the time library for summoning a 2 seconds delay.
import os
import time
In this example program, you will be changing the values of the A0.5 output of the Raspberry Pi industrial PLC. In order to do it, you will implement a loop that will increase every 2 seconds the output value by 25% and reset it after reaching 100%.
import os
import time
os.system("echo Start")
while True:
try:
os.system("sudo ./set-analog-output A0.5 0")
                time.sleep(2)
os.system("sudo ./set-analog-output A0.5 1024")
                time.sleep(2)
os.system("sudo ./set-analog-output A0.5 2048")
                time.sleep(2)
os.system("sudo ./set-analog-output A0.5 3072")
                time.sleep(2)
os.system("sudo ./set-analog-output A0.5 4095")
                time.sleep(2)
except KeyboardInterrupt:
os.system("sudo ./set-analog-output A0.5 0")
os.system("echo End")
break
 To run the Python program, you will call it as the following:
> python3 analog_outputs.py
To exit, the program just presses ^C.
Basics about Raspberry Pi PLC analog outputs