使用Library涵式庫
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library
連接線使用
TB6612FNG上方PWM的腳位需與Arduino的PWM腳位連接才能使用
在Arduino nano上為3、5、6、9、10、11共6個
接線可以參考
https://swf.com.tw/?p=1066
在我的這邊腳位接法
| TB6612FNG | Arduino nano | | PWMA | 3 | | AIN2 | 4 | | AIN1 | 5 | | STBY | 2 | | BIN1 | 11 | | BIN2 | 10 | | PWMB | 9 |
語法內如果加入時間會無法多個馬達同動因此拿掉改為直接給PWM訊號
Library範例加上自己修改的去跑
- /******************************************************************************
- TestRun.ino
- TB6612FNG H-Bridge Motor Driver Example code
- Michelle @ SparkFun Electronics
- 8/20/16
- https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library
- Uses 2 motors to show examples of the functions in the library. This causes
- a robot to do a little 'jig'. Each movement has an equal and opposite movement
- so assuming your motors are balanced the bot should end up at the same place it
- started.
- Resources:
- TB6612 SparkFun Library
- Development environment specifics:
- Developed on Arduino 1.6.4
- Developed with ROB-9457
- ******************************************************************************/
- // This is the library for the TB6612 that contains the class Motor and all the
- // functions
- #include <SparkFun_TB6612.h>
- // Pins for all inputs, keep in mind the PWM defines must be on PWM pins
- // the default pins listed are the ones used on the Redbot (ROB-12097) with
- // the exception of STBY which the Redbot controls with a physical switch
- #define PWMA 3//D3
- #define AIN2 4//D5
- #define AIN1 5//D6
- #define STBY 2//D2
- #define BIN1 11//D8
- #define BIN2 10//D7
- #define PWMB 9//D6
- // these constants are used to allow you to make your motor configuration
- // line up with function names like forward. Value can be 1 or -1
- const int offsetA = 1;
- const int offsetB = 1;
- // Initializing motors. The library will allow you to initialize as many
- // motors as you have memory for. If you are using functions like forward
- // that take 2 motors as arguements you can either write new functions or
- // call the function more than once.
- Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
- Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
- void setup()
- {
- //Nothing here
- }
- void loop()
- {
- //Use of the drive function which takes as arguements the speed
- //and optional duration. A negative speed will cause it to go
- //backwards. Speed can be from -255 to 255. Also use of the
- //brake function which takes no arguements.
- //digitalWrite(STBY, HIGH);
-
- //Use of the forward function, which takes as arguements two motors
- //and optionally a speed. If a negative number is used for speed
- //it will go backwards
- motor2.drive(55);
- motor1.drive(55);
-
- delay(3000);
- motor2.drive(155);
- motor1.drive(155);
- delay(1000);
- motor2.drive(255);
- motor1.drive(255);
- delay(2000);
- motor2.drive(-155);
- motor1.drive(-155);
- delay(1000);
- //digitalWrite(STBY,LOW);
- delay(1000);
-
- delay(1000);
- delay(1000);
- //Use of the back function, which takes as arguments two motors
- //and optionally a speed. Either a positive number or a negative
- //number for speed will cause it to go backwards
- brake(motor1, motor2);
- delay(5000);
- //Use of the brake function which takes as arguments two motors.
- }
複製代碼
|