├── .gitattributes ├── .gitignore ├── LICENSE.md ├── README.md ├── examples └── MotorTestRun │ └── MotorTestRun.ino ├── keywords.txt ├── library.properties └── src ├── SparkFun_TB6612.cpp └── SparkFun_TB6612.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files 58 | *.b#? 59 | *.s#? 60 | *.l#? 61 | 62 | 63 | ################# 64 | ## Visual Studio 65 | ################# 66 | 67 | ## Ignore Visual Studio temporary files, build results, and 68 | ## files generated by popular Visual Studio add-ons. 69 | 70 | # User-specific files 71 | *.suo 72 | *.user 73 | *.sln.docstates 74 | 75 | # Build results 76 | [Dd]ebug/ 77 | [Rr]elease/ 78 | *_i.c 79 | *_p.c 80 | *.ilk 81 | *.meta 82 | *.obj 83 | *.pch 84 | *.pdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.vspscc 94 | .builds 95 | *.dotCover 96 | 97 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 98 | #packages/ 99 | 100 | # Visual C++ cache files 101 | ipch/ 102 | *.aps 103 | *.ncb 104 | *.opensdf 105 | *.sdf 106 | 107 | # Visual Studio profiler 108 | *.psess 109 | *.vsp 110 | 111 | # ReSharper is a .NET coding add-in 112 | _ReSharper* 113 | 114 | # Installshield output folder 115 | [Ee]xpress 116 | 117 | # DocProject is a documentation generator add-in 118 | DocProject/buildhelp/ 119 | DocProject/Help/*.HxT 120 | DocProject/Help/*.HxC 121 | DocProject/Help/*.hhc 122 | DocProject/Help/*.hhk 123 | DocProject/Help/*.hhp 124 | DocProject/Help/Html2 125 | DocProject/Help/html 126 | 127 | # Click-Once directory 128 | publish 129 | 130 | # Others 131 | [Bb]in 132 | [Oo]bj 133 | sql 134 | TestResults 135 | *.Cache 136 | ClientBin 137 | stylecop.* 138 | ~$* 139 | *.dbmdl 140 | Generated_Code #added for RIA/Silverlight projects 141 | 142 | # Backup & report files from converting an old project file to a newer 143 | # Visual Studio version. Backup files are not needed, because we have git ;-) 144 | _UpgradeReport_Files/ 145 | Backup*/ 146 | UpgradeLog*.XML 147 | 148 | 149 | ############ 150 | ## Windows 151 | ############ 152 | 153 | # Windows image file caches 154 | Thumbs.db 155 | 156 | # Folder config file 157 | Desktop.ini 158 | 159 | 160 | ############# 161 | ## Mac OS 162 | ############# 163 | 164 | .DS_Store 165 | 166 | 167 | ############# 168 | ## Linux 169 | ############# 170 | 171 | # backup files (*.bak on Win) 172 | *~ 173 | 174 | 175 | ############# 176 | ## Python 177 | ############# 178 | 179 | *.py[co] 180 | 181 | # Packages 182 | *.egg 183 | *.egg-info 184 | dist 185 | build 186 | eggs 187 | parts 188 | bin 189 | var 190 | sdist 191 | develop-eggs 192 | .installed.cfg 193 | 194 | # Installer logs 195 | pip-log.txt 196 | 197 | # Unit test / coverage reports 198 | .coverage 199 | .tox 200 | 201 | #Translations 202 | *.mo 203 | 204 | #Mr Developer 205 | .mr.developer.cfg 206 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | SparkFun License Information 2 | ============================ 3 | 4 | SparkFun uses two different licenses for our files - one for hardware and one for code. 5 | 6 | Hardware 7 | --------- 8 | 9 | **SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** 10 | 11 | Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). 12 | 13 | You are free to: 14 | 15 | Share — copy and redistribute the material in any medium or format 16 | Adapt — remix, transform, and build upon the material 17 | for any purpose, even commercially. 18 | The licensor cannot revoke these freedoms as long as you follow the license terms. 19 | Under the following terms: 20 | 21 | Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 22 | ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. 23 | No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 24 | Notices: 25 | 26 | You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. 27 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 28 | 29 | 30 | Code 31 | -------- 32 | 33 | **SparkFun code, firmware, and software is released under the [MIT License](http://opensource.org/licenses/MIT).** 34 | 35 | The MIT License (MIT) 36 | 37 | Copyright (c) 2016 SparkFun Electronics, Inc. 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all 47 | copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 55 | SOFTWARE. 56 | 57 | 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SparkFun SparkFun Motor Driver - Dual TB6612FNG (1A) Arduino Library 2 | ===================================================================== 3 | 4 | ![SparkFun Motor Driver - Dual TB6612FNG (1A)](https://cdn.sparkfun.com//assets/parts/3/1/5/7/09457-01b.jpg) 5 | 6 | [*SparkFun Motor Driver - Dual TB6612FNG (1A) (ROB-0457)*](https://www.sparkfun.com/products/9457) 7 | 8 | The TB6612FNG motor driver can control up to two DC motors at a constant current of 1.2A (3.2A peak). 9 | Two input signals (IN1 and IN2) can be used to control the motor in one of four function modes - CW, CCW, short-brake, and stop. 10 | The two motor outputs (A and B) can be separately controlled, the speed of each motor is controlled via a PWM input signal with a frequency up to 100kHz. 11 | The STBY pin should be pulled high to take the motor out of standby mode. 12 | 13 | Repository Contents 14 | ------------------- 15 | 16 | * **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. 17 | * **/extras** - Additional documentation for the user. These files are ignored by the IDE. 18 | * **/src** - Source files for the library (.cpp, .h). 19 | * **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. 20 | * **library.properties** - General library properties for the Arduino package manager. 21 | 22 | Documentation 23 | -------------- 24 | 25 | * **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. 26 | * **[Product Repository](https://github.com/sparkfun/Motor_Driver-Dual_TB6612FNG/tree/V_1.1)** - Main repository (including hardware files) for the TB6612FNG Motor Driver. 27 | * **[Hookup Guide](https://learn.sparkfun.com/tutorials/tb6612fng-hookup-guide)** - Basic hookup guide for the TB6612FNG Motor Driver. 28 | 29 | Products that use this Library 30 | --------------------------------- 31 | 32 | * [ROB-09457](https://www.sparkfun.com/products/9457)- Motor Driver 33 | * [ROB-13845](https://www.sparkfun.com/products/13845)- Motor Driver with headers installed 34 | 35 | 36 | License Information 37 | ------------------- 38 | 39 | This product is _**open source**_! 40 | 41 | Please review the LICENSE.md file for license information. 42 | 43 | If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. 44 | 45 | Distributed as-is; no warranty is given. 46 | 47 | -Your friends at SparkFun. 48 | 49 | 50 | -------------------------------------------------------------------------------- /examples/MotorTestRun/MotorTestRun.ino: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | TestRun.ino 3 | TB6612FNG H-Bridge Motor Driver Example code 4 | Michelle @ SparkFun Electronics 5 | 8/20/16 6 | https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library 7 | 8 | Uses 2 motors to show examples of the functions in the library. This causes 9 | a robot to do a little 'jig'. Each movement has an equal and opposite movement 10 | so assuming your motors are balanced the bot should end up at the same place it 11 | started. 12 | 13 | Resources: 14 | TB6612 SparkFun Library 15 | 16 | Development environment specifics: 17 | Developed on Arduino 1.6.4 18 | Developed with ROB-9457 19 | ******************************************************************************/ 20 | 21 | // This is the library for the TB6612 that contains the class Motor and all the 22 | // functions 23 | #include 24 | 25 | // Pins for all inputs, keep in mind the PWM defines must be on PWM pins 26 | // the default pins listed are the ones used on the Redbot (ROB-12097) with 27 | // the exception of STBY which the Redbot controls with a physical switch 28 | #define AIN1 2 29 | #define BIN1 7 30 | #define AIN2 4 31 | #define BIN2 8 32 | #define PWMA 5 33 | #define PWMB 6 34 | #define STBY 9 35 | 36 | // these constants are used to allow you to make your motor configuration 37 | // line up with function names like forward. Value can be 1 or -1 38 | const int offsetA = 1; 39 | const int offsetB = 1; 40 | 41 | // Initializing motors. The library will allow you to initialize as many 42 | // motors as you have memory for. If you are using functions like forward 43 | // that take 2 motors as arguements you can either write new functions or 44 | // call the function more than once. 45 | Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); 46 | Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); 47 | 48 | void setup() 49 | { 50 | //Nothing here 51 | } 52 | 53 | 54 | void loop() 55 | { 56 | //Use of the drive function which takes as arguements the speed 57 | //and optional duration. A negative speed will cause it to go 58 | //backwards. Speed can be from -255 to 255. Also use of the 59 | //brake function which takes no arguements. 60 | motor1.drive(255,1000); 61 | motor1.drive(-255,1000); 62 | motor1.brake(); 63 | delay(1000); 64 | 65 | //Use of the drive function which takes as arguements the speed 66 | //and optional duration. A negative speed will cause it to go 67 | //backwards. Speed can be from -255 to 255. Also use of the 68 | //brake function which takes no arguements. 69 | motor2.drive(255,1000); 70 | motor2.drive(-255,1000); 71 | motor2.brake(); 72 | delay(1000); 73 | 74 | //Use of the forward function, which takes as arguements two motors 75 | //and optionally a speed. If a negative number is used for speed 76 | //it will go backwards 77 | forward(motor1, motor2, 150); 78 | delay(1000); 79 | 80 | //Use of the back function, which takes as arguments two motors 81 | //and optionally a speed. Either a positive number or a negative 82 | //number for speed will cause it to go backwards 83 | back(motor1, motor2, -150); 84 | delay(1000); 85 | 86 | //Use of the brake function which takes as arguments two motors. 87 | //Note that functions do not stop motors on their own. 88 | brake(motor1, motor2); 89 | delay(1000); 90 | 91 | //Use of the left and right functions which take as arguements two 92 | //motors and a speed. This function turns both motors to move in 93 | //the appropriate direction. For turning a single motor use drive. 94 | left(motor1, motor2, 100); 95 | delay(1000); 96 | right(motor1, motor2, 100); 97 | delay(1000); 98 | 99 | //Use of brake again. 100 | brake(motor1, motor2); 101 | delay(1000); 102 | 103 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map SparkFun TB6612 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Motor KEYWORD1 Motor 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | drive KEYWORD2 16 | brake KEYWORD2 17 | standby KEYWORD2 18 | left KEYWORD2 19 | right KEYWORD2 20 | forward KEYWORD2 21 | back KEYWORD2 22 | 23 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SparkFun TB6612FNG Motor Driver Library 2 | version=1.0.0 3 | author=Michelle @ SparkFun Electronics 4 | maintainer=SparkFun Electronics 5 | sentence=Allows control of 2 DC motors, using two input signals. 6 | paragraph=Individually control motor outputs of 2 DC motors at 1.2A constant current. There are four function modes, including: CW, CCW, short-brake, and stop. 7 | category=Device Control 8 | url=https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library 9 | architectures=avr 10 | -------------------------------------------------------------------------------- /src/SparkFun_TB6612.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | TB6612.cpp 3 | TB6612FNG H-Bridge Motor Driver Example code 4 | Michelle @ SparkFun Electronics 5 | 8/20/16 6 | https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library 7 | 8 | Uses 2 motors to show examples of the functions in the library. This causes 9 | a robot to do a little 'jig'. Each movement has an equal and opposite movement 10 | so assuming your motors are balanced the bot should end up at the same place it 11 | started. 12 | 13 | Resources: 14 | TB6612 SparkFun Library 15 | 16 | Development environment specifics: 17 | Developed on Arduino 1.6.4 18 | Developed with ROB-9457 19 | ******************************************************************************/ 20 | 21 | #include "SparkFun_TB6612.h" 22 | #include 23 | 24 | Motor::Motor(int In1pin, int In2pin, int PWMpin, int offset, int STBYpin) 25 | { 26 | In1 = In1pin; 27 | In2 = In2pin; 28 | PWM = PWMpin; 29 | Standby = STBYpin; 30 | Offset = offset; 31 | 32 | pinMode(In1, OUTPUT); 33 | pinMode(In2, OUTPUT); 34 | pinMode(PWM, OUTPUT); 35 | pinMode(Standby, OUTPUT); 36 | } 37 | 38 | void Motor::drive(int speed) 39 | { 40 | digitalWrite(Standby, HIGH); 41 | speed = speed * Offset; 42 | if (speed>=0) fwd(speed); 43 | else rev(-speed); 44 | } 45 | void Motor::drive(int speed, int duration) 46 | { 47 | drive(speed); 48 | delay(duration); 49 | } 50 | 51 | void Motor::fwd(int speed) 52 | { 53 | digitalWrite(In1, HIGH); 54 | digitalWrite(In2, LOW); 55 | analogWrite(PWM, speed); 56 | 57 | } 58 | 59 | void Motor::rev(int speed) 60 | { 61 | digitalWrite(In1, LOW); 62 | digitalWrite(In2, HIGH); 63 | analogWrite(PWM, speed); 64 | } 65 | 66 | void Motor::brake() 67 | { 68 | digitalWrite(In1, HIGH); 69 | digitalWrite(In2, HIGH); 70 | analogWrite(PWM,0); 71 | } 72 | 73 | void Motor::standby() 74 | { 75 | digitalWrite(Standby, LOW); 76 | } 77 | 78 | void forward(Motor motor1, Motor motor2, int speed) 79 | { 80 | motor1.drive(speed); 81 | motor2.drive(speed); 82 | } 83 | void forward(Motor motor1, Motor motor2) 84 | { 85 | motor1.drive(DEFAULTSPEED); 86 | motor2.drive(DEFAULTSPEED); 87 | } 88 | 89 | 90 | void back(Motor motor1, Motor motor2, int speed) 91 | { 92 | int temp = abs(speed); 93 | motor1.drive(-temp); 94 | motor2.drive(-temp); 95 | } 96 | void back(Motor motor1, Motor motor2) 97 | { 98 | motor1.drive(-DEFAULTSPEED); 99 | motor2.drive(-DEFAULTSPEED); 100 | } 101 | void left(Motor left, Motor right, int speed) 102 | { 103 | int temp = abs(speed)/2; 104 | left.drive(-temp); 105 | right.drive(temp); 106 | 107 | } 108 | 109 | 110 | void right(Motor left, Motor right, int speed) 111 | { 112 | int temp = abs(speed)/2; 113 | left.drive(temp); 114 | right.drive(-temp); 115 | 116 | } 117 | void brake(Motor motor1, Motor motor2) 118 | { 119 | motor1.brake(); 120 | motor2.brake(); 121 | } -------------------------------------------------------------------------------- /src/SparkFun_TB6612.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | SparkFun_TB6612.h 3 | TB6612FNG H-Bridge Motor Driver Example code 4 | Michelle @ SparkFun Electronics 5 | 8/20/16 6 | https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library 7 | 8 | Uses 2 motors to show examples of the functions in the library. This causes 9 | a robot to do a little 'jig'. Each movement has an equal and opposite movement 10 | so assuming your motors are balanced the bot should end up at the same place it 11 | started. 12 | 13 | Resources: 14 | TB6612 SparkFun Library 15 | 16 | Development environment specifics: 17 | Developed on Arduino 1.6.4 18 | Developed with ROB-9457 19 | ******************************************************************************/ 20 | 21 | 22 | #ifndef SPARKFUN_TB6612_h 23 | #define SPARKFUN_TB6612_h 24 | 25 | #include 26 | 27 | //used in some functions so you don't have to send a speed 28 | #define DEFAULTSPEED 255 29 | 30 | 31 | 32 | class Motor 33 | { 34 | public: 35 | // Constructor. Mainly sets up pins. 36 | Motor(int In1pin, int In2pin, int PWMpin, int offset, int STBYpin); 37 | 38 | // Drive in direction given by sign, at speed given by magnitude of the 39 | //parameter. 40 | void drive(int speed); 41 | 42 | // drive(), but with a delay(duration) 43 | void drive(int speed, int duration); 44 | 45 | //currently not implemented 46 | //void stop(); // Stop motors, but allow them to coast to a halt. 47 | //void coast(); // Stop motors, but allow them to coast to a halt. 48 | 49 | //Stops motor by setting both input pins high 50 | void brake(); 51 | 52 | //set the chip to standby mode. The drive function takes it out of standby 53 | //(forward, back, left, and right all call drive) 54 | void standby(); 55 | 56 | private: 57 | //variables for the 2 inputs, PWM input, Offset value, and the Standby pin 58 | int In1, In2, PWM, Offset,Standby; 59 | 60 | //private functions that spin the motor CC and CCW 61 | void fwd(int speed); 62 | void rev(int speed); 63 | 64 | 65 | }; 66 | 67 | //Takes 2 motors and goes forward, if it does not go forward adjust offset 68 | //values until it does. These will also take a negative number and go backwards 69 | //There is also an optional speed input, if speed is not used, the function will 70 | //use the DEFAULTSPEED constant. 71 | void forward(Motor motor1, Motor motor2, int speed); 72 | void forward(Motor motor1, Motor motor2); 73 | 74 | //Similar to forward, will take 2 motors and go backwards. This will take either 75 | //a positive or negative number and will go backwards either way. Once again the 76 | //speed input is optional and will use DEFAULTSPEED if it is not defined. 77 | void back(Motor motor1, Motor motor2, int speed); 78 | void back(Motor motor1, Motor motor2); 79 | 80 | //Left and right take 2 motors, and it is important the order they are sent. 81 | //The left motor should be on the left side of the bot. These functions 82 | //also take a speed value 83 | void left(Motor left, Motor right, int speed); 84 | void right(Motor left, Motor right, int speed); 85 | 86 | //This function takes 2 motors and and brakes them 87 | void brake(Motor motor1, Motor motor2); 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | #endif --------------------------------------------------------------------------------