Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

PWM resolution on dual-mc33926 Motor Shiled on Arduino



i have the dual-mc33926 Motor Shield I have some troubles to unterstand some pwm stuff. I’m using a PID controller to control the both motors, but i need a higher pwm resolution. I’m using a Arduino Mega with an Atmega2560.
Which parameters I have to change in the library?

In the code below, I’ve added some gcc tags like AVR_ATmega1280 and 2560, changed ICR3 to 1023 (10 Bits) and the speedlimits to 1023. Is it enought or do I forget something?

#if defined(__AVR_ATmega1280__)|| defined(__AVR_ATmega2560__)|| defined(__AVR_ATmega328P__)
  // Timer 1 configuration
  // prescaler: clockI/O / 1
  // outputs enabled
  // phase-correct PWM
  // top of 400
  // PWM frequency calculation
  // 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
 TCCR3A = _BV(COM3A1)|_BV(COM3B1) | _BV(COM3C1);
  TCCR3B = _BV(WGM33) | _BV(CS30);

  ICR3 = 1023;
// Set speed for motor 1, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM1Speed(int speed)
  unsigned char reverse = 0;
  if (speed < 0)
    speed = -speed; // Make speed a positive quantity
    reverse = 1; // Preserve the direction
  if (speed > 1023) // Max PWM dutycycle
    speed = 1023;
 #if defined(__AVR_ATmega1280__)|| defined(__AVR_ATmega2560__)||defined(__AVR_ATmega328P__)
  OCR1A = speed;
  analogWrite(_M1PWM,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
  if (reverse)



I suggest looking at this post, which shows how to modify the Arduino library for the VNH5019 shield to use 20kHz PWM signals on the Arduino Mega. The VNH5019 and MC33926 motor driver shields have similar Arduino connections, so you might try applying those modifications to the Arduino library for the Pololu Dual MC33926 Motor Driver Shield.

- Amanda


Thanks for the answer. It’s not so important, that I have 20 kHz PWM signals. I need only more steps for the speed limit for example 1023 steps but with 7,8 kHz. I use the timer 3 and the pins 2 and 3. So I have adapted the library and I hope the right way, because I am little bit afraid, that I can damage the motors or the arduino.
DualMC33926MotorShield.cpp (3.7 KB)
DualMC33926MotorShield.h (1.2 KB)



Hi, Michael.

Your modifications to the DualMC33926MotorShield library (in your recent post) looks mostly fine; I noticed you included ATmega328P in your #ifdef statements. The ATmega328P does not have a Timer 3, so you will not be able to compile the library for an Arduino board using that AVR chip. I suggest removing that from the statement just to keep the code clean.

- Amanda