Go Down

Topic: Stepper motors not following coded path (L298n drivers, NEMA 17hs8401 steppers) (Read 259 times) previous topic - next topic

UNDRINS_JNR

Hi, I am a second year university student, currently working on an graded assignment in which I must produce a machine that can catch six balls, sort them into two groups and drive to two different containers to drop them off. I have the ball collection and sorting system working perfectly, but the motors are not following the path that I have coded (or that I think that I have coded).

Code: [Select]
/*
 * Code to control two stepper motors simultaneously using the stepper library
*/
#include <Stepper.h>

//enter the steps per rev for your motors here
int stepsInRev = 200;
int num_of_steps = 1;

// setup pins for each driver motor1 ~ IN1, IN2, IN3, IN4; motor2 ~ IN1, IN2, IN3, IN4
Stepper myStepper1(stepsInRev, 4, 5, 6, 7);
Stepper myStepper2(stepsInRev, 8, 9, 10, 11);

void setup() {
//set speed for each stepper motor
  myStepper1.setSpeed(30);
  myStepper2.setSpeed(30);
   
   
//delay for start of course
  delay(5000);

//move Karl forward
  for (int x = 0; x < 463; x++){
      myStepper1.step(-1);
      myStepper2.step(1);
    }
   

 //turn Karl to face right
  for(int x = 0; x < 68; x++){
      myStepper2.step(-1);
 }
    delay(500);
 
 //move Karl forward
for(int x = 0; x < 829; x++){
    myStepper1.step(-1);
    myStepper2.step(1);
 }
    delay(500);
 
 //move Karl to face left
for(int x = 0; x < 174; x++){
    myStepper2.step(1);
}
   delay(500);

//move Karl forward
 for(int x = 0; x < 399; x++){
    myStepper1.step(-1);
    myStepper2.step(1);
 }
    delay(500);
 
  //move Karl to face left
for(int x = 0; x < 215; x++){
    myStepper2.step(1);
}
   delay(3000);

//move Karl forward
 for(int x = 0; x < 581;  x++){
    myStepper1.step(-1);
    myStepper2.step(1);
 }
    delay(500);
 
 // steppers stop
  PORTD = B00000000; //sets all of the pins 0 to 7 as LOW to power off stepper1
  PORTB = B00000000; //sets all of the pins 8 to 13 as LOW to power off stepper2
}

void loop(){
}


After running for a short amount of the first for loop, the motors stop for a few seconds, and then repeat this begin again and stop and so on. I have the arduino and stepper motors running off 16 AA batteries. This is my first time doing any coding of any kind, as well as any kind of electronics, so I am very new to it all. Any advice would be greatly appreciated.

Robin2

After running for a short amount of the first for loop, the motors stop for a few seconds, and then repeat this begin again and stop and so on. I have the arduino and stepper motors running off 16 AA batteries. This is my first time doing any coding of any kind, as well as any kind of electronics, so I am very new to it all. Any advice would be greatly appreciated.
How have you everything connected - post a diagram (and please DO NOT use Frizting. A photo of a simple pencil drawing will be easier to understand)

The behaviour you describe is often due to the Arduino resetting because the voltage drops too low.

Post a link to the datasheet for your stepper motors. Stepper motors are very inefficient and are not really suitable for battery power.

Having all your code in the setup() function seems very strange.

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

UNDRINS_JNR

How have you everything connected - post a diagram (and please DO NOT use Frizting. A photo of a simple pencil drawing will be easier to understand)

The behaviour you describe is often due to the Arduino resetting because the voltage drops too low.

Post a link to the datasheet for your stepper motors. Stepper motors are very inefficient and are not really suitable for battery power.

Having all your code in the setup() function seems very strange.

...R
Stepper Motor Basics
Simple Stepper Code

I'm sorry if its hard to see, but I'll do my best to explain how I've wired it. I have one stepper motor connected to each L298n driver, with the correct coils in the motor A and motor B inputs, and the IN1-4 pins connected from the drivers to the arduino, pins 4-7 for one motor and 8-11 for the other. The 12V input on each driver is connected to the battery pack directly through the breadboard, and which outputs 5V from the drivers to the arduino. So the arduino is being powered through the drivers for the steppers. As for having my code in setup, I was unsure how to code in c++, and had asked a friend about how he had done his code for the assignment and he told me to put it in setup.

UNDRINS_JNR

Sorry I forgot to attach the picture, I don't think it will do much as the camera quality is very bad, but here it is.

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

I have an L298 and the scrap of paper that came with it said NOT to power anything from the 5v of the driver - it is only intended for powering the driver logic.

If you have a 12v battery there is no reason not to connect it directly to the barrel jack on the Uno.

Alternatively get a 7805 voltage regulator to drop the 12v to 5v specially for the Arduino.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

UNDRINS_JNR

Ok, thank you very much for your help, I will try rewire it to how you have said.

Go Up