Notifications
Clear all

Stepper motor position sketch  

  RSS

Herbie
(@herbie)
Active Member
Joined: 2 months ago
Posts: 6
2020-08-28 12:18 am  

Hello everyone.

 

Very very new to all this so bear with me.  I'm needing to control stepper motor via an Arduino Nano and stepper motor driver.  I have a Nema 23 stepper motor, a TB6600 driver plus the nano.

I require the motor to turn 270 degrees with the push of a button and stop on its own.  Then have the motor move 270 degrees in reverse when the button is pressed again.

I would also like the ability to stop motor within the 270 degree field by pressing the same button incase of obstruction.

I'm needing help with the coding.  I have looked at a few of the videos on here regarding stepper motors and successfully ran the stepper-test01.ino sketch. 

I am hoping to just use the one button to operate this like it does in stepper-test01.ino but to include the motor to know its home position and end position and not just keep rotating. 

Any help would be apreciated.

 

 


Quote
Herbie
(@herbie)
Active Member
Joined: 2 months ago
Posts: 6
2020-08-28 1:50 am  

This is the sketch I'm currently trialling.  Still need to incorporate buttons to start / stop / reverse

I'm wanting first button press to operate and motor to turn 270 degrees and stop on it own.

As a safety measure in case something was preventing the motor working, a second button press before the motor reaches 270 degrees would stop it.  A third button press would return the motor back to 0 degrees.  

 

 

//defines pins
const int stepPin = 6; //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8; //ENA -Enable

void setup(){
//Sets the pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}

void loop(){
//Enables the motor direction to move
digitalWrite(dirPin,HIGH);
//Makes 3200 Pulses for making one full cycle rotation
for(int x = 0; x < 2400; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}

//One second delay
delay(1000);

//Changes the rotations direction
digitalWrite(dirPin,LOW);
// Makes 3200 pulses for making one full cycle rotation
for(int x = 0; x < 2400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}

//One second delay
delay(1000);
}


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 502
2020-08-28 8:48 am  

 @@herbie

https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

 

//defines pins
const int stepPin = 6; //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8; //ENA -Enable

void setup(){
  //Sets the pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
}

void loop(){
  //Enables the motor direction to move
  digitalWrite(dirPin,HIGH);
  //Makes 3200 Pulses for making one full cycle rotation
  for(int x = 0; x < 2400; x++){
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }

  //One second delay
  delay(1000);

  //Changes the rotations direction
  digitalWrite(dirPin,LOW);
  // Makes 3200 pulses for making one full cycle rotation
  for(int x = 0; x < 2400; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }

  //One second delay
  delay(1000);
}


 

This post was modified 2 months ago by robotBuilder

Herbie liked
ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 502
2020-08-28 7:48 pm  

 

First you need to debounce your button.
https://www.arduino.cc/en/tutorial/switch

With regards to your requirements:

I require the motor to turn 270 degrees with the push of a button and stop on its own. Then have the motor move 270 degrees in reverse when the button is pressed again.

I would also like the ability to stop motor within the 270 degree field by pressing the same button in case of obstruction.

Then you need a variable to hold the current state of the stepper motor.

For example:
stateOfMotor = 0 'meaning at starting point
stateOfMotor = 1 'meaning at end point
stateOfMotor = 2 'meaning it has been stopped in between.

This variable can be used to determine the action you want the push button to take.

You have not mentioned your programming skill level and with Arduino C in particular?


ReplyQuote
Herbie
(@herbie)
Active Member
Joined: 2 months ago
Posts: 6
2020-08-31 2:16 am  

Thank you for replying Robotbuilder.  My programming skill level would be at beginner.  Currently I  tend to find a code that is close to what I want and then begin to learn by modifying small parts and seeing the results.  


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 502
2020-08-31 11:10 pm  

@herbie

It is difficult to give a precise solution as I don't actually have a Nema 23 stepper motor, a TB6600 driver plus the nano to test a coded solution. I only have the Arduino hardware I was able to get from Jaycar and Altronics before the lock down. The only item I have bought online was from Silicon Chip.

 


ReplyQuote
Herbie
(@herbie)
Active Member
Joined: 2 months ago
Posts: 6
2020-09-01 7:12 am  

@robotbuilder I understand but any tips anyone provides helps the learning process so  I appreciate your input regarding the switch setup.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 502
2020-09-01 10:24 am  

@herbie

I assume you have watched Bill's tutorial?

 

This post was modified 2 months ago by robotBuilder

ReplyQuote
noweare
(@noweare)
Active Member
Joined: 6 months ago
Posts: 14
2020-09-02 2:42 am  

You will need another sensor so that the motor can find home, like a limit switch of photo interrupter.

Since its a stepper motor with no feedback then you will have to count steps.

If 3200 pulses is 360 degrees then   x = (270/360)*3200

x will give you the number of pulses for 270 degrees. You can write that as a routine and call it from main.

that way you can get the steps for any angle. 

 

 

 


ReplyQuote
Herbie
(@herbie)
Active Member
Joined: 2 months ago
Posts: 6
2020-09-02 10:25 pm  

@robotbuilder yes I've watched this a few times.  The equipment in this video is pretty much what I'm using.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 502
2020-09-03 1:11 am  

@herbie

So is the current sketch you are trialling actually working?

Have you tried the sketch and circuit from Bill's tutorial where he uses a button to reverse the direction of the motor and a POT to control its speed?

I see Bill didn't bother with key button debounce.

 

 

This post was modified 2 months ago by robotBuilder

ReplyQuote