BLOG記事用論壇

 找回密碼
 註冊
搜索
熱搜:
查看: 2443|回復: 0

TB6612FNG連接Arduino nano

[複製鏈接]
發表於 2021-7-10 00:45:01 | 顯示全部樓層 |閱讀模式

使用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


語法內如果加入時間會無法多個馬達同動
  1. motor1.drive(-155,1000);
複製代碼
因此拿掉改為直接給PWM訊號
  1. motor1.drive(-155);
複製代碼


Library範例加上自己修改的去跑

  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. Uses 2 motors to show examples of the functions in the library.  This causes
  8. a robot to do a little 'jig'.  Each movement has an equal and opposite movement
  9. so assuming your motors are balanced the bot should end up at the same place it
  10. started.

  11. Resources:
  12. TB6612 SparkFun Library

  13. Development environment specifics:
  14. Developed on Arduino 1.6.4
  15. Developed with ROB-9457
  16. ******************************************************************************/

  17. // This is the library for the TB6612 that contains the class Motor and all the
  18. // functions
  19. #include <SparkFun_TB6612.h>

  20. // Pins for all inputs, keep in mind the PWM defines must be on PWM pins
  21. // the default pins listed are the ones used on the Redbot (ROB-12097) with
  22. // the exception of STBY which the Redbot controls with a physical switch

  23. #define PWMA 3//D3
  24. #define AIN2 4//D5
  25. #define AIN1 5//D6

  26. #define STBY 2//D2

  27. #define BIN1 11//D8
  28. #define BIN2 10//D7
  29. #define PWMB 9//D6

  30. // these constants are used to allow you to make your motor configuration
  31. // line up with function names like forward.  Value can be 1 or -1
  32. const int offsetA = 1;
  33. const int offsetB = 1;

  34. // Initializing motors.  The library will allow you to initialize as many
  35. // motors as you have memory for.  If you are using functions like forward
  36. // that take 2 motors as arguements you can either write new functions or
  37. // call the function more than once.
  38. Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
  39. Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

  40. void setup()
  41. {
  42. //Nothing here
  43. }


  44. void loop()
  45. {
  46.    //Use of the drive function which takes as arguements the speed
  47.    //and optional duration.  A negative speed will cause it to go
  48.    //backwards.  Speed can be from -255 to 255.  Also use of the
  49.    //brake function which takes no arguements.
  50.   //digitalWrite(STBY, HIGH);
  51.    
  52.    //Use of the forward function, which takes as arguements two motors
  53.    //and optionally a speed.  If a negative number is used for speed
  54.    //it will go backwards
  55.     motor2.drive(55);
  56.     motor1.drive(55);
  57.    
  58.     delay(3000);
  59.     motor2.drive(155);
  60.     motor1.drive(155);
  61.     delay(1000);
  62.     motor2.drive(255);
  63.     motor1.drive(255);
  64.     delay(2000);
  65.     motor2.drive(-155);
  66.     motor1.drive(-155);
  67.     delay(1000);
  68.     //digitalWrite(STBY,LOW);
  69.     delay(1000);
  70.    
  71.     delay(1000);
  72.     delay(1000);
  73.    //Use of the back function, which takes as arguments two motors
  74.    //and optionally a speed.  Either a positive number or a negative
  75.    //number for speed will cause it to go backwards
  76.    brake(motor1, motor2);
  77.    delay(5000);
  78.    //Use of the brake function which takes as arguments two motors.

  79. }
複製代碼




您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|綜合討論區

GMT+8, 2026-6-27 20:11 , Processed in 0.037710 second(s), 8 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回復 返回頂部 返回列表