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
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.
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
Still don't know why you ever want to control the speed of a motor that way... : 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 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
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.
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.
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.
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.