analogwrite and sine and newbie

I'm slowly learning to programm Arduino. Now I'm trying to controll
DC motor with analogWrite simply this way
analogWrite(motorPin, 60);
I would like to use Sine as value for analogwrite instead of directly writing
a value, 60 in above example.
I would like to define amplitude and frequency of sine to determine
the analogwrite value.

I hope that I explained my problem clearly
and that someone could give me advise how to doit

regards
Mario

Why do you want a sine? Motor speed is proportional to the voltage so by using a sine you just make the speed non-linear...

And frequency can be done. Just Google about changing the frequency of the PWM. In a nutshell, if you want to keep the same resolution then you only have a limited amount of possible frequencies.

yes that exactly what I want nonlinear change of
speed based on the waveform of sine

novio8:
speed based on the waveform of sine

Based on what waveform? What will be the input? And why non-linear?

Waveform is sine, that is triggered or started by a switch,
And as sine goes from zero to max and back the speed
of a motor will change accordantly.
I imagane that there is a sine function, but I'm lost
where to began

Could you maybe use the sin() function?

Still don't know why you ever want to control the speed of a motor that way... ::slight_smile: Increase spinning fast, have a lot of speed steps at high speed, increase fast again and change direction quick...

But yeah, the Arduino has a build in sine function BUT it's slow. It's like calculating the sin without using the sin on your calculator. Faster option is to make a look up table which you fill with the pre-calculated sine or any other waveform.

yes, but how to put this function in to
analogWrite(motorPin, 60);
instead of in my case 60 and the sinewave
is triggered by a switch
I don'have any programing experience, so my
questions may look stupid

novio8:
yes, but how to put this function in to
analogWrite(motorPin, 60);

analogWrite(motorPin, sin(60));

But the problem stays the same, you need to write a program to change the value (with or without sin) because otherwise it will not change. To do that:

novio8:
I don'have any programing experience, so my

Then time to get some :wink: It's a forum to learn but you have to put effort in it first. And if you don't want to, fine, we have a forum section for that as well where someone else can write it for you. But they will expect legal tender in return :slight_smile:

I already wrote one program to drive a motor with several
end switches and I did put a lot of effort in it.
I ask questions to learn more.

I don'have any programing experience, so my questions may look stupid

Your questions do not make any sense.
The analogue write produces a PWM signal, see this:- http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Now the PWM is a rectangular wave, what controls the speed of the motor is the duty cycle of this waveform. The frequency does not affect the speed at all.

It is not clear if you are asking 1) how to change the duty cycle according to a sin function, or 2) you want a sin function out of the PWM instead of the rectangular wave.

  1. Simply apply the number you want to a sin function before mapping it to the analogue output, however this does nothing to change the speed of the motor that putting the required number into analogWrite would not do.

  2. If you did that then there would be no difference in the speed control you have with a rectangular wave. You still get the effective voltage of the average of the waveform.

So you can see it is a bit of a none question.

novio8:
I ask questions to learn more.

And we ask questions back to try and understand the problem :wink:

yes the duty cycle will change based on sine wave.

Maybe a a look up table is a better way.

That is what happens when one doesn't think like a programmer.

oh, and thanks to every one
Members of this Forum reponse Quickly and Quality
is excellent

septillion:
analogWrite(motorPin, sin(60));

Just remember that the sin() function use radians, not degrees.

Also, you are using a DC motor and it's speed is correctly controlled using PWM - which results in a linear variable speed - due to the change in the average voltage being applied.

The DC motor will not respond well to a non-linear signal generated by the sin() function which is something from the world of AC, not the DC world. In fact, if the motor has any load on it, i doubt it may even rotate, most likely it will sit there, stalled and groaning from a lack of torque.

It makes no sense whatsoever to those that understand motor speed control. Since you have not explained why you think you need a sine input to PWM, we're all left wondering what the heck you could possibly want to do.

Typical X-Y Problem