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