/* Program 10-2 Stepper motor control * * The H-bridge driver TB6612FNG consists of four half-bridge circuit. * A bipolar stepper motor should have one winding connected * across M1-M2 terminals and the other winding M3-M4. It works with * a unipolar stepper motor as well. * The PWM pins controls the power applied to the motor. In this program, * the PWM pins are on at 100%. * This program drives the stepper motor with halfsteps. */ const int motor[] = {2, 3, 7, 8}; /* motor control pins */ const int PWM12 = 5; /* PWM power control for M1 and M2 */ const int PWM34 = 6; /* PWM power control for M3 and M4 */ void setup() { /* make motor control pins output */ for (int i = 0; i < 4; i++) pinMode(motor[i], OUTPUT); /* enable motor output with PWM at 100% */ pinMode(PWM34, OUTPUT); pinMode(PWM12, OUTPUT); digitalWrite(PWM34, HIGH); digitalWrite(PWM12, HIGH); } void loop() { const char halfsteps[ ] = {0x9, 0x1, 0x5, 0x4, 0x6, 0x2, 0xa, 0x8}; static int i = 0; digitalGroupWrite(motor, 4, halfsteps[i++ & 7]); delay(50); } /* write to a group of output pins */ void digitalGroupWrite(const int* group, int length, int data) { for (int i = 0; i < length; i++) digitalWrite(group[i], data & (1 << i) ? HIGH : LOW); }