# How to make stepper motor rotate with angle of sine wave?

Hi, everyone. my English is not good, hope you can understand what i mean.

I use easydriver to control my stepper motor
(http://danthompsonsblog.blogspot.tw/2008/09/easydriver-v31-tutorial.html)
and i have roughly known how to make the stepper motor rotate with the pwm signal,
but it only can rotate with a particular speed.
(theta = w*t or theta’=w)

My target is that use stepper motor to force a torsion and cause it to force oscillation.
so i need that the rotated angle of motor vary with sine wave,
and can adjust rotation amplitude and angle.
(theta = Acos (wt) , theta= -wasin(wt) )

how do i do that? could you give some suggestion or example? thank you much.

maplefffttt:
and i have roughly known how to make the stepper motor rotate with the pwm signal,

This is worrying. Stepper motors don’t use PWM.

Have a look at the links below.

And please use the code button </> `so your code looks like this` and is easy to copy to a text editor

Do you want the motor to move with a speed that is proportionate to the sine of the angle, or do you want it to move to a position that is proportionate to the sine of the angle ?

When you say you want to “force a torsion” I wonder if a DC motor might be more suitable?
Please describe the project you are trying to implement.

First calculate position as a function of time, then have the actual position track this by stepping whenever the actual position is out by 1 or more. The actual position variable is changed when you output a step.

Robin2:
This is worrying. Stepper motors don’t use PWM.
Have a look at the links below.

i mean the signal i give to easydriver, i don’t really know its principle clearly,
but i think it is similar to the paragraph “Arduino Pulse Width Modulation (PWM)” in your article.
i only need to give the step and direction signal.
this code is copied from the link in my article.
i am using the modified version of this.

``````////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver 3.1
////////////////////////////////////////////////////////

// Dan Thompson 2008
//
// Inpired by the code and chat on this thread.
// http://forum.sparkfun.com/viewtopic.php?t=10378&highlight=easydriver
//
// Use this code at your own risk.
// For all the product details visit http://greta.dhs.org/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/
//

int dirpin = 3;
int steppin = 12;

void setup() {
Serial.begin(9600);

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}
void loop()
{

int i;

digitalWrite(dirpin, LOW);     // Set the direction.
delay(100);

Serial.println(">>");
for (i = 0; i<4000; i++)       // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(200);      // This delay time is close to top speed for this
}                              // particular motor. Any faster the motor stalls.

digitalWrite(dirpin, HIGH);    // Change direction.
delay(100);

Serial.println("<<");
for (i = 0; i<4000; i++)       // Iterate for 4000 microsteps
{
digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(200);      // This delay time is close to top speed for this
}                              // particular motor. Any faster the motor stalls.

}
``````

Robin2:
And please use the code button </>

``````so your code looks like this
``````

and is easy to copy to a text editor

Do you want the motor to move with a speed that is proportionate to the sine of the angle, or do you want it to move to a position that is proportionate to the sine of the angle ?

i think this is same, because if the function of angle varying with time is sine,
the function of angle speed varying with time is cosine.

Robin2:
When you say you want to “force a torsion” I wonder if a DC motor might be more suitable?

in fact, first i have the opinion same as you, but it’s the ask of my teacher.
and TAs told me that the DC motor is not easy to control its position.

Robin2:
Please describe the project you are trying to implement.

my project is to detect some factor such as damping coefficient in the motion of torsion

when you rotate the torsion a particular angle, obviously you will feel a restoring torque
and the torque is proportional to the angle in a suitable range.
so we can write down as I(theta)’’+k(theta)=0
theta = the angle of the torsion comparing with the balance position.
this is similar to S.H.M, so the torsion will rotate with clockwise direction, counterclockwise direction,…
and in reality, there is a damping torque which is proportional to angle speed in low speed.
the equation become as I(theta)’’+c(theta)+k(theta)=0
so the amplitude will decreasing in exponent way.
we have done the above, then we can fitting the point to find what we want to know.
you can see that in here

then, we want to observe the motion if we give a torque to the torsion periodical.
and we know any periodical function can be decompose as many sine function.
so we just focus on the torque varying with sine to time.
I(theta)’’+c(theta)+k(theta)=A*sin(wt)
and we know if the angle varying with time is sine,
then angle speed, angle acceleration and torque should be sine or cosine.

in fact, if i use the code above, i can observe that the amplitude will increase, decrease periodically.
it seem be a wave pack which superpose by many sine,
so i think it should can work, if i can make the angle varying with time is sine.

I find it almost impossible to read your Reply #3 because of the unnecessary white space. My brain cannot get all those bits that look like separate paragraphs to join together as a sentence. Please modify your Reply and make it readable.

Do you want the motor to move with a speed that is proportionate to the sine of the angle, or do you want it to move to a position that is proportionate to the sine of the angle ?

i think this is same, because if the function of angle varying with time is sine, the function of angle speed varying with time is cosine.

I suspect your maths is very much better than mine because I would not think of expressing the issue like you have.

However I think you have misunderstood my question (or I was not sufficiently clear). I have one notion in my head in which the motor runs continuously but with a varying speed. And I have another notion in which the motor moves forwards and backwards within a single revolution with its position following a sine pattern.

If the motor is running continuously there will be an interval between steps. It would be easy to change that interval by adding or subtracting an amount that varies according to a sine function.

If the motor is to be positioned according to a sine function then you need to relate the number of steps from the zero position to the value of the sine function.

…R

Do you want the angle of the motor shaft to be proportional to sin(wt)?

Yes, its been said many times!

maplefffttt have you read my replay #2? I've told you how to approach this.

Yes, its been said many times!

Not by the OP, in a way that makes the intent perfectly clear.

MarkT:
Yes, its been said many times!

maplefffttt have you read my replay #2? I’ve told you how to approach this.

Yes, i have read this and code an embryo.
but i have to wait for tomorrow to test and find the problem.
this is the code.

``````double step_angle=; // rad angle of one step of stepper motor;
int step=0;   // the step which stepper motor have rotated;
double angle_now=0; // the angle which the stepper motor have rotated now;
double angle_should=0; // the angle which the stepper motor should be;
double angle_should_before=0; // record the last sine value to detect the change of direction;

int dirpin=;
int steppin=;

double ff=;
double ww=2*3.1415926*ff;

void setup()
{
Serial.begin(9600);

pinMode(dirpin,OUTPUT);
pinMode(steppin,OUTPUT);
}
void loop()
{
double tt= (double) millis()/1000;
angle_should=amplitude_angle*sin(ww*tt);

if(angle_should>angle_should_before)
{
digitalWrite(dirpin,HIGH);

while(angle_now<angle_should)
{
digitalWrite(steppin,LOW);
digitalWrite(steppin,HIGH);
delay(200);
step +=1;
angle_now=step_angle*step;
}
angle_should_before=angle_should;

}
else
{
digitalWrite(dirpin,LOW);

while(angle_now>angle_should)
{
digitalWrite(steppin,LOW);
digitalWrite(steppin,HIGH);
delay(200);
step -=1;
angle_now=step_angle*step;
}
angle_should_before=angle_should;
}

}
``````

Robin2: I find it almost impossible to read your Reply #3 because of the unnecessary white space. My brain cannot get all those bits that look like separate paragraphs to join together as a sentence. Please modify your Reply and make it readable.

i think this is same, because if the function of angle varying with time is sine, the function of angle speed varying with time is cosine.

I suspect your maths is very much better than mine because I would not think of expressing the issue like you have.

However I think you have misunderstood my question (or I was not sufficiently clear). I have one notion in my head in which the motor runs continuously but with a varying speed. And I have another notion in which the motor moves forwards and backwards within a single revolution with its position following a sine pattern.

If the motor is running continuously there will be an interval between steps. It would be easy to change that interval by adding or subtracting an amount that varies according to a sine function.

If the motor is to be positioned according to a sine function then you need to relate the number of steps from the zero position to the value of the sine function.

...R

yes, i think i have a basic concept to what i should do.

jremington: Do you want the [u]angle[/u] of the motor shaft to be proportional to sin(wt)?

yes, but more clearly, what i need is that the torque be proportional to sin(wt) but if the angle= Asin(wt),then the angle speed = wAcos(wt) and acceleration of angle =-aw^2sin(wt)

and torque = Moment of inertia * acceleration of angle=-Iaw^2sin(wt) this is my need. so the angle to be proportional to sin(wt) is same as torque to be proportional to sin(wt)

yes, but more clearly, what i need is that the torque be proportional to sin(wt)

You can't do that with a stepper motor. You could with a galvanometer.

jremington: You can't do that with a stepper motor. You could with a galvanometer.

could you explain the reason more clearly?

is there worry in my inference?

Motor torque is proportional to the current through the motor windings.
With a stepping motor, the current is not under program control.

jremington: Motor torque is proportional to the current through the motor windings. With a stepping motor, the current is not under program control.

Which is why I suggested a DC motor in Reply #1

...R

jremington:
Motor torque is proportional to the current through the motor windings.
With a stepping motor, the current is not under program control.

I think that is a concept of mean value.
No matter how the current and torque vary with time instantaneously.
If its path of the angle is controlled to conform to the sine.
So the torque should be proportional to sine in time scale of second from the discussion in my reply.
It is similar to the power of a AC machine can be calculated by the rms voltage it use in the scale of second.

There is no reason that a motor should conform to any arbitrary mathematical model.

In the first place, it is impossible to have the angular position of a stepping motor shaft actually conform to a sine function of time, because [u]the shaft moves by steps, that have an acceleration and velocity profile.[/u]

In turn, the velocity and acceleration profile of each step the motor takes depends in a complicated way on the power supply and the motor driver you are using.

maplefffttt: So the torque should be proportional to sine in time scale of second from the discussion in my reply. It is similar to the power of a AC machine can be calculated by the rms voltage it use in the scale of second.

Can you post a diagram of the machine you are trying to use the stepper motor in. Then the stuff you are saying might make sense.

As far as I can see you have still not told us if this machine rotates continuously (at what speed?) and needs to develop controlled variations in torque while moving. I have not been able to visualize such a machine. Any machine I can conceive uses torque to cause an acceleration or deceleration (for example a car driving on the road).

Or whether it needs to develop variations in torque against a stationary load.

Is this an XY Problem ?

...R

Have you read the posting? Of course it doesn't rotate continuously if its oscillating!!!!

MarkT: Have you read the posting? Of course it doesn't rotate continuously if its oscillating!!!!

I don't know who this is addressed to, but since it follows my Reply #17 there is a possibility that I am the target.

I have to say I have not seen sufficient evidence in the OP's Posts to conclude that the motor is oscillating within a range of less than 360 degrees rather than rotating at varying speeds. In his very first Post he says " it only can rotate with a particular speed" from which I have inferred that he probably wants it to rotate at varying speeds.

...R