Fast PWM causing some problems

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):

      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 :slight_smile:
Andrew

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?

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.

andyjjones:
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?

completely bonkers

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

andyjjones:

        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.

AWOL:

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:

//    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.