Getting Started with DIN Simul8
This short guide helps you connecting the board to a PLC of the Opta® family and test some basic functionality.
In this tutorial you'll be guided in connecting the Arduino® DIN Simul8 to the Arduino Opta® WiFi, and trigger some basic functions.
Hardware & Software Requirements
Hardware
- Arduino® DIN Simul8
- Arduino Opta® WiFi
- USB-C® cable
- 24 V power supply with barrel plug adapter
- 8x wire for signal
- 2x wire for power distribution
Software
Overview
DIN Simul8 is a digital-input-simulator and power distribution board for the PLC of the Opta family. It provides eight toggle switches (0 - 10 V output) and four screw terminal for bringing the 24 V and the GROUND easily to the PLC or other boards.
If you have any problems using the Opta WiFi you can read its manual before proceeding.
Connections
To connect the DIN Simul8 to the PLC you'll need 8 wires for the signal and two for the power.
Connections are super important in an industrial project, first of all disconnect the power plug and then connect all the pins using cable with lugs, or be careful that no copper part of the cable touch other pins.
- Connect the power pins: +24V and GND
- Connect all the signal pins
Upload Test Code
Firstly we have to test if the components and the connections works as excepted. Let's print on the Serial Monitor what arrives to the inputs.
The Opta WiFi has eight inputs ports, at the top, named from
I1
to I8
. They are mapped to pin A0
to A7
. Let's assign to each of them a variable with the name of the port on the Opta WiFi, easier to remember, we can use the #define function, like this:1#define pin_I1 A0
Opta™ Terminal | Arduino Pin Mapping | Variable |
---|---|---|
| /
|
|
| /
|
|
| /
|
|
| /
|
|
| /
|
|
| /
|
|
| /
|
|
| /
|
|
From now on we can refer to the port
I1
of the Opta WiFi using the pin_I1
variable.To read what's arriving on each input port, we can use the digitalRead function, inside the
Serial.print
command:1Serial.print(digitalRead(pin_I1));
Now let's do this for each input port, dividing each value with a comma
,
.
Here the full code:1#define pin_I1 A02#define pin_I2 A13#define pin_I3 A24#define pin_I4 A35#define pin_I5 A46#define pin_I6 A57#define pin_I7 A68#define pin_I8 A79
10void setup() {11 // put your setup code here, to run once:12
13 Serial.begin(9600);14
15}16
17void loop() {18 // put your main code here, to run repeatedly:19
20 Serial.print(digitalRead(pin_I1));21 Serial.print(",");22 Serial.print(digitalRead(pin_I2));23 Serial.print(",");24 Serial.print(digitalRead(pin_I3));25 Serial.print(",");26 Serial.print(digitalRead(pin_I4));27 Serial.print(",");28 Serial.print(digitalRead(pin_I5));29 Serial.print(",");30 Serial.print(digitalRead(pin_I6));31 Serial.print(",");32 Serial.print(digitalRead(pin_I7));33 Serial.print(",");34 Serial.println(digitalRead(pin_I8));35
36 delay(100);37}
Don't forget to initialize the Serial communication with the Serial.begin command in the setup.
Don't forget to call the the last
asSerial.print
, in order to print a new line and wrap the text to make readable.Serial.println
Don't forget to put some delay, like 100ms in order to have a stable communication over serial.
Each switch in the DIN Simul8 outputs 0 V when OFF and 10 V when ON. The
digitalRead()
function will read 0 for 0 V and 1 for 10 V.
If you switch on the toggle switches 1-3-5-7 and leave off the 2-4-6-8, like in the following image:The output should be like this:
Upload Function Trigger Code
If everything works we can move on and try to trigger a function when switch change state: a function that print
"Switch 1 ON"
when turned on and the inverse for the OFF state.1void ON_function() {2 Serial.println("Switch 1 ON");3}4
5void OFF_function() {6 Serial.println("Switch 1 OFF");7}
In order to make every function runs once, only when the state changed, and not continuously, we can save the status of the switch in a variable:
1bool stateSwitch = false;
and call the function when it reads the opposite state: if the state is OFF (0) and it reads ON (1) it means that is just changed to ON, so call the
ON_function()
and update the stateSwitch
variable.1if (stateSwitch == 0) {2 if (digitalRead(pin_I1) == 1) {3 ON_function();4 stateSwitch = 1;5 }6 }
The final code should like like this and it will works only for the toggle switch 1.
1#define pin_I1 A02#define pin_I2 A13#define pin_I3 A24#define pin_I4 A35#define pin_I5 A46#define pin_I6 A57#define pin_I7 A68#define pin_I8 A79
10
11void setup() {12 // put your setup code here, to run once:13
14 Serial.begin(9600);15}16
17bool stateSwitch = false;18
19void loop() {20 // put your main code here, to run repeatedly:21 22 if (stateSwitch == 0) {23 if (digitalRead(pin_I1) == 1) {24 ON_function();25 stateSwitch = 1;26 }27 } else {28 if (digitalRead(pin_I1) == 0) {29 OFF_function();30 stateSwitch = 0;31 }32 }33
34}35
36void ON_function() {37 Serial.println("Switch 1 ON");38}39
40void OFF_function() {41 Serial.println("Switch 1 OFF");42}
Upload Final Code
In order to make it works for all the eight switches, you can repeat the
stateSwitch
variable and the if-else
conditions eight times... but that would be boring. What about using arrays and a for-loop?The idea is to replace all the variables used in the loop with arrays that stores eight values, one for each switch, and put it inside a
for loop
that will cycle through all the eight inputs. Let's see how to do it:First of all we need an array for all the pins definition of the inputs:
1int pins[] = { pin_I1, pin_I2, pin_I3, pin_I4, pin_I5, pin_I6, pin_I7, pin_I8 };
Then we need one for the
stateSwitch
variable:1bool stateSwitch[] = { false, false, false, false, false, false, false, false };
And then we can substitute them:
1if (stateSwitch[ ] == 0) {2 if (digitalRead(pins[ ]) == 1) {3 ON_function();4 stateSwitch[ ] = 1;5 }6} else {7 if (digitalRead(pins[ ]) == 0) {8 OFF_function();9 stateSwitch[ ] = 0;10 }11}
Now we need to tell to each array-variable which values to use, you can do it by putting a number from 0 to 7 inside the
[]
, i.e. pins[0] = pin_I1 // pins[1] = pin_I2 // etc..
.
One way of doing it is with the for-loop
. We can assign a variable, like int i
, to change from 0 to 7, and then assign it to each array-variable:1for (int i = 0; i <= 7; i++) {2 if (stateSwitch[i] == 0) {3 if (digitalRead(pins[i]) == 1) {4 ON_function();5 stateSwitch[i] = 1;6 }7 } else {8 if (digitalRead(pins[i]) == 0) {9 OFF_function();10 stateSwitch[i] = 0;11 }12 }13}
Ok, that would work, but the
ON_function()
and the OFF_function()
will print only the state of the Switch 1, unless we make the i
variable as an argument, and so we can print the number related to the switch we are switching.Here the full code:
1#define pin_I1 A02#define pin_I2 A13#define pin_I3 A24#define pin_I4 A35#define pin_I5 A46#define pin_I6 A57#define pin_I7 A68#define pin_I8 A79
10int pins[] = { pin_I1, pin_I2, pin_I3, pin_I4, pin_I5, pin_I6, pin_I7, pin_I8 };11
12void setup() {13 // put your setup code here, to run once:14
15 Serial.begin(9600);16}17
18bool stateSwitch[] = { false, false, false, false, false, false, false, false };19
20void loop() {21 // put your main code here, to run repeatedly:22
23 for (int i = 0; i <= 7; i++) {24 if (stateSwitch[i] == 0) {25 if (digitalRead(pins[i]) == 1) {26 ON_function(i);27 stateSwitch[i] = 1;28 }29 } else {30 if (digitalRead(pins[i]) == 0) {31 OFF_function(i);32 stateSwitch[i] = 0;33 }34 }35 }36}37
38void ON_function(int n) {39 Serial.print("Switch ");40 Serial.print(n+1);41 Serial.println(" ON");42}43
44void OFF_function(int n) {45 Serial.print("Switch ");46 Serial.print(n+1);47 Serial.println(" OFF");48}
And here you can see what's goes on the serial monitor when you turn on all the switch from 1 to 8 and then off from 8 to 1:
Considerations
If something strange happens when you turn a switch on or off, like if the switch was triggered on and off super rapidly, don't panic! That's normal, and it is related to something called "debounce". You can have a look here to understand what's going on.
Conclusions
The Arduino DIN Simul8 is the perfect playground to start experiment your coding skill in the PLC world. Try creating more functions that could be triggered by some combination of switch position, or whatever you like, have fun!
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.