/* Program 10-1 DC Motor Control * * H-bridge driver TB6612FNG consists of four half-bridge circuit. * A half-bridge output can be high or low. * When M1 output is high and M2 output is low, the current flows through * the motor from M1 to M2. When M1 is low and M2 is high, the current * flows through the motor from M2 to M1 and the DC motor will reverse * the direction. When both of them are high or both of them are low, * there is not current flowing through the motor and the motor stops. * * The PWM pins controls the power applied to the motor. In this program, * the PWM pins are kept on at 100%. */ const int M1 = 2; /* motor control pins */ const int M2 = 3; const int PWM12 = 5; /* PWM power control for M1 and M2 */ void setup() { /* make motor control pins output */ pinMode(M1, OUTPUT); pinMode(M2, OUTPUT); /* enable motor output with PWM at 100% */ pinMode(PWM12, OUTPUT); digitalWrite(PWM12, HIGH); } void loop() { /* current flows from M1 to M2 */ digitalWrite(M1, HIGH); digitalWrite(M2, LOW); delay(1000); /* current flow stops */ digitalWrite(M1, HIGH); digitalWrite(M2, HIGH); delay(500); /* current flows from M2 to M1 */ digitalWrite(M1, LOW); digitalWrite(M2, HIGH); delay(1000); /* current flow stops */ digitalWrite(M1, HIGH); digitalWrite(M2, HIGH); delay(500); }