Sunday 4 September 2011

Arduino and PWM for model trains...

Noisy!
You may have noticed from my videos that the trains on my test layout make a hideous high pitched whine. This is due to the PWM (pulse width modulation) frequency used by the Arduino analogWrite function.


Solved
If you call this function that I found in the Arduino Playground, all is fixed: http://www.arduino.cc/playground/Code/PwmFrequency

Note that the default divisor for pins 5 & 6 is 64 so the timers need to be adjusted to cope with the change. 

Choosing 1024 gave the best result so I had to divide all timer functions by 1024/64 = 16.

No comments:

Post a Comment