In industrial automation, effective communication between devices is essential. This post focuses on the practical aspects of integrating a USB to RS232 adapter with an Arduino based PLC, specifically the M-Duino PLC setup. We will cover the necessary steps, from connecting the hardware to programming, to ensure proper communication with a PC or another USB-capable device using the RS232 serial communication protocol.
Essential requirements for Arduino PLC communication: Key components
Step-by-step guide to M-Duino PLC setup
Connecting the Hardware
For this tutorial, we use an RS232 to USB adapter featuring a DB9 connector with 9 pins numbered from 1 to 9. We will use 3 pins: RX (pin 2), TX (pin 3), and GND (pin 5). In the M-Duino's case, the TX, RX, and GND pins are used. The diagram below illustrates how to connect the M-Duino PLC to the RS232 adapter:
Programming the PLC for RS232 communication
The first requirement for RS232 serial communication to work is to properly power the PLC using a 12-24Vdc power supply. Once the PLC is powered and the connections are made, you need to program the PLC.
We will use the RS232 library included with our boards package. If you do not have it yet, you can install it following this tutorial. Remember to select your PLC model in the IDE.
Example code for RS232 serial communication with Arduino
Check the following example, which implements bidirectional communication. Characters written in the Arduino serial monitor will be printed on it as well as sent through RS232. Received messages will also be printed in the serial monitor.
Note that in the RS232.begin() function the baudrate used in the communication needs to be specified: in this case, 115200. The PLC and the USB-RS232 device need to be working at the same baudrate for the communication to work. There are also some additional parameters which can be configured:
- Data bits: 8 default.
- Stop bits: 1 default.
- Parity: none default.
These parameters can be changed adding a second argument to the begin function. For example: RS232.begin(115200, "SERIAL_8N2") where "8N2" means the following:
- 8 data bits
- No parity
- 2 stop bits
#include<RS232.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
RS232.begin(115200);
}
void loop() {
while (RS232.available()) {
Serial.println(RS232.read());
}
while (Serial.available()) {
char a = Serial.read();
RS232.write(a);
Serial.print(a);
}
}
Configuring the USB-RS232 adapter for Arduino integration
Testing the serial communication on different platforms
The USB-RS232 adapter can be used with any terminal emulator to test the serial communication:
- On Windows, programs such as TeraTerm can be used.
- On Linux, Picocom or other terminal emulators can be used.
When the USB device is connected to the PC, a port should be available: COMX in windows and /dev/ttyUSBX in Linux. Identify your device and then access it using your preferred terminal emulator.
In this case we will be using Picocom. The following command can be used to open a connection with a 115200 baudrate:
picocom /dev/ttyUSB0 -b 115200
The default configuration is 8 data bits, 1 stop bit and no parity. The --echo flag can be specified so that the terminal prints every message we send through it, as well as the received ones.
Demonstration of successful Arduino RS232 integration
Real-time communication example
With the knowledge from this post, you can successfully communicate using RS232 serial communication. Check the following capture: the left window is the Arduino serial monitor, and the right one is Picocom. Messages can be sent in both directions successfully.
Final thoughts on Arduino PLC communication with USB-RS232 adapter
Successfully integrating an Arduino-based PLC with a USB-RS232 adapter can significantly enhance your industrial automation projects. By following this guide, you’ve learned how to set up and program an M-Duino PLC for seamless RS232 serial communication with various USB-capable devices. This efficient communication setup not only broadens the scope of your automation capabilities but also ensures reliable data transfer and device interaction in your industrial environment. Mastering these skills is essential for creating more versatile and effective automation systems.
How to communicate an Arduino based PLC using a USB-RS232 adapter