Conflict with Stepper and Ethernet2 libraries

Hello everyone.

I am currently working on a project where I want to control a stepper motor with my Arduino Uno R4 Minima board using TCP/IP commands.

I use an ethernet shield (W5500) and my H bridge is a L298. Everything is correctly wired. By that I mean I ran a test program the motor and everything worked well. As well, I ran a test program to test my TCP/IP commands and everything was working : command was received by the arduino and the computer correctly received the answer.

However when I try to do one program with everybody (Ethernet + Motor) then nothing works. I have that error when I try to send a command :

[WinError 10061] No connection could be made because the target machine actively refused it

Here is the program :

#include <SPI.h>
#include <Ethernet2.h>
#include <Stepper.h>

// Bobinage 1
int BOB1_A = 3;
int BOB1_C = 4;
// Bobinage 2
int BOB2_B = 6;
int BOB2_D = 7;

// Moteur pas a pas
int deg = 7200;  // 7200 steps/rev


int vitesse = 8;

/*If we want to change the speed the thing is that the delay
between pulses for the two coil should not be shorter than 0.83ms (ie corresponding to a frequency of 1200Hz)
If you need to check that take an oscilloscope and scope for example A & B (yellow and black wires).
The speed is given to the program in rev/min. To compute the delay between the pulses the formula is :

delay_pulses = 60 000 000/steps/speed (in ms)

*/


Stepper MOTEUR(7200, BOB1_A, BOB1_C, BOB2_B, BOB2_D);
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x11, 0x7F };  // Could be found at the bottom of the ethernet shield
IPAddress ip(192, 168, 0, 27);                        //Arbitrary just make sure the adress is not already taken
EthernetServer server(23);





void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("Starting server...");
  delay(1500);
  Ethernet.begin(mac, ip);
  delay(1500);
  server.begin();
  Serial.println("Server started.");
}

void loop() {
  EthernetClient client = server.available();
  String cmd_parts[2];
  deg = 0;
  if (client) {

    while (client.connected()) {
      if (client.available()) {

        String command = client.readStringUntil('\n');
        command.trim();
        split_command(cmd_parts, command);

        if (cmd_parts[0] == "MOT") {



          if (cmd_parts[1] != "") {
           //MOTEUR.step(20*cmd_parts[1].toFloat()); // 20 is because of 7200/360=20
           client.println("Motor moved of : " + String(deg) + "°");
            //digitalWrite(LED_BUILTIN,HIGH);
          }
        }

        if (cmd_parts[0] == "TEST") {
          float val = cmd_parts[1].toFloat();
          client.println("Value received : " + String(val));
        }
        client.println("Commande reçue : " + command);
      }
    }
    client.stop();  
  }
}






void split_command(String res[], String cmd) {

  char buff[128];
  char *p;
  int i = 0;

  cmd.toCharArray(buff, sizeof(buff));

  p = strtok(buff, " ");

  while (p && i < 2) {
    res[i] = p;
    p = strtok(NULL, " ");
    i++;
  }
}

float deg_to_step(float deg) {

  return 7200 * deg / 360;
}

What is strange is that when I remove (comment) the line of the definition of the motor
( Stepper MOTEUR(7200, BOB1_A, BOB1_C, BOB2_B, BOB2_D);, and removed everything that could make the motor to move) then TCP/IP commands work well again.

It seems that there are conflicts between the two libraries. I know that I should not use D10-D13 when SPI is loaded but my motor setup uses D3;D4;D6;D7.
I send the command in a simple way with a Python script :

import socket

def send_command(command):

    try:
       
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.settimeout(5) 
            s.connect(("192.168.0.27", 23)) 
            
          
            command += "\n" 
            s.sendall(command.encode())

            
            response = s.recv(1024).decode().strip()
            return response
        
    except socket.timeout:
        return "Erreur : Timeout de connexion"
    except socket.error as e:
        return f"Erreur de connexion : {e}"

The board is powered by jack (12V). The bridge needs 8 wires :

  • 1 to D3
  • 1 to D4
  • 1 to D6
  • 1 to D7
  • 3 to 5V (parallel)
  • 1 to GND

The bridge has an external 12V source. I tried to use AccelStepper instead of Stepper but the result was the same...
It is a bit strange if anyone has already faced such problem I would be happy to be helped :slight_smile:

why do you use the Ethernet2 library?

I forgot to mention that. The code in my first message is my last attempt. I read that Ethernet 2 was compatible with my shield and a bit lighter than Ethernet.

The main difference between the two in my project is that when I try to define the motor with Ethernet 2 I can ping the Arduino but I have that WinError 10061. With Ethernet I can't even ping the Arduino.
So Ethernet 2 was a small progress compare to Ethernet

use Ethernet.init(10); with the Ethernet library

Update : Thanks for the help. By reading again the doc of my shield it seems like it uses the pin 4 for the SD card. Moving the pins for my motor from 3-4-6-7 to 6-7-8-9 solved the problem.

do you use the SD card? is it inserted? if not, pin 4 is free

No I'm not using it but by doing some tests it seems it was the problem.