tadeus
Hi thiagoralves,

Please help me to change IO pin allocation on Arduino Mega, i need more input/output digital pin, so my plan is to exchange analog input/output pin with digital input/output.

existing allocation is (openPLC default):
24 digital inputs, 16 digital outputs, 16 analog inputs, 12 analog outputs.

for my project, i need:
36 digital inputs, 26 digital outputs, 4 analog inputs, 2 analog outputs.

please help me to solve it, how can i edit the code? i used raspberry pi as a host.

thank you
Reply 0 0
thiagoralves
Hi tadeus,

To change the pins, you will need to edit the Arduino Mega firmware and the /core/hardware_layer.cpp file on your Pi. The first thing you need to change is the OPLC_input and OPLC_output structs. These structs define what goes inside the messages between the Arduino and the Pi. It is crucial that these structs remain the same on both sides (Arduino and Pi) otherwise you'll have communication problems. Therefore, if you change the structs on the Arduino side, change equally on the Pi side as well.

The OPLC_input struct has two arrays: uint8_t digital[4] and uint16_t analog[16]. The analog array is simple. It is as big as the number of analog inputs you have: 16. The digital array however can handle 8 digital inputs per byte. Since it is 4 bytes long, it currently can handle 4*8 = 32 digital inputs. If you need more inputs than that you'll have to adjust this size accordingly.

The OPLC_output is analogous to the OPLC_input struct, however, it is for outputs. Adjust the size of the arrays inside these structs to match the number of I/Os you want. After that, just modify the lines:

uint8_t pinMask_DIN[] = {22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 14, 15, 16, 17, 18, 19, 20, 21};
uint8_t pinMask_DOUT[] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};
uint8_t pinMask_AIN[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
uint8_t pinMask_AOUT[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

with the allocation you want. As you may have realized, these pinMask arrays have the pins in the order that the OpenPLC reads them, ex: pin 22 is the first digital input, pin 24 is the second,...

These modifications are for the Arduino firmware. For the Pi, you only need to change the structs in the /core/hardware_layer.cpp file to be exactly the same as your modified Arduino firmware. Everything else should rearrange automagically.

Let me know if it worked!
Reply 0 0
tadeus
thank you thiagoralves for the answer, i'll try it and inform you if it worked
Reply 0 0
tadeus
Hi thiagoralves, i just wanna tell you if your solution has worked successfully, thank you very much!
Reply 0 0
naitik
anyone please send the the link for OpenPLC arduino firmwares to change the IO according to my need
Reply 0 0
thiagoralves
You can change the pin allocations on the upload dialog. You don’t have to edit the firmware anymore
Reply 1 0
KZstu
 
You can change the pin allocations on the upload dialog. You don’t have to edit the firmware anymore
I'm trying to do this for the raspberry pi pico w, I change gp 13 from input to output, I get this error:
Error: wrong location for var __QX0_8
Reply 0 0
thiagoralves
QX0.8 does not exist. A byte has 8 bits (0-7). After QX0.7 it goes to QX1.0
Reply 0 0
KZstu
QX0.8 does not exist. A byte has 8 bits (0-7). After QX0.7 it goes to QX1.0
thank you
Reply 0 0
Reply