Hello, FipsJr.
The VNH5019 motor shield connects to digital pins 9 and 10 on the Arduino. On the Uno, these pins correspond to the hardware PWM outputs of Timer 1, the ATmega328's only 16-bit timer. This allows for an ultrasonic PWM frequency. Unfortunately, for some reason, these same pins on the Arduino Mega 2560 correspond to the hardware PWM outputs of Timer
2, which is an 8-bit timer. This seems like a poor pin-assignment choice given that the Arduino Mega 2560 has four 16-bit timers that could have been assigned to those pins instead, including Timer 1, but maybe there are some other constraints I'm not aware of that led to that decision.
You will not be able to get a 20 kHz PWM from an 8-bit timer, so you will either need to live with hearing the PWM-induced whine from your motors, or you can reassign the motor driver shield's PWM pins to the PWM outputs of a 16-bit timer. The shield was designed to somewhat easily allow for customized pin assignments, and the
shield user's guide explains how to do this. I suggest you reassign
MIPWM to Arduino digital pin
11 and
M2PWM to Arduino digital pin
12. You should also reassign
M2EN/DIAG to some unused digital pin because that is assigned to digital pin 12 by default.
If you reassign the driver connections this way, the VNH5019 shield library code should just work after a few small modifications:
1) In DualVNH5019MotorShield::init(), delete or comment out the lines:
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)#endif2) In void DualVNH5019MotorShield::setM1Speed(int speed), delete or comment out the lines:
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)#elseanalogWrite(_PWM1,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255#endifThe only line from that block remaining should be:
OCR1A = speed;3) Do the same thing for DualVNH5019MotorShield::setM2Speed(int speed), except in this case, the only line remaining should be:
OCR1B = speed;4) Do the same thing for the setM1Brake() and setM2Brake methods.
Remember to use the constructor that provides the shield pin mappings so that the library knows about the new location for M1EN/DIAG. Please let me know if you have any questions or run into any trouble getting it to work.
- Ben