Support » Pololu Dual G2 High-Power Motor Driver Shields for Arduino User’s Guide »
5. Programming your Arduino
Our Arduino library for the Dual G2 High-Power Motor Driver Shield makes it easy to get started writing your Arduino sketches. A link to download the library, installation instructions, and the library command reference can be found on the library’s github page. Once installed, we recommend you try out the example sketch by selecting
File > Examples > DualG2HighPowerMotorShield > Demo
from the Arduino IDE, or by copying the following code into a new sketch (and make sure to uncomment the line at the top that corresponds to the version of your motor driver shield):
#include "DualG2HighPowerMotorShield.h" // Uncomment the version corresponding with the version of your shield. DualG2HighPowerMotorShield24v14 md; // DualG2HighPowerMotorShield18v18 md; // DualG2HighPowerMotorShield24v18 md; // DualG2HighPowerMotorShield18v22 md; void stopIfFault() { if (md.getM1Fault()) { md.disableDrivers(); delay(1); Serial.println("M1 fault"); while (1); } if (md.getM2Fault()) { md.disableDrivers(); delay(1); Serial.println("M2 fault"); while (1); } } void setup() { Serial.begin(115200); Serial.println("Dual G2 High Power Motor Shield"); md.init(); md.calibrateCurrentOffsets(); delay(10); // Uncomment to flip a motor's direction: //md.flipM1(true); //md.flipM2(true); } void loop() { md.enableDrivers(); delay(1); // The drivers require a maximum of 1ms to elapse when brought out of sleep mode. for (int i = 0; i <= 400; i++) { md.setM1Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M1 current: "); Serial.println(md.getM1CurrentMilliamps()); } delay(2); } for (int i = 400; i >= -400; i--) { md.setM1Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M1 current: "); Serial.println(md.getM1CurrentMilliamps()); } delay(2); } for (int i = -400; i <= 0; i++) { md.setM1Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M1 current: "); Serial.println(md.getM1CurrentMilliamps()); } delay(2); } for (int i = 0; i <= 400; i++) { md.setM2Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M2 current: "); Serial.println(md.getM2CurrentMilliamps()); } delay(2); } for (int i = 400; i >= -400; i--) { md.setM2Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M2 current: "); Serial.println(md.getM2CurrentMilliamps()); } delay(2); } for (int i = -400; i <= 0; i++) { md.setM2Speed(i); stopIfFault(); if (i%200 == 100) { Serial.print("M2 current: "); Serial.println(md.getM2CurrentMilliamps()); } delay(2); } md.disableDrivers(); // Put the MOSFET drivers into sleep mode. delay(500); }
This example enables the drivers and ramps motor 1 from stopped to full speed forward, ramps down to full speed reverse, and back to stopped. Then, it does the same with the other motor. After motor 2 is stopped, sleep mode is entered for 500 ms before the demo enables the drivers again. Current readings for each motor are sent over serial and can be seen with the serial monitor. If a fault is detected, a message is sent over serial.
Note: Even if you don’t have any motors yet, you can still try out this sketch and use the motor indicator LEDs for feedback that the shield is working properly.