Modified VNH5019 shield library for 20kHz PWM with Mega

Hi there,
I wonder why not just to change the frequency of pin 9 & 10 to match the UNO?
We added the following:

//set Arduino Mega pin 9 & 10 to frequ 31250.
TCCR2B = TCCR2B & 0b11111000 | 0x01;

it seems to work just as expected without the howling of the motors. As I’m not an advanced register-haxxor I would be glad if anybody could explain potential objections with that?
thxalot,
david

Hi, David.

Thank you for sharing! I tried adding your line of code to our unmodified VNH5019 library and it did change the PWM frequency. I had not thought of doing something so simple to change the default PWM frequency of certain pins. Unfortunately, just setting the prescaler for Timer2 to zero changes the frequency of analogWrite to around 30kHz, which is faster than the maximum PWM frequency specified for the VNH5019.

I modified our library to use Timer1 because it is a 16-bit timer and easy to get 20 kHz (the fastest PWM frequency the VNH5019 can do) with. If you wanted to use Timer2 (which is 8-bit), you could use fast PWM mode, a prescaler of 8, and a top value of 100 in order to get 20 kHz. However, that would only let you have 100 different motor speeds.

-Claire

Hello Claire,

I have bought the motor driver to use to roll up my greenhouse walls. Unfortunately, although i can get motors working, I have no idea what the difference in hertz means for the motors. Will the 500Hz with the mega set up normally with the motor shield actually damage the motor, or will it just sound funny? I don’t care about the sound, but the motors are $300 a piece, and there’s two for each greenhouse, so I wouldn’t want to fry them. What do you think?

Thanks!

Hi.

There shouldn’t be any issues operating our VNH5019 shield at 500Hz. Generally, people like to use higher PWM frequencies (around 20kHz) to eliminate the high pitched whine that lower frequencies make. However, lower PWM frequencies can help reduce switching losses, so if you are fine with the noise, a lower frequency might be better.

By the way, since you are planning to roll up the walls of a greenhouse with your motors, it sounds like they might be pretty powerful. Do you know what their stall current is?

-Claire

Hi Claire,

Sorry I lost track of this for a little while! The motor is found here:

advancingalternatives.com/index. … ts_id=4142

It is a 24v, 3.0 amp motor. It is geared way down so the output is 2.7 rpm, which helps to very slowly open the greenhouse. Do you think this will work? I can’t see stall current on it anywhere.

thanks,

Hello.

I am more concerned with the voltage than the current. While 24 V will not damage the VNH5019, it might trigger the VNH5019’s over-voltage shutdown, which can be as low as 24 V. Do you have the option of running those motors at a lower voltage?

Since you already have those motors, you could connect them directly to your power supply and measure the current they draw while opening the greenhouse.

-Jon

Hi Jon,

The power supply i have for the motor allows voltage adjustments through a screw, and I can go from 22 to 26v. I would worry about frying the motor with not enough volts though. What happens when the overload kicks in on the motor driver?

I hooked up the greenhouse motor and it draws a constant .4 amps, and with me resisting via vice grips I couldn’t get it to go above .5 amps. So I think the current shouldn’t be an issue.

By the way, for this project I purchased 3 of the motor drivers, but I think they are the last revision, which doesn’t map to the UNO rev 3 perfectly. Is there a way to exchange them? They are still in original packaging, but I bought them from Robotshop.

Thanks!

As a little update,

I got the whole thing running on 23v through the motor driver and it seems to be working fine. I ran a program to just run it steadily at ‘400’, which is maximum voltage, and it read exactly 23v, which is what I set the power supply at. The motor runs at around 400 miliamps (from the serial monitor), but the serial monitor indicated that it went up to 8800 miliamps on starting, and that was with no load! Is this going to be a problem once there’s a load on there? I spose the gear reducers might be considered a load by themselves? It reduces to 2.7 rpm from 3000 rpm. . . Any feedback is appreciated!

Supplying a brushed DC motor with less than its rated voltage should not damage it. (If you supply too low of a voltage, the motor will just not turn.) When the overvoltage shutdown feature is triggered, the driver shuts off the supply to the motor.

We would not accept those boards since you did not get them from us. You might check with RobotShop to see if they are willing to offer you an exchange. (Does it actually make a difference for you in your application?)

Motors can draw large amounts of current at startup. Yes, the attached gear train does add additional load to the motor. You might try slowly ramping up your motor speed over time to reduce the amount of current drawn when first turning the motors on.

-Jon

With the original library I remapped pin 10 to pin 3 and pin 12 to pin 5 so that I freed up 10, 11, 12 and 13 for the Ethernet shield. I thought the low frequency PWM noise wouldn’t be a problem but at low speeds its rather loud.

Using the modified library for the Mega, can I remap these pins and still have a 20kHz PWM on Pin 3? If so, please can someone advise how to do it?

I have left PWM1 on pin 9.

Thank you.

Hi.

This modified library uses timer 1 to generate the PWM signal. This timer is only available on pins 11 and 12. If you want to use other pins, you would have to change the library to reference a different timer. Also, pin 3 on the Mega is linked to timer 3 and pin 9 is linked to timer 2, so while you could write code to generate PWM from both of those timers, it would probably not be very practical. You will probably want to choose two pins that use the same timer.

-Claire

Hi Claire,

Thanks for the reply.
The only PWM pins available are 45 and 46 on Timer 5, and Id prefer not to run wires down to them if I can help it. I would do so only as a last resort. I don’t really understand why the use of two timers is not practical?

I currently have pin 2 as M1INA, pin 3 is M2PWM and Pin 5 is M2EN. M1PWM is pin 9. So, if I swap M2EN from pin 5 to 9, and put M1PWM on pin 9, I should just be able to use Timer 3 at 20kHz. Please can you tell me if that’s right? Is it set up the same way as Timer 1?

Thank you.

Sorry, I forgot to mention, I previously remapped M2EN from pin 12 to Pin 5.

So the new mapping that I propose is as follows:

Pin 2 M1INA no change
Pin 3 M1PWM remapped from pin 9
Pin 4 M1INB no change
Pin 5 M2PWM remapped from pin 10
Pin 6 M1EN no change
Pin 7 M2INA no change
Pin 8 M2INB no change
Pin 9 M2EN remapped from pin 12

So now, both PWM lines to control the motors are on Timer3, and pins 10 to 13 are free for the Ethernet shield.

For Timer 3, Do I keep the OCR statements as they are or do they need changing if using pins 3 and 5 for 20 kHz PWM on the Mega? I’m getting quite confused with setting up the correct frequency on the appropriate pins.

As I understand it, OCR3C is for the register for pin 3, and OCR3A is for pin 5. Since I’m using these pins instead of 11 and 12, do I just change the OCRxx in the .cpp file for the parts that deal with motor 1 and 2?

Also, Are the following TCCRxx correct for the lines below defines statement for the Mega 1280/2560 :

TCCR3A = 0b1010100;
RCCR3B = 0b0001001;

Ive attached the modifications to the library. The .h file has the remappings and the .cpp file has modifications to remap the EN2DIAG2 from pin 5 to pin 9 because I want the two PWM pins to be 3 and 5. The TCCxx lines are set to the above two assignments. I changed SetM1Speed OCRxx to OCR3C for Timer 3 on pin 3. I wont change anything else until I get motor 1 work correctly.
DualVNH5019MotorShieldMegaMYMODS.zip (3.24 KB)

The pin mapping you suggested seems fine. The reason I said using separate timers to generate the PWM signals is probably not practical is because then you would be using 2 timers when you could just use one. Even on the Mega, timers can often be in short supply, so you might run into trouble later if you used two.

I took a quick look at the modified library files you posted, and noticed that you forgot to change a few instances of OCR1A and OCR1B to OCR3A and OCR3B. You also did not change ICR1 to ICR3. Other than that I think you should be able to run the motors off timer 3 with your library.

-Claire

Many Thanks for the explaination. I’ll give that a try.

For pin 3, I used OCR3C, not OCR3B. I got it to work on Motor1, next to try Motor2 with the demo code. I’m very grateful for your help!

Once its fully working, If anyone else would find it useful. let me know and I’ll attach the library.

Thanks for letting us know you got it working for one motor. I am sure that others would find it really useful if you post your final code.

-Claire

Here is my modified Dual VNH5019 library for the Arduino Mega that does 20 kHz PWM on pins 3 and 5, together with some other remappings so that the Ethernet shield can be used at the same time as the Dual VNH5019 Shield.

Pin mappings

Pin 2 M1INA no change
Pin 3 M1PWM remapped from pin 9 (20kHz)
Pin 4 M1INB no change
Pin 5 M2PWM remapped from pin 10 (20kHz)
Pin 6 M1EN no change
Pin 7 M2INA no change
Pin 8 M2INB no change
Pin 9 M2EN remapped from pin 12

So now, both PWM outputs to control the motors are on Timer3, and pins 10 to 13 are free for the Ethernet shield, as mentioned in a previous post.

I have tested this with Ethernet control and it’s been working fine. Thanks again, Claire for your help to get this done.
DualVNH5019MotorShieldMega.zip (3.61 KB)

Thank you for sharing your modified library, Paul; I am glad you were able to get it to work with your Ethernet shield!

-Brandon