/* Program 11-1 DC motor speed control using PWM * * The H-bridge driver TB6612FNG consists of four half-bridge circuit. * This program uses two half-bridges to control the output of M3-M4 * terminals. A DC motor should be connected between M3 and M4 terminals * of the board. Pin 6 is used to control the pulse-width modulation (PWM) of * the output. The duty cycle of the PWM controls the power applied to the motor. * Pins 7 and 8 control the direction of the half-bridges and in turn control the * direction of the motor. * This program speeds up the motor, slows down the motor, reverse the direction * then repeats. */ const int PWM34 = 6; const int HalfBridgeLeft = 7; const int HalfBridgeRight = 8; void setup() { pinMode(HalfBridgeRight, OUTPUT); pinMode(HalfBridgeLeft, OUTPUT); } void loop() { /* change motor direction */ digitalWrite(HalfBridgeRight, HIGH); digitalWrite(HalfBridgeLeft, LOW); /* ramp up the pulse width */ for(int pulseWidth = 0 ; pulseWidth <= 255; pulseWidth +=5) { analogWrite(PWM34, pulseWidth); /* set pulse width */ delay(100); /* pause before reverse */ } /* ramp down the pulse width */ for(int pulseWidth = 255 ; pulseWidth >= 0; pulseWidth -=5) { analogWrite(PWM34, pulseWidth); /* set pulse width */ delay(100); /* pause before reverse */ } /* change motor direction */ digitalWrite(HalfBridgeRight, LOW); digitalWrite(HalfBridgeLeft, HIGH); /* ramp up the pulse width */ for(int pulseWidth = 0 ; pulseWidth <= 255; pulseWidth +=5) { analogWrite(PWM34, pulseWidth); /* set pulse width */ delay(100); /* pause before reverse */ } /* ramp down the pulse width */ for(int pulseWidth = 255 ; pulseWidth >= 0; pulseWidth -=5) { analogWrite(PWM34, pulseWidth); /* set pulse width */ delay(100); /* pause before reverse */ } }