Notifications
Clear all

Arduino based function to control stepper motors

Page 2 / 3

Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 

I don't understand why you would want to open up such small increments (0.88) yet close a large angle (72).  If it was attached to a physical item (shutter), it would break it.

BUT, Now that I have it running... I am observing that it does not like negative steps and it is not your code.  I tried with one of the canned example programs and got the same results when setting negative steps.  Maybe someone else has an answer.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2017
 

@inq 

Stop using the included Stepper.h and switch over to the AccelStepper library. It's much more flexible and reliable. Find it at ...

https://www.airspayce.com/mikem/arduino/AccelStepper/

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Kirk
 Kirk
(@kirk)
Eminent Member
Joined: 12 months ago
Posts: 26
Topic starter  

Inq:

      In response to the three comments you made:

  1. In Setup I inserted the intStepsRequired because I thought all the variables had to be declared. I was unaware that  that statement results in motion of the motor. I will delete it.

2, Defining  Steps_Required seems to be what I don't understand. In the Constants section at the start of the program STEPS_PER_REV= 32

                    GEAR_RED= 64 and STEPS_PER_OUT_REV = the product of 32x48 =2048

           Then in the function Stepper1A  StepsRequired  = STEPS_PER_OUT_REV/ (some #)

          I had the numbers as 20 which I thought would result in 102.4.  Maybe that has to be defined as  an   even  integer?  Am I reading the formula correctly? 

 

3. I don't see where I am assigning a value of 5 to StepsRequired? 

Thanks for your help on this.


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2017
 
Posted by: @kirk

3. I don't see where I am assigning a value of 5 to StepsRequired? 

Search for "StepsRequired = 5;" (without the quotes)

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 
Posted by: @will

@inq 

Stop using the included Stepper.h and switch over to the AccelStepper library. It's much more flexible and reliable. Find it at ...

https://www.airspayce.com/mikem/arduino/AccelStepper/

So far... I haven't used Stepper or AccelStepper.  I wrote my own drivers to handle very specific cases.  However, @kirk might be advisable to use AccelStepper.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


ReplyQuote
Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 
Posted by: @kirk

Inq:

      In response to the three comments you made:

  1. In Setup I inserted the intStepsRequired because I thought all the variables had to be declared. I was unaware that  that statement results in motion of the motor. I will delete it.

2, Defining  Steps_Required seems to be what I don't understand. In the Constants section at the start of the program STEPS_PER_REV= 32

                    GEAR_RED= 64 and STEPS_PER_OUT_REV = the product of 32x48 =2048

           Then in the function Stepper1A  StepsRequired  = STEPS_PER_OUT_REV/ (some #)

          I had the numbers as 20 which I thought would result in 102.4.  Maybe that has to be defined as  an   even  integer?  Am I reading the formula correctly? 

 

3. I don't see where I am assigning a value of 5 to StepsRequired? 

Thanks for your help on this.

  1. You must prefix someone's name with the @ sign ( @Inq ) or they don't get notified of your post.  I just stumbled across this post.  A fluke of chance.
  2. Also, my help has a statute of limitations.  I'm not trying to be rude, and I know real world issues can get in your way of working on a hobby project, but in the intervening 11 days, I've not only disassembled the parts of your project I assembled just to test your code, I've totally forgotten what it was all about.
  3. Your code indicates to me, you don't under many aspects.  Again, not trying to be harsh, but there seems to be a lot of coding fundamentals you do not understand, and stepper motor fundamentals you do not understand. 
  4. The following line numbers are referencing the file provided by @will at the bottom of page 1.  So far you have not been able to provide readable source code.  I won't read code that has ten+ blank lines between each line of code and I'm not going to reformat it.
    1. Line 44 - Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);  The first parameter should be an integer.  The compiler does not complain, so I don't think this is causing your problem.
    2. Also note... I believe the stepper motor you are using is a 5 wire (5 phase motor).  If so you are using the wrong constructor in line 44.  You should have the wires going to 5 pins and using the 5 pin version:  Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11, X);  You will have to figure out which wires go into which pin by some other resource on the Internet or on this forum.  I don't use this Stepper library.
    3. Line 60 in setup() - setSpeed(50); sets the speed that the stepper motor WILL turn.  It does not move it at all.  Your comment out next to it seems to indicate that you thought it actually turns it 90 degrees.  That is incorrect.
    4. Line 59 in setup() - step(StepsRequired); actually tells it to move (in your case) 5 steps.  Unfortunately, you haven't set the speed yet, so it moves 5 steps at ZERO speed... because you have them in the wrong order.
    5. I do not believe you understand STEPS_PER_REV.
  5. Here are how stepper motors work
    1. Stepper motors don't continuously turn like a regular motor.  They only move one step at a time.  When you use the step() function they move ONLY the number of steps you provide.
    2. const float STEPS_PER_REV = 32;  The stepper motor you are using requires 32 steps to make one revolution.  Again... use the step() function to tell it to turn.
    3. const float GEAR_RED = 64;  Your stepper motor also contains a gear reduction drive.  Thus... the shaft that actually comes out of if takes 32 * 64 = 2048 steps to turn around once.  
    4. So at line 144 in your Stepper1A() function, you tell it to move 5 steps.  This results in turning it a mere 0.88 degrees!  So your program may be doing exactly what you are telling it, but you can't see it.  

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


Ron liked
ReplyQuote
Kirk
 Kirk
(@kirk)
Eminent Member
Joined: 12 months ago
Posts: 26
Topic starter  

@inq  Thanks for all of your patience and assistance.  I reviewed your suggestions and will make changes accordingly.  Hopefully I will have the motor running tomorrow and can get back to you on changes in code.  I just had to get my math on the right track.   Kirk


ReplyQuote
Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 

Between getting the 5 wires hooked up properly and using enough steps to actually see a rotation will get you so you can at least see where to go on from there.  BTW... the red wire coming out of the motor (at least on mine) is the 5 odd-ball wire that I think will need to use a pin and add to that Stepper constructor.  But, that is a guess!  I don't use the 5 wires versions.

Also, if it isn't a top-secret, can you share more on your project???  Pictures, purpose, goals???  The concept of opening an aperture, see if there is any light and close it, if there isn't enough, sounds a little strange procedurally for anything that I can think of.  Just curious where this is leading.

I wish you luck.

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


ReplyQuote
Kirk
 Kirk
(@kirk)
Eminent Member
Joined: 12 months ago
Posts: 26
Topic starter  

@will  @Inq

       I updated the code to use a constant of 100 for StepsRequired in the Constants section and both functions (Stepper 1A and Stepper 1B). I deleted some of the statements that you had noted. I have attached the revised program.  So far, I haven't gotten Stepper 1A to turn the motor; so maybe I am still missing something...

 

The goal is to have Stepper 1A open a shutter which is a leaf shaped piece of plastic attached to the motor at one end. The shutter allows the passage or blockage of sunlight (concentrated beam) onto a brick target.

I will probably need to change to light metal for final testing of the shutter.  For the switch function, case 1 tests for blockage of a light beam by solid objects or smoke. This whole system is a hypothetical method for recycling plastic bags by melting their edges together and developing a home insulation product. Case 2 defines action for an unobstructed light path (right angle to solar beam) and activates a function for moving the solar beam as well as activating a conveyor belt drive with standard  DC (or AC) motors.

 


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1480
 

@kirk 

Just had a bit of a read of this thread.
The code you posted, motortest3.ino, just flashes a LED for each key input?
Where is the stepper motor code nicely copied and posted and its wiring?
This is the little stepper motor and controller I have used in projects.

stepperAndController

 (nice copy of the code you attached)

 

char serialData = ' ';

void setup()
{
  Serial.begin(9600); 
  pinMode(32,OUTPUT);
  pinMode(33,OUTPUT);
  pinMode(34,OUTPUT);
  pinMode(35,OUTPUT); 
}

const int LED1 = 32;
const int LED2 = 33;
const int LED3 = 34;
const int LED4 = 35;

int LEDS[5];

void loop()
{
  if (Serial.available() > 0)
  {
    serialData = Serial.read();
    
    if (serialData == '1' && LEDS[1]==0){
       digitalWrite(LED1,HIGH);
       LEDS[1]=1;    
    }else{
      digitalWrite(LED1,LOW);
      LEDS[1]=0;
    }

    if (serialData == '2' && LEDS[2]==0){
       digitalWrite(LED2,HIGH);
       LEDS[2]=1;    
    }else{
      digitalWrite(LED2,LOW);
      LEDS[2]=0;
    }

     if (serialData == '3' && LEDS[3]==0){
       digitalWrite(LED3,HIGH);
       LEDS[3]=1;    
    }else{
      digitalWrite(LED3,LOW);
      LEDS[3]=0;
    }


      if (serialData == '4' && LEDS[4]==0){
       digitalWrite(LED4,HIGH);
       LEDS[4]=1;    
    }else{
      digitalWrite(LED4,LOW);
      LEDS[4]=0;
    }
        
    // Serial.println(serialData);  // return data
    Serial.println("hello world");
  }  
}

 

 


ReplyQuote
Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 

@kirk 

First - I looked at your OP and scanned your thread.  I don't see where you have told us what microprocessor you're using.  Is it an Arduino or some other beast.  Which version?

I would ask you to back up and let's start by using some example code.  We (being us on the forum) can't see your rig, and see how you are testing it.  We don't know if its the sensor or wiring or what...

Anyway, here is the example directly out of the Arduino IDE.  The ONLY change I have made is to change the number of stepsPerRevolution to your stepper motor.

I know both on this forum and on the Internet, that it says you must swap the "9" and "10" parameters.  Try it both ways and see if you successfully get a FULL turn CW and then CCW.

Report back.

Thanks for the back story.  Sounds cool!  Would definitely like to see your progress after you get over this hurdle.

VBR,

Inq

#include <Stepper.h>

const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

 

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


ReplyQuote
Kirk
 Kirk
(@kirk)
Eminent Member
Joined: 12 months ago
Posts: 26
Topic starter  

@Inq

I have tried your program and  used it with a new motor of the same type and new driver board of the same type. I still have no  rotation. I rechecked the wiring from the UNO to the driver board and found a broken pin going into  the UNO digital output #11. I tested the UNO with the BLINK sample program and it appeared normal.  I checked the inputs to the Driver with a voltmeter and they varied between 2.4v and 0.0. Really puzzled here....  


ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3046
 

@kirk Try another board.

It is considered poor judgement to traverse a chasm in 2 leaps.


ReplyQuote
Inq
 Inq
(@inq)
Prominent Member
Joined: 6 months ago
Posts: 891
 
Posted by: @kirk

@Inq

I have tried your program and  used it with a new motor of the same type and new driver board of the same type. I still have no  rotation. I rechecked the wiring from the UNO to the driver board and found a broken pin going into  the UNO digital output #11. I tested the UNO with the BLINK sample program and it appeared normal.  I checked the inputs to the Driver with a voltmeter and they varied between 2.4v and 0.0. Really puzzled here....  

OK... I must have taken pity on you, cause I rummaged through my junk stuff to find an old Uno and hooked it up.  I needed to change two lines in the code to finally get it to work.

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);  // It does require the swap the pins in the call to the stepper constructor.

myStepper.setSpeed(20);  // It could not tolerate the high speed of 60.  20 worked, 30 didn't!

Double check you wiring also.  They should look something like this.

UnoStepper

This all worked.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


ReplyQuote
Kirk
 Kirk
(@kirk)
Eminent Member
Joined: 12 months ago
Posts: 26
Topic starter  

@Inq

At this point I think the problem is wiring but I did order another Arduino Uno just for comparison. My project led me to create an intermediary board to make connections to drivers (more than one). This has probably complicated things in that there are more chances for something to go wrong. Do you know a good source for flat  4 strand cable?  I want to connect straight from the Arduino digital output port to the driver.

I did want to clear up some terminology.  The programs I have used define 3 statements:

1. const float STEPS_PER_REV = 32

2. const_float GEAR_RED = 64

3. const_float STEPS_PER_OUT_REV =STEPS_PER_REV*GEAR_RED    //2048

In earlier programs I haven't had problems with that form, but....

Again, thanks for all of your assistance. I will keep you updated on any progress

     Kirk Rensmeyer


ReplyQuote
Page 2 / 3