Factory I/O is a Factory simulator made to connect to PLC's (industrial controllers) to learn and train in a virtual environment.
You have some predefined scene's or can make a Factory of you own to program and learn. I used the software a lot with PLC controllers and I wanted to connect it with Arduino but that is not supported by the program. So in this project we will cover connecting Factory I/O with Arduino.
And an important thing to note that Factory I/O is not free but has a trail version of 30 days. It is the good kind of trail, read: without giving any credit card information :-).
You can download Factory I/O and learn more about it by visiting the website.
Video TutorialYou could watch this video to get started and you can find a detailed step guide below it with some extra details.
How to connect the Arduino to Factory I/O?How it started? (you can skip this part if you want to).
I mentioned before that the Arduino in not supported by Factory I/O but a Modbus TCP/IP server can be used. I was happy to hear that because there are Modbus library's written for Arduino. Modbus is a communication protocol that is used in automation because it is fast. There is a lot of info out there about Modbus but this is not a tutorial about Modbus so we will leave it at that.
When trying to figure out how to connect the Arduino with the Modbus server in Factory I/O I came to the conclusion that it is not possible to make a direct connection. The reason is that the Arduino Modbus goes over the USB cable so the communication has to be serial. And the server that is used by Factory I/O is also Modbus but it is using Modbus over TCP/IP.
I really wanted to use a normal Arduino to do this so I searched for a software tool to convert the serial Modbus signal of the Arduino to Modbus TCP/IP but there where no free tools available.
So I figured how hard can it be?
For me it was a challenge. I'm a mechatronics technician and I have no formal training in IT. So I made an app using Python to parse the data from the Arduino to Modbus TCP/IP. The code is probably not up to standards of professionals but it gets the job done. It was also frustrating constantly switching between the two languages Python and C++ like you can imagen.
The end result is a small Exe. file (8MB) that you have to download and run and you are ready to connect the two.
Setting Up Factory I/OSo first off, Download and install Factory I/O on your computer.
https://factoryio.com/start-trial
After that you open Factory I/O. In the start screen click on "Scenes" and open then select the scene "2- from A to B (Set and Reset).
Now the scene will open. If you never used Factory IO you can start by exploring the camera settings and just take a look around. How to use the camera is well explained in the Factory I/O manuals and only takes a minute. You only need the basic camera functions https://docs.factoryio.com/manual/navigation/
To continue you have to enable the two functions in the right hand corner ("show sensor tags" and "show actuator tags"). Then you press the play button and you will notice the entry conveyer runs automatically. This is because it is forced to true. This is not a bug but a feature, and a part of the exercise.
So you only have to worry to run the conveyer when the box reaches Sensor A and make the conveyer stop when its read Sensor B. Notice that bought sensors are orange. This is because they are "1" or "True" when the see the reflector. So the will be "0" or "False" when there is a box in front of it.
You can for now press the stop button and click on the "File" button in the left hand of the window and select "Drivers" (or you can use the shortcut F4).
Drivers are what will drive the scene and you can select all kind of PLC's but to connect to the Arduino you have to select Modbus TCP IP Server.
After that you click on "configuration". You have to set the configuration the same as the following image (Tip: click on the image for more detail).
If you go back your Modbus server driver should look like this.
So Sensor A is connected to Input 0 and Sensor B is connected to Input 1. The conveyer is connected to Coil 0 (Output 0).
Nerd fact:Coils are the same as digital outputs. It is a remnant of the days that automated machines worked with relays. To make the transition to from relay logic to PLC's the named "Coils" as digital outputs. Modbus was invented to communicate between PLC's and there fore they also used the term "Coils".
No the we are all set to the Factory I/O side. You can use the setting for the driver for all the scene's and you are just limited by the 16 input, 16 outputs and 8 Analog inputs and outputs.
Setting up the ArduinoFor this you will need the ZIP file provided in this project.
If you want to figure out the logic for the scene out on your own you use the file : "Arduino_Link_To_FactoryIO_Firmware"
One important thing to remember is that you can not use the delay function in your code or the Modbus communication will Time Out!
*This is also not done in real automation projects.
If you want to link it right away you download the ZIP :
"Arduino_Link_To_FactoryIO_Scene2"
Don't forget to unzip it. If you just open it the attached code are not there and you will get compile errors!
If you open the script you will see some tabs but the only tab you need is the main script. In the other tabs you can see the code to handle the communication. A big thanks to Andre Sarmento Barbosa how wrote the Library code for the Modbus communication.
So to get you started you can store the inputs in a variable. I would advice to use the same variables that are used in the Factory I/O but makes sure it is one word. For example:
// make sure you defined your variables in the void set-up. (bool Sensor_A = 0;, ...)
//reading the inputs
Sensor_A = FactoryIO_Input0;
Sensor_B = FactoryIO_Input1;
//The control logic
if.... then.... else...? You can check out the ZIP file "Arduino_Link_To_FactoryIO_Scene2" if you are stuck on the logic. Or you can just upload it to see if it works...
// writing the outputs
Conveyer = FactoryIO_Coil0;
* My next project will be solving a more complex scene (scene 3) in Factory I/O so if you don't want to miss it you can follow me.
Then you upload the sketch to your Arduino Uno, Nano or Mega.
Linking the twoI previously mentioned that there is no way to directly connect an Arduino to Factory IO because the communication from the Arduino to the PC works over USB an so a serial connection and the server is running on Modbus TCP/IP.
So I wrote a program (in python) that just parse the data from the Arduino to the server and acts like a Modbus client.
I will add the Python script for the ones that are interested but like mentioned before I have no formal training in text based coding (but it works). but I compiled it in a executable file off 8MB.
So download this file attached to the project: "Software_converter_modbus"
First some basic things you must check to make sure it will all work:
- Make sure your Factory I/O is opened and your driver is configured.
- Make sure you wrote the program on the Arduino and you know the COM port number.
- Make sure your Arduino is connected to your PC
- Don't use the serial monitor because the serial connection is needed to communicate to your computer.
Then you run the program. It will give a notification because the program is not known to Windows (if I have the time I will call my friend Bill Gates to fix this).
But for now you have to click on "more info".
And click "Run Anyway".
It is possible your antivirus will block it because it is an exe. file but this is the way.
Then this beautiful minimalistic designed user interface will pop-up:
You select the COM port the Arduino is connected to and the IP address should be good to go (default IP address in Factory I/O).
After that you close the window and on the terminal you should see the following.
The "read Input 0" is showing because the python script will read the inputs from Factory I/O only if they are changed. Because initially all the inputs are 0 and because the sensor A and B are 1 when there is no box it will detected the change. I did not used the same functionality with the integers just because it would be spammed full if you use an analog input of the Arduino as an input to Factory I/O because of the instability.
The RX and TX light on the Arduino should blink rapidly when connected.
If you want to stop the communication you just close the terminal. Also note you have to stop the communication to download to the Arduino.
If you get an error please read it true and make sure to check:
The Driver config in Factory IO, The program is successfully uploaded to the Arduino, No serial connection to the Arduino is already established (like serial monitor), Your antivirus approved the script (as a test you can disable it), You program is written correctly (if in doubt use the program written for Scene 2 attached to this project, Factory I/O is in play mode (not in stop), You have no other Modbus servers running on the PC.
So now you should be connected to Factory I/O and with the same method you can write code for all the scene's or you can create your own scene's.
If you want to learn more Factory I/O an Industrial automation you can check out my YouTube channel: I'm manly focused on Arduino projects and Industrial control logic. Because I'm now able to also link Arduino to Factory I/O I will compare PLC's (Industrial controllers made for automation) and Arduino to really dive in the differences by example.
If you are in that kind of stuff you could check it out:
https://www.youtube.com/c/seafoxc
Bonus TipIt is super easy to include Arduino Inputs and Outputs to your code. This way you could make a control panel to drive your scene.
Like using a Start Pusbutton or a status light.
In the next project we will solve scene 3 and use physical buttons as a way to control the scene. If you want to have a challenge you can try to solve scene 3 on your own...
You can find out how the scene should work in the Factory I/O documentation:
https://docs.factoryio.com/manual/scenes/filling-tank/
How you could support my workI'm doing this as a hobby but I would like to break even if possible and spend more time creating video's and tutorials and by decent equipment to do so (I'm now filming with my phone).
The most easy way you could help me out is subscribing to my YouTube channel (only if you enjoy the content of course).
https://www.youtube.com/c/seafoxc
You could also consider buying me a 🍺🍺🍺 or a ☕.
Comments