內心凋零的男孩
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Arduino 步進馬達 ###### tags: `Arduino` ## 目錄 [TOC] ## library ```cpp= #include <Stepper.h> ``` ## 運作原理 ### 基本原理 基本上分兩種,單極步進馬達和雙極步進馬達。它們之間最大的區別就是兩種不同類型都有各自的捲繞裝置,而且它們各自的捲繞裝置是會影響它們的步進馬達的控制方式。 基本上外部有四個線圈,內部中心有一個磁鐵。輪流讓線圈接地(GND),即可改變線圈磁性,就可以讓在中間的磁鐵輪流轉動。 ![](https://i.imgur.com/PKbQpVM.gif) 這個影片獎的不錯 <iframe width="560" height="315" src="https://www.youtube.com/embed/eyqwLiowZiU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### 單極步進馬達 ![](https://i.imgur.com/tqlrMLw.png) ![](https://i.imgur.com/du1Q7EY.png) ### 雙極步進馬達 ![](https://i.imgur.com/wWtPy3l.png) ![](https://i.imgur.com/lOUQfbm.png) ## 圖片 ![](https://i.imgur.com/DALahol.jpg) ## Stepper 宣告 ```cpp= Stepper(steps, pin1, pin2); Stepper(steps, pin1, pin2, pin3, pin4); ``` - steps:必須設定為你的馬達實際上一圈會有多少步,即 (360 / 每步轉幾度),步進角在查詢的規格上應該會寫 - pin1, pin2:腳位 - pin3, pin4:腳位 example ```cpp= #include <Stepper.h> const int stepsPerRevolution = 200; Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // initialize the stepper library on pins 8 through 11: void setup() {} void loop() {} ``` ## Stepper: setSpeed(rpms) 這個函式並不會讓馬達轉動,只是設定他的速度(初始化)當呼叫 Step()函式時才會開始轉動 ```cpp= setSpeed(rpms); ``` - rpms:馬達每分鐘轉的速度(正數) ## Stepper: step(steps) 啟動馬達行進steps步數。setSpeed() 定義速度,正的表示一個方向, 負數表示反方向。 ```cpp= step(steps); ``` ## 範例 ```cpp= #include <Stepper.h> #define STEPS 200 //定義步進馬達每圈的步數 //steps:代表馬達轉完一圈需要多少步數。如果馬達上有標示每步的度數, //將360除以這個角度,就可以得到所需要的步數(例如:360/3.6=100)。(int) Stepper stepper(STEPS, 11, 10, 9, 8); void setup(){ stepper.setSpeed(140); // 將馬達的速度設定成140RPM 最大 150~160 } void loop() { stepper.step(100);//正半圈 delay(1000); stepper.step(-100);//反半圈 delay(1000); stepper.step(200);//正1圈 delay(1000); stepper.step(-200);//反1圈 delay(1000); stepper.step(300);//正1圈半 delay(1000); stepper.step(-300);//反1圈半 delay(1000); stepper.step(1600);//正8圈 delay(1000); stepper.step(-1600);//反8圈 delay(1000); } ``` ```cpp= #include <Stepper.h> const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution // for your motor // initialize the stepper library on pins 8 through 11: Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); // initialize the serial port: Serial.begin(9600); } void loop() { // step one revolution in one direction: Serial.println("clockwise"); myStepper.step(stepsPerRevolution); delay(500); // step one revolution in the other direction: Serial.println("counterclockwise"); myStepper.step(-stepsPerRevolution); delay(500); } ``` ## 其他 當然也可以不要用官方的函式,使用 digitalWrite 去慢慢寫也可以。 ```cpp= int apin = 8; int bpin = 9; int cpin = 10; int dpin = 11; int delayTime = 10; void setup() { pinMode(apin, OUTPUT); pinMode(bpin, OUTPUT); pinMode(cpin, OUTPUT); pinMode(dpin, OUTPUT); } void loop() { digitalWrite(apin, HIGH); // 變成高電位 delay(delayTime); digitalWrite(apin, LOW); digitalWrite(bpin, HIGH); // 變成高電位 delay(delayTime); digitalWrite(bpin, LOW); digitalWrite(cpin, HIGH); // 變成高電位 delay(delayTime); digitalWrite(cpin, LOW); digitalWrite(dpin, HIGH); // 變成高電位 delay(delayTime); digitalWrite(dpin, LOW); } ``` ```cpp= int t= 2; //t會影響轉速,t越小轉越快,太小會轉不動 void setup() { pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); Serial.begin(9600); } void loop() { for(int i = 1; i <= 512; i++){ stepForward(); Serial.println(String(i)); //在序列傅印出 } Serial.println("One round"); delay(1000); for(int i = 1; i <= 512; i++){ stepBack(); Serial.println(String(i)); //在序列傅印出 } Serial.println("One round"); delay(1000); } void stepForward(){ //向前 digitalWrite(8,1); digitalWrite(9,0); digitalWrite(10,0); digitalWrite(11,0); delay(t); digitalWrite(8,0); digitalWrite(9,1); digitalWrite(10,0); digitalWrite(11,0); delay(t); digitalWrite(8,0); digitalWrite(9,0); digitalWrite(10,1); digitalWrite(11,0); delay(t); digitalWrite(8,0); digitalWrite(9,0); digitalWrite(10,0); digitalWrite(11,1); delay(t); } void stepBack(){ //向後 digitalWrite(8,0); digitalWrite(9,0); digitalWrite(10,0); digitalWrite(11,1); delay(t); digitalWrite(8,0); digitalWrite(9,0); digitalWrite(10,1); digitalWrite(11,0); delay(t); digitalWrite(8,0); digitalWrite(9,1); digitalWrite(10,0); digitalWrite(11,0); delay(t); digitalWrite(8,1); digitalWrite(9,0); digitalWrite(10,0); digitalWrite(11,0); delay(t); } ``` ## 參考資料 - [mBlock & Arduino(16)控制步進馬達](https://openhome.cc/Gossip/CodeData/mBlockArduino/mBlockArduino16.html) - [Arduino與步進馬達](https://sites.google.com/site/zsgititit/home/arduino/arduino-yu-bu-jin-ma-da) - [Stepper Library](https://www.arduino.cc/en/reference/stepper) - [Arduino筆記(8):控制步進馬達](https://atceiling.blogspot.com/2013/04/arduino.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully