Problem with my stepper motor

hello english is not my first language sorry

Hello everyone, I'm having a problem controlling a bipolar stepper motor with an Arduino board. Let me briefly explain my situation:
My stepper motor has an axis around which a wire is wound. I have a dipole supplying a variable voltage. This voltage takes a finite number of values (16 in all). To limit the vagaries of precision, I've chosen to define voltage intervals instead. This voltage will be read by my Arduino board. Then, for each voltage value, my motor must have unwound a certain length of wire (relative to the starting position, i.e. when 0mm of wire is unwound). So I've created a list that gives (in the right order) the unwound lengths associated with each voltage interval read. The program will then read the tension, check whether it lies within one of the intervals I've defined, and if so, calculate the number of steps required (relative to the initial state) to unwind the associated length. Next, the program makes the difference between the current position in steps and the position to be reached, and the motor moves as many steps as the difference (in the right direction, thanks to a condition on the sign of the difference). As the voltage will vary during use, the operation is in loop.

However, when I run my code, the wire unwinds once, then nothing, even though I'm still varying the voltage.

My Arduino is a UNO R3
My motor is a bipolar stepper.
My electronic chopper is an L298N
My power supply is 15V

I can give you my connections if you need them, even though I think they're good, since the operation is performed once at each launch. I think the problem lies in the code, but I can't identify it.

Please do not mind the french comments sorry

#include <AccelStepper.h>

// Définitions des pins de contrôle
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
#define EN1 6
#define EN2 5
#define POT_PIN A0

// Initialisation des étapes du moteur
int stepsPerRevolution = 200; // Ajuster selon les spécifications du moteur
float mmPerStep = (8.0 * 3.14159) / 200.0; // Diamètre de l'axe (8 mm) converti en mm par pas

// Listes des intervalles de tension et des longueurs de fil correspondantes
float debutIntervalles[] = {0,1,2,3,4}; // Début des intervalles de tension
float finIntervalles[] = {1,2,3,4,5};   // Fin des intervalles de tension
float longueurs[] = {3,5,6,9,10};         // Longueurs de fil déroulées correspondantes en mm

// Variables pour la position
long targetPosition = 0;
long currentPosition = 0;

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(EN1, OUTPUT);
  pinMode(EN2, OUTPUT);

  digitalWrite(EN1, HIGH);
  digitalWrite(EN2, HIGH);

  Serial.begin(9600);
}

// Fonction pour faire un pas du moteur
void stepMotor(int step) {
  switch (step) {
    case 0:
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      break;
    case 1:
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      break;
    case 2:
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      break;
    case 3:
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      break;
  }
}

// Fonction pour faire tourner le moteur jusqu'à la position cible
void moveToPosition(long target) {
  long stepsToMove = target - currentPosition;
  int direction = stepsToMove > 0 ? 1 : -1;

  stepsToMove = abs(stepsToMove);

  for (long i = 0; i < stepsToMove; i++) {
    stepMotor((currentPosition + i * direction) % 4);
    delay(10); // Ajuster pour contrôler la vitesse
  }

  currentPosition = target;
}

void loop() {
  int potValue = analogRead(POT_PIN);
  float voltage = potValue * (5.0 / 1023.0);

  // Map la tension lue aux intervalles de longueur de corde
  for (int i = 0; i < sizeof(debutIntervalles) / sizeof(debutIntervalles[0]); i++) {
    if (voltage >= debutIntervalles[i] && voltage < finIntervalles[i]) {
      targetPosition = longueurs[i] / mmPerStep;
      break;
    }
  }

  moveToPosition(targetPosition);
  delay(50);
}

Sorry, this does not make sense. Please post a schematic diagram and a drawing explaining your apparatus. Also post a link to the data sheet or product page for the stepper.

Note that the L298N is a brushed DC motor driver, not a stepper driver, and cannot be used with low impedance stepper motors (the most common type).

You are asking for the size of the arrays, in bytes, NOT the values in the array entry you are interested in.

this is the product page for the stepper:

This is my installation :

The driver seems to be working well with my motor though, that is why i don't think its the problem

The L298N may work, but it is totally inappropriate for that stepper motor. The driver chip is probably overheating and may partially or totally shut down.

The A4988 or DRV8833 stepper drivers would be appropriate choices, with the current limit set to 1 Ampere per winding.

The code and/or power supply may be problems. Power the stepper separately.

From the linked page:

*Courant nominal / phase : 1 A.
*Résistance de phase : 3,6 ohms.
*Inductance : 4,00 mH ± 20 % (1 kHz).

This stepper cannot be operated by the L298N. Stepper and/or driver will be damaged over time.
As already stated you need a current controlling driver like A4988 or DRV8825

Maybe that's a typo?

Yes, DRV8825 as you suggested.

Okay, my professor told me this driver was okay for my motor though, and the motor was working fine with other codes than this one, but I will still try to get another one. Thank you so much for your advice.
Regarding the code, do you detect any issues or mistakes about it ?

Thank you for your advice, I will try to get the new driver. Regarding the code, do you detect any issues or mistakes about it ? To be honest my principal concern is the code working in theory so i can show it to my professor, for the rest i can manage.

You can insert some Serial.print to see what's going on.
I tried your code with WOKWI and the motor moves back and forth if the pot is moved. I don't know if it moves as you want it to move, but it definitely doesn't move only once and respond to the change in voltage.

Still possible that your driver is shutting down because of overload.

Thank you very much, I'll see if I can get another driver

Do you know what kind of motor could work with this driver (if I'm not able to find another one) ?

A high impedance stepper will work with the L298N. The coil resistance should be high enough that the current draw is less than 1 Ampere per channel, which is maximum the L298N can handle under steady state conditions.

If your 15V power supply can supply 2 Amperes or higher, then a stepper with winding resistance of about 15 Ohms or higher will work. Expect a voltage drop in the L298N of up to 4V per channel, and it should have a heat sink.

If necessary, you can add an external high wattage, current limiting resistor to each winding.

This calculates the number of elements in the array. The size of the entire array divided by the size of one element in the array.

Just to assure there is no mechanical binding try removing the cord (wire) between the axle and the sail and see if the motor is working properly without a load.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.