Hi,
I have a universal robot UR10e. I also have an Arduino Uno with a Ethernet IP port.
I want to send a value to the robot through Modbus but I didn’t succeed.
I would like to send an analog value from the Arduino to the robot and register it in a variable.
How can I do that from physically connecting the robot to the Arduino board to seeing the analog value in the analog output.
Merci !
@abergeron I have the same question. Were you able to connect to an Arduino Uno with Ethernet port and use Modbus TCP to send data over?
I @abalalimoghaddam, yes I have been able to connect my arduino uno to the robot using this code in the arcuino board :
#include <SPI.h>
#include <Ethernet.h>
#include <ModbusEthernet.h>
const int analogInputPin = A0;
const unsigned long samplingInterval = 1;
const unsigned long windowDuration = 500;
//const unsigned long numSamples = windowDuration / samplingInterval;
int sampleCount = 0;
int totalValue = 0;
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 101);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);
const int MODBUS_PORT = 502;
const int MODBUS_SLAVE_ADDRESS = 231; // Choose a value between 1 and 247 (avoid 0 and 248)
ModbusEthernet mb;
//unsigned int samples[numSamples];
unsigned int currentIndex = 0; // Current index for inserting the sample
unsigned long lastSamplingTime = 0; // Time of the last sample
unsigned long lastSendTime = 0; // Time of the last sample
void setup() {
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Serial.begin(9600);
Serial.begin(115200);
#if defined(AVR_LEONARDO)
while (!Serial) {}
#endif
Ethernet.init(5);
mb.connect((100,0,0,100),502); //Robot connection
delay(1000);
mb.server();
// Add a single Holding register to store the average value
mb.addIreg(30002);
}
void loop() {
unsigned long currentTime = millis();
// Perform sampling every ‘samplingInterval’ milliseconds
if (currentTime - lastSamplingTime >= samplingInterval) {
lastSamplingTime = currentTime;
unsigned int analogValue = analogRead(analogInputPin);
totalValue += analogValue;
sampleCount++;
}
if (currentTime - lastSendTime >= windowDuration) {
lastSendTime = currentTime;
if (sampleCount > 0) {
unsigned int average = totalValue / sampleCount;
// Write the average to the Modbus Holding register (Holding Register #100)
mb.Ireg(30002,average);
Serial.print("Average : ");
Serial.println(average);
sampleCount = 0;
totalValue = 0;
}
}
mb.task(); // Handle Server Modbus TCP queries
delay(50);
}
@abergeron thanks for sharing the code, it was helpful to get me started. It looks like you used this code to send some information from the Arduino to the Robot controller, right?
What I need to do is to send a couple of binary coils and a couple of analog registers to the Arduino so that it can control the stepper motors accordingly.
I am also running into some problems testing even the most basic Modbus setup with a simulator. I described my problem in another post.
Thank you!
Hi @abalalimoghaddam from what I see on your other post, your master (the robot) do not resave the connection confirmation to the slave. To know if your modbus is well configured you need the grey dot in the installation to be green.
To do so. it is important that your arduino board has the same slave address as the address you choose in the installation of the robot.
for exemple, this was my address on my board so I would choose the same for the robot :
You were right that was the problem with the simulator. I matched the address in the simulator to the “Modbus Slave Address” in the MODBUS settings in the installation tab and the light went green and communication is working as expected. I also tested it now with an ESP8266 and with that I did not have a way of setting the Modbus slave address so I left it as the default (255) and it is working.
Thank you so much for your help!