How to send signals between a Siemens PLC and an Arduino

A Siemens PLC uses inputs and outputs that operate on 24V, while an Arduino has inputs and outputs of 5V. Thus you can’t just plug an output from your PLC into the input of an Arduino. There are several ways to circumvent this problem, such as a converter (step-up or step-down) or relays.

Converters

If you decide to use converters, you need to check two things.

  1. Is the input side rated for the amount of voltage you are supplying it with?
  2. Is the output side rated for the amount of voltage you need?

If your converter is rated for both the input and output you want, you can wire the input side up. Next you need to use a multimeter to check what the current voltage is that it outputs. Rotate the potentiometer untill you have the voltage you need on the output, then turn off the input voltage and wire the ouput up. This should work for both a step-up and step-down converter.
If you want to send a signal from the PLC (24V) to the Arduino (5V) and also back, you’ll need both a step-down converter (from 24V to 5V) and a step-up converter (from 5V to 24V).

Relays

If you decide to use relays, you need to ones again check two things:

  1. Is the switching coil rated for the amount of voltage you are supplying it with?
  2. Is the COM to NO/NC rated for the amount of voltage you need?

If you’ve found a relay that can handle both the input and output of what you need, you can start wiring it. You can either place the relay in a breadboard or solder some wires to the pins of the relay. You need to wire the switching coil to the output and ground of the first device (most of the times, it doesn’t matter which way around), the COM to the live wire of the input device (so the 24V of the PLC or the 5V of the Arduino) and finally you need to wire the Normally Open or Normally Closed to the input of the input device (check if you need a pull-up/pull-down resistor on the input, so you don’t get a floating input value).
If everything is wired up correctly it could look something like this:

Programming the devices

PLC

To program the PLC in TIA-Portal to be able to interact with the Arduino, you can follow these steps:

  1. Create an input and an output tag in your default tag table that correspond with the input and output pin that you wired it up to on your PLC. (I also made a start button tag to start the program and a pneumatic cylinder output tag to trigger when a signal is received).
  2. Create code to send an output to the Arduino (this example is made in the programming language FBD)
  3. Create code to receive an input from the Arduino

Arduino

To program the Arduino to be able to interact with the PLC, you can use and change to following code:

// Define the pins that will be used
#define RELAYINPUTPIN 13      // Goes from the 24V PLC output to the 5V Arduino input
#define RELAYOUTPUTPIN 12     // Goes from the 5V Arduino output to the 24V PLC input

// Create variable for rising edge detection
int lastInput = LOW;

void setup() {
  // Set the baudrate
  Serial.begin(9600);

  // Setup the input and output pins in the code
  pinMode(RELAYINPUTPIN, INPUT);        // You can make this a INPUT_PULLUP if you don't want to use a physical pull-down resistor

  // Set the relay output pin to LOW
  digitalWrite(RELAYOUTPUTPIN, LOW);
}

void loop() {
  // Read the relay input and check if it has gone from low to high (rising edge)
  int status = digitalRead(RELAYINPUTPIN);
  if (status == HIGH && lastInput == LOW) {
    /*
    ---------------------------------
    Put your Arduino code that needs to be run here
    ---------------------------------
    */

    // Give an input signal to the PLC that the Arduino code is done running
    digitalWrite(RELAYOUTPUTPIN, HIGH);
    delay(250);   // Make the sigal 250ms long
    digitalWrite(RELAYOUTPUTPIN, LOW);
  }
  // Write the relay input to the lastInput variable for rising edge detection
  lastInput = status;
  delay(50);
}

With this, you should be able to send signals between a PLC and a Arduino (should work similarly with other microcontrollers if needed)