This sketch requires the use of a W5100 or W5500 ethernet adapter and optionally supports either an Adafruit ADS1115 ADC and MCP4725 DAC device (or both).
The program allows control of 32 inputs and 32 outputs using the Mach4 modbus plugin.
The sketch accesses the Arduino pins directly using port manipulation for faster signaling. The input pins are active low (NPN -GND) and the output pins are active high (PNP +5v).
You can either use one of the included profiles in the Mach4 Profiles directory, or use the ArduinoModbusInstaller.exe from the dist folder of the repo to update an existing Mach4 profile.
- Arduino Mega
- W5100 Ethernet Shield or W5500 Ethernet Shield
- 12-bit DAC (optional)
- ADS1115 ADC (optional)
To use the ADC or DAC uncomment the #defines at the top of the sketch.
The ADC & DAC inputs and outputs communicate over the i2C bus. Use the i2C scanner at this link: https://gist.github.com/xzenzza/62d35c316191d3a5bae1da68374d4790 to find the address of the devices you are using and change the variables in the sketch accordingly.
https://github.com/adafruit/Adafruit_MCP4725
https://github.com/adafruit/Adafruit_BusIO
https://github.com/adafruit/Adafruit_ADS1X15
- Copy the nested folders in the libraries directory to the Arduino sketch folder on your computer (usually in Documents/Arduino/libraries).
- Change the IP address of the Arduino in the Mach4ModbusW5500.ino sketch to conform to your network. For example, if your network subnet is 192.168.1.X then change the IP address to something like 192.168.1.85.
- Upload the Mach4ModbusW5500.ino file to the Arduino Mega using the Arduino (or other) IDE.
- Connect the Arduino to power and the network.
There are several configurations that can be made using the ArduinoModbusInstaller.exe program.
a. Use just the inputs and outputs on the Arduino Mega (this is the default).
b. Use inputs outputs and ADC.
c. Use inputs outputs and DAC.
d. Use inputs outputs ADC and DAC.
To change these configurations select the appropriate check box(es) in the installer.
- Launch the ArduinoModbusInstaller.exe from the dist folder of the repo.
- Select the Mach4 profile you want to update, enter the IP address of the Arduino and press install (ip address is from the Arduino sketch).
- Open Mach4 and navigate to Diagnostic->Modbus in the File Menu.
- To connect or reconnect to the modbus device, press the stop button in the diagnostic window and then the play button.
Pin 53 is used as the SS or CS pin so that pin 10 can be used as an output. If you are using the W5100 ethernet shield, then pin 10 should be bent or cut off and wire should be connected from pin 10 on the Ethernet shield to pin 53 of the Arduino Mega.
***This setup is not necessary of you used the exe installer; only if you are importing one of the provided Mach4 profiles in the repo
The IP address in the sketch and the Mach4 modbus plugin configuration must be setup according to the network configuration of the connected device. The modbus plugin configuration can be accessed through the Mach4 file menu at: Configure>Plugins...>Modbus.
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 38, 39, 40, 41}
{A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10,
A11, A12, A13, A14, A15, 30, 31, 32, 33, 34,
35, 36, 37, 42, 43, 44, 45, 46, 47, 48, 49}
https://www.youtube.com/watch?v=koNoJPKRC_M
https://github.com/andresarmento/modbus-arduino
The following image shows the response time difference of using a for loop in the Arduino loop() function versus just using the loop() function and incrementing a global position variable to read/write modbus registers. Using just the Arduino loop results in much faster response times.