Skip to Content

First steps with Arduino and ESP32 based PLCs

Install the Industrial Shields boards in the Arduino IDE, use the pin names of your PLC and run a first Ethernet example. Three short guides to start programming your industrial automation project without the most common issues.

M-Duino and Ardbox Arduino PLCs for DIN rail

Quick guides

Start here

Follow the guides in order. Each one takes a few minutes.

Also check the switch options, the LED signals and the power supply needs of your PLC in its user guide before you power it.

Guide 1

Install the Industrial Shields boards in the Arduino IDE

The Industrial Shields boards are a package of board definitions and libraries for the Arduino IDE. When you select one of our PLCs, the IDE uses them. They give you two things:

  • Automatic definition, variable association and pinMode of each PLC pin.
  • The Industrial Shields libraries for the PLC features, such as the RS-232 and RS-485 ports.

Our pins (QX.X, IX.X, AX.X, RX.X) point to a real Arduino pin, and this pin changes between models. A sketch written with the boards also works on future versions and other models. Without them, it does not.

With the boards installed, you do not need to know which Arduino board is inside each PLC, check the pin mapping or add our libraries by hand.

PLC Arduino - M-Duino Etherner controller familyPLC Arduino - Ardbox controller family

Steps

  1. Open the Arduino IDE 2.x. Version 1.8.x also works. If you do not have it, download it from the Arduino website.
  2. Go to the "File" menu and open "Preferences".
  3. In the "Additional boards manager URLs" box, add this address:
    https://apps.industrialshields.com/main/arduino/boards/package_industrialshields_index.json
    Install boards Arduino IDE - PLC Arduino
  4. Close the preferences window with the "OK" button.
  5. Open the "Tools" menu, then "Boards", and click "Boards Manager".
  6. Type "industrialshields" in the search filter, select it in the list and click "Install".
    Install boards Arduino IDE - PLC Arduino
  7. Close the "Boards Manager".

Now you can select each PLC in "Tools" > "Boards": ARDBOX, M-DUINO and the rest of the family.

Install boards Arduino IDE - PLC Arduino

Examples of use

Once the boards are installed, the IDE includes usage examples for your Arduino based controller. Find them in "File" > "Examples" > "MDuino Family Examples".

Install boards Arduino IDE - PLC Arduino - Examples

With the boards, you no longer need to overwrite w5100.h in the Ethernet library for the M-DUINO family.

See the process step by step in this video:

 
 

More in the blog: How to install the boards in the Arduino IDE

Guide 2

Use the pin names of your PLC

Install the boards first (guide 1). Then the Arduino IDE does the mapping of your Industrial Shields PLC for you: you use the PLC pin name and the IDE finds the Arduino pin.

Select your board, family and model. Then write each pin with "_" instead of ".". For example, Q0.1 becomes Q0_1.

  • Digital outputs: QX_X
  • Analog outputs: AX_X
  • Relay outputs: RX_X
  • Inputs, digital or analog: IX_X
Use Mapping Pins - PLC Arduino

Sketch example

// In this case is selected the M-DUINO 57R+ board (IS.MDuino.57R+), 
//but the way to use the GPIOs is the same for every Industrial Shields PLC
void setup() {  } //////////////////////////////////////////////////////////////////////////////////////// void loop() { // Toggle pins once a second (I = Input Pins, Q = Digital Outputs, A = Analog Outputs & R = Relay Outputs) digitalRead(I0_0);//Reading a Digital Input
analogRead(I0_8);//Reading an Analog Input

digitalWrite(Q0_0, HIGH);//Writing a HIGH on a Digital Output digitalWrite(Q0_1, HIGH);//Writing a HIGH on a Digital Output //... analogWrite(A0_0, 188);//Writing on an Analog Output analogWrite(A0_1, 255);//Writing on an Analog Output //... digitalWrite(R0_0, HIGH);//Writing a HIGH on a Relay Output digitalWrite(R0_1, HIGH);//Writing a HIGH on a Relay Output //... delay(1000); digitalWrite(Q0_0, LOW);//Writing a LOW on a Digital Output digitalWrite(Q0_1, LOW);//Writing a LOW on a Digital Output //... digitalWrite(R0_0, LOW);//Writing a LOW on a relay Output digitalWrite(R0_1, LOW);//Writing a LOW on a relay Output //... delay(1000); } 

More in the blog: How to use the mapping pins of Industrial Shields boards

Guide 3

Connect an Arduino based PLC to a TCP server

Connect an M-Duino PLC to a TCP server over Ethernet. This example follows the TCP example of the Arduino website with a few changes.

Requirements

Steps

  1. Start the TCP server and write down its IP address and the port where it listens.
  2. Upload this sketch to the M-Duino. Change the IP addresses and the port to the ones of your network.
    #include <Ethernet.h>
    #include <SPI.h>
    
    byte mac[] = { 0xBE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 192, 168, 1, 100 };
    byte server[] = { 192, 168, 1, 105 }; // TCP server (server.js)
    int tcp_port = 5566;
    
    EthernetClient client;
    
    void setup()
    {
      Ethernet.begin(mac, ip);
      Serial.begin(9600);
    
      delay(1000);
    
      Serial.println("Connecting..."); 

     if (client.connect(server, tcp_port)) { // Connection to server.js Serial.println("Connected to server.js"); client.println(); } else { Serial.println("connection failed"); } } void loop() { if (client.available()) { if(Serial.available()){ char s = Serial.read(); client.write(s); // Send what is read on the serial monitor char c = client.read(); Serial.print(c); // Print on serial monitor the data from server } } if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); for(;;) ; } }
    More about the TCP functions in the Arduino reference: Ethernet library
  3. Open the serial monitor and type a message, for example "Hello server.js". The server replies and the reply appears on the serial monitor.

Server side (server.js)

TCP server output in server.js

M-Duino serial monitor

M-Duino serial monitor with the server reply

Need help?

Our support team answers your technical questions about installation and programming.