Created
November 3, 2018 21:13
-
-
Save giljr/a162a0f4f37ed01a8ec04ebd4820a99d to your computer and use it in GitHub Desktop.
TB6612FNG: Dual DC Motor Driver SparkFun Motor Driver — [email protected] peak — Ardu-Serie#49 https://medium.com/jungletronics/tb6612fng-dual-dc-motor-driver-690abd44465d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//define the two direction logic pins and the speed / PWMA and PWMB pin | |
const int PWMA = 6; // Front Motor (A) | |
const int AIN1 = 5; | |
const int AIN2 = 4; | |
// Rear Motor (B) | |
const int PWMB = 3; | |
const int BIN1 = 2; | |
const int BIN2 = 1; | |
void setup() | |
{ | |
// Motor A : set all pins as output | |
pinMode(AIN1, OUTPUT); | |
pinMode(AIN2, OUTPUT); | |
pinMode(PWMA, OUTPUT); | |
// Motor B : set all pins as output | |
pinMode(BIN1, OUTPUT); | |
pinMode(BIN2, OUTPUT); | |
pinMode(PWMB, OUTPUT); | |
} | |
void loop() | |
{ | |
//drive forward at full speed by pulling AIN1/BIN1 High | |
//and AIN2/BIN2 low, while writing a full 255 to PWMA/PWMB to | |
//control speed | |
digitalWrite(AIN1, HIGH); | |
digitalWrite(AIN2, LOW); | |
analogWrite(PWMA, 255); | |
digitalWrite(BIN1, HIGH); | |
digitalWrite(BIN2, LOW); | |
analogWrite(PWMB, 255); | |
//wait 1 second | |
delay(1000); | |
//Brake the motorS by pulling both direction pins to | |
//the same state (in this case LOW). PWMA/PWMB doesn't matter | |
//in a brake situation, but set as 0. | |
digitalWrite(AIN1, LOW); | |
digitalWrite(AIN2, LOW); | |
analogWrite(PWMA, 0); | |
digitalWrite(BIN1, LOW); | |
digitalWrite(BIN2, LOW); | |
analogWrite(PWMB, 0); | |
//wait 1 second | |
delay(1000); | |
//change direction to reverse by flipping the states | |
//of the direction pins from their forward state at lower speed | |
digitalWrite(AIN1, LOW); | |
digitalWrite(AIN2, HIGH); | |
analogWrite(PWMA, 150); | |
digitalWrite(BIN1, LOW); | |
digitalWrite(BIN2, HIGH); | |
analogWrite(PWMB, 150); | |
//wait 1 second | |
delay(1000); | |
//Brake again | |
digitalWrite(AIN1, LOW); | |
digitalWrite(AIN2, LOW); | |
analogWrite(PWMA, 0); | |
digitalWrite(BIN1, LOW); | |
digitalWrite(BIN2, LOW); | |
analogWrite(PWMB, 0); | |
//wait 1 second | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment