Forum >Setting frequency of PWM
ArduinoGeneral

Setting frequency of PWM

userHead Jmu 2013-10-10 02:07:25 5616 Views6 Replies
Hi,

I use the DFRobot Motor Shield to control a model train and that works fine. But the motor has a problem to start and keeps making a humming sound. So I want to change the frequency of the PWM signal to find out if this would be a solution for my problems.
Of course I read the Wiki and other documentation, but it's not clear for me how to do this. Could someone please help me with some information, or sketch to experimate?

Thanks for your help!!
2013-10-15 18:48:32 Hi, Jmu
When the motor begin to start from stationary state, it need a current named"starting current". It will make voltage larger than Rated voltage.
As your words, 40% is the starting current.
The sound before starting is made by current going through the motor.
At the moment, the voltage is to o low to move the motor. and the motor does not work ?it is called Locked-Rotor. If you keep this status for a long time, it will damage the motor and the battery.
It is a normal  phenomenon?Don't worry about this.
userHeadPic Grey.CC
2013-10-15 08:41:13 Hi Grey,

Thanks again for your reply. This is interesting stuff... I will try to understand it and if it's useful for me.
The problems that I have with the start is that the motor needs an initial power to start. Let's say it starts at 40% of the power. But when slowing down, I can go as low as 25% before the train stops. I think the motor needs a "kick" to get started. Or perhaps another frequency will do the job.

The other problem is that the motor makes some sound before starting to run (when M1 is HIGH).

Joop
userHeadPic Jmu
2013-10-13 00:26:15 You can try this command
setPwmFrequency(9, 8 );  //Set pin 9's PWM frequency to 3906 Hz (31250/8 = 3906)
"9" is pin number.and "8" is divisor. and the base frequency for pins 3, 9, 10, and 11 is 31250 Hz

setPwmFrequency(6, 1);// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
                                      //the base frequency for pins 5 and 6 is 62500 Hz

setPwmFrequency(10, 1024);// Set pin 10's PWM frequency to 31 Hz (31250/1024 = 31)

The resulting frequency is equal to the base frequency divided by the given divisor:
Two base frequencies:
* The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* The base frequency for pins 5 and 6 is 62500 Hz.
Divisors:
* The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64, 256, and 1024.
* The divisors available on pins 3 and 11 are: 1, 8, 32, 64,128, 256, and 1024.

PWM frequencies are tied together in pairs of pins. If one in a pair is changed, the other is also changed to match:
* Pins 5 and 6 are paired on timer0
* Pins 9 and 10 are paired on timer1
* Pins 3 and 11 are paired on timer2

Note that this function will have side effects on anything elsethat uses timers:
* Changes on pins 3, 5, 6, or 11 may cause the delay() and millis() functions to stop working. Other timing-related functions may also be affected.
* Changes on pins 9 or 10 will cause the Servo library to function incorrectly.

Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).
Divides a given PWM pin frequency by a divisor.
userHeadPic Grey.CC
2013-10-13 00:22:33 Hi, Jmu
What 's the problem with the start? Does it work?
You know when the motor with load, it will make a little noisy. it is fine. Could you describe your problem in detail? whether the motor was joggling when you controlled your model train?And Is the humming sound too large?
userHeadPic Grey.CC
2013-10-12 02:06:40 Hi Grey,

Thanx for your reply. But that was not my question. I already have written a similar sketch to control the speed. But now I want to experiment with changing the frequency of the PWM. I want to find out if another freq of the pulses results in a better behaviour of the motor. Now the freq is 1 kHz (pin 5 & 6) and that results in a humming sound in the motor. What happens at, let's say, 8 kHz? But how can I adjust the freq and what is the best way?

Also, is it possible to use Pin 9 & 10 as output?

Joop
userHeadPic Jmu
2013-10-10 19:21:47
Hello, Jmu
PWM output is depended on the value you give it.
It is a simple code to control it.

//Arduino PWM Speed Control?
int E1 = 5; 
int M1 = 4;
int E2 = 6;                     
int M2 = 7;                       

void setup()
{
    pinMode(M1, OUTPUT); 
    pinMode(M2, OUTPUT);
}

void loop()
{
  int value;
  digitalWrite(M1,HIGH); 
  digitalWrite(M2, HIGH);     
  analogWrite(E1, value);  //PWM Speed Control
  analogWrite(E2, value);  //PWM Speed Control
  delay(30);
}

When m1&m2 are set high together, it means Move forward
low  together means move backward
and one is high  the other is low  means turn left or right.
The value is the speed of the motors Its range is 0 to 255
255 is the fast one. you can try input the value directly.
Here is a simple code about Arduino how to control the PWM.
[url=https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225)]https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225)[/url]

I hope it will help you.
userHeadPic Grey.CC