Go Down

Topic: Fast PWM causing some problems (Read 692 times) previous topic - next topic

Hey guys,

I noticed when I use Arduino's PWM to control DC motors, I can hear some annoying "singing", and I've been told this is because Arduino's PWM frequency is somewhere between 0-22,000Hz where the human ear can hear it.

I want to change this PWM frequency to something higher, so I looked up the datasheet for the Atmega328P and changed the registers as follows (Fast PWM, No prescaler, PWM frequency around 62kHz):

Code: [Select]
      TCCR1A=TCCR1A&0xA1;
      TCCR1B=TCCR1B&0x09;
      TCCR0A=TCCR0A&0xA3;
      TCCR0B=TCCR0B&0x01;
      TCCR2A=TCCR2A&0xA3;
      TCCR2B=TCCR2B&0x01;



I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore.  Could anyone shed some light onto what could possibly be the matter?

Thankyou :)
Andrew

PaulS

Quote
I noticed when I use Arduino's PWM to control DC motors, I can hear some annoying "singing"

Coming from where? The Arduino? The motors?

Quote
I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore.  Could anyone shed some light onto what could possibly be the matter?

Without seeing the rest of the code? Without knowing what "completely bonkers" means to you? Without knowing what it is supposed to do, and what it does instead? No, I don't think so.

Nick Gammon


I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore. 


Completely bonkers, huh? My code does that sometimes too. Can you be a little bit more specific?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

Quote
completely bonkers

Jerry Lewis comedic bonkers, or Harold Camping barking-mad bonkers?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

davekw7x

#4
May 23, 2011, 02:56 pm Last Edit: May 23, 2011, 04:02 pm by davekw7x Reason: 1

Code: [Select]
       TCCR0B=TCCR0B&0x01;

...bonkers...


Well, bonkers is as bonkers does.

However...

If anything in your code depends on the standard Arduino setup of Timer 0, it won't work if you change the settings of Timer 0.  This includes the functions micros(), millis(), delay(), maybe others.


Regards,

Dave

Footnote:
Normal setup is TCCR0A = 0x03 and TCCR0B = 0x03.  You are changing the Timer 0 prescaler division value from 64 to 1.  

#5
May 24, 2011, 07:00 am Last Edit: May 24, 2011, 07:02 am by andyjjones Reason: 1

Quote
completely bonkers

Jerry Lewis comedic bonkers, or Harold Camping barking-mad bonkers?


Actually more along the lines of Dizzee Rascal - Bonkers.  Sorry to not be more specific.

I copied the these following two lines of code from somewhere else, and they worked:

Code: [Select]
//    TCCR1B=TCCR1B&0xf8|0x01;    // Pin9,Pin10 PWM 31250Hz
//    TCCR2B=TCCR2B&0xf8|0x01;    // Pin3,Pin11 PWM 31250Hz


Which modifies just Timer 1 and Timer 2 prescalers (I think?).  Pin3 and Pin11 are not connected to my motor drivers, so I just used some jumper wires and now it works.

I guess Timer 0 is off limits to change the prescaler.

Go Up