109單晶片
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    --- tags: 單晶片助教 --- # Arduino講義:跟隨自走車 [ToC] * 超音波模組工作原理: ![](https://i.imgur.com/ODTMfKF.png) * 觸發 : 採用I/O觸發測距,給至少為 <font color="red">10uS</font> 的高電位信號。 * 發射 : 模組自動發送 8 個 40KHz 的方波,自動檢測是否有信號返回。 * 接收 : 有信號返回,通過模組之 I/O 輸出高電位,<font color="red">高電位持續的時間</font>就是超聲波 從發射到返回的時間。 ![](https://i.imgur.com/qci8enf.png) * 距離計算 = (高電位時間 / 2) * 聲速 。 * 讀取高電位時間 : 使用 <font color="red">pulseIn( )</font> 此Function,單位為microsecond(µs)。 * Reference: [Arduino pulseln()](https://www.arduino.cc/en/Reference.PulseIn) * 聲速 = 331.5 + 0.607 * t(攝氏溫度)。 Ex : 溫度為攝氏 20 度,則聲速為 331.5 + 0.607 * 20 = 343.64 m/s。 * 將聲速還算成 cm/µs => 343.64 * 100 / 1000000 = 0.034364 cm/µs。 * 超音波模組規格與腳位 : * 電源需求 : DC 5V / 2mA * 輸出電位( 1/ 0):5V/ 0V * 精度:3mm * <font color="red">距離範圍:2 ~ 450cm</font> * 有效的角度:<15℃ ![](https://i.imgur.com/LWgbM7Z.png) * 超音波使用須知 ![](https://i.imgur.com/fboNeyL.png) * 不可少於2公分 ![](https://i.imgur.com/B5eTjnD.png) * 每次訊號間隔不可太短 ![](https://i.imgur.com/6CxZFXd.jpg) * 待測物注意反射角度 ![](https://i.imgur.com/Rv82yzQ.png) * 待測物圓柱體為最佳 ### Example ![](https://i.imgur.com/hWvIJ3F.png) ### Lab1 - 超聲波測距器 #### 實作說明: * 實作目的 : 學習使用超聲波模組。 * 實作範例 : 使用超聲波模組搭配 LCD 顯示器,當物體在 15 公分內時,將測得之距離顯示於 LCD 上,超過 15 公分,則顯示 Exceed 。 * 實作電路圖 : ![](https://i.imgur.com/Ue8brEF.png) * 實作腳位配置 : | HC-SR04 超聲波模組 | Arduino | | -------- | -------- | | Vcc | 5V | | Trig | Pin12 | | Echo | Pin11 | | Gnd | GND | | LCM 1602 | Arduino | | -------- | -------- | | GND | GND | | Vcc | 5V | | SDA | A4 | | SCL | A5 | * 實作影片 : <iframe width="640" height="361" src="https://www.youtube.com/embed/D3kzD2FmErQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## PWM (Pulse Width Modulation) 脈衝寬度調變 * PWM : 利用數位訊號模擬類比訊號的方式。 * PWM原理 : 藉由數位訊號高頻率的切換,調整開關的比例,就可以模擬出需要的類比訊號。 ![](https://i.imgur.com/JzH4Ugy.png) ![](https://i.imgur.com/g1XaOdl.png) ![](https://i.imgur.com/aBkBbUf.png) * Timer Registers * ATmega328 內與 Timer 相關的 Register 大略如下(n 代表 Timern): * Timer Counter Control Register A (TCCRnA):決定 timer 的運作模式(operating mode)。 * Timer Counter Control Register B (TCCRnB):決定 timer 的 prescale value。 * Timer Counter Register (TCNTn):儲存 timer 目前的累積計數 (timer count)。 * Output Compare Register A (OCRnA):當 TCNTn 累積計數到 OCRnA 的計數時,觸發中斷(interrupt)。 * Ouput Compare Register B (OCRnB):當 TCNTn 累積計數到 OCRnB 的計數時,觸發中斷(interrupt)。 * Timer0 與 Timer2 皆為 8-bit 的 Timer,表示 TCNT0, 2 範圍為 0 ~ 255;Timer1 的TCNT1則為 16-bit,可以計數 0 ~ 65535。 * 如何使用 Arduino PWM : ![](https://i.imgur.com/0rBZBoi.png) Pin 3、9、10、11 頻率為 490 Hz,Pin 5,6 頻率為 980 Hz | Pin | timer | base frequency | prescaler value | |:----:|:------:|:--------------:| ------------------------------ | | 5,6 | timer0 | 62500Hz | 1,8,64,256,1024 | | 9,10 | timer1 | 31250Hz | 1,8,64,256,1024 | | 3,11 | timer2 | 31250Hz | 1,8,**32**,64,**128**,256,1024 | | Pin | timer | base frequency | defalut prescaler | defalut frequency | | :---: | :------: | :--------------: | :---------------: | :-:| | 5,6 | timer0 | 62500Hz | 64 | 980Hz | | 9,10 | timer1 | 31250Hz | 64 | 490Hz | | 3,11 | timer2 | 31250Hz | 64 | 490Hz | * Arduino PWM輸出函數 : <font color="red" size="5">analogWrite(pin, value)</font> -Pin:輸出PWM腳位。 -Value:Duty cycle,數值範圍(0~255)。 -Duty Cycle = Ton / (Ton + Toff) (%),為 ON 的時間與週期相除的百分比,例如:0=0%,127=50%,255=100%。 ### Lab2 - 測距警示燈 #### 實作說明: * 實作目的 : 學習控制 Arduino PWM 以及 超聲波模組。 * 實作範例 : 控制PWM與上節所使用到超音波模組,Duty Cycle會隨超音波測得距離越近而越高,反之則相反。 * 實作電路圖 : ![](https://i.imgur.com/0JuCStZ.png) * 實作影片 : <iframe width="640" height="360" src="https://www.youtube.com/embed/K_ivwCI-eMM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <iframe width="640" height="361" src="https://www.youtube.com/embed/HmjG65rJGTc?list=UUQmpO0IQCpe-DNwHk2z9jVQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### Lab3 - 雙超音波定位應用 #### 實作說明: ![](https://i.imgur.com/L6PE42K.jpg) * 實作目的 : 學習使用超聲波模組。 * 實作範例 : 使用兩顆超聲波模組搭配 LCD 顯示器,當物體在 50 公分內時,將測得之方位顯示於 LCD 上 。 * 實作影片 : * <iframe width="740" height="553" src="https://www.youtube.com/embed/fPPryYAdZBI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### bonus1 - 電算機 #### 實作說明: * 實作目的 : 學習更加完善之電算機。 * 實作範例 : 可應付四則運算,多位數運算,多組括號,多層括號 * 實作影片 : <iframe width="740" height="553" src="https://www.youtube.com/embed/8RhHF9nyTOI" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### bonus2 - 電算機2 #### 實作說明: * 實作目的 : 學習更加完善之電算機。 * 實作範例 : 將Lab4加以改良,實現可以省略乘號。 * 實作影片 : <iframe width="740" height="553" src="https://www.youtube.com/embed/XWxlOy_4sKc" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ### Lab3 - L298N馬達應用 #### 實作說明: * 實作目的 : 學習控制 L298N馬達模組。 * 實作範例 : 透過各別控制每顆馬達,來分別進行正轉、反轉、停止等功能等時作 * L298N接腳: * 模組週邊腳位及端子台說明: ![](https://i.imgur.com/BhD0KSP.jpg) | L298N馬達模組 | Arduino | | -------- | -------- | | INT1 | Pin2 | | INT2 | Pin4 | | INT3 | Pin5 | | INT4 | Pin6 | | ENA | Pin3(PWM) | | ENB | Pin9(PWM) | * 模組控制訊號說明 |DC馬達|轉向|IN1|IN2|IN3|IN4|ENA|ENB| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |M1|正轉|高|低| | |高| | |M1|反轉|低|高| | |高| | |M1|停止|低|低| | |高| | |/|/|/|/|/|/|/|/| |M2|正轉| | |高|低| |高| |M2|反轉| | |低|高| |高| |M2|停止| | |低|低| |高| * ENA和ENB腳位,可用<font color="red">PWM</font>來改變馬達轉速。 ![](https://i.imgur.com/OzLFp2X.png) ![](https://i.imgur.com/wNELm9M.png) ![](https://i.imgur.com/ptWH999.png) ![](https://i.imgur.com/T3MaljC.png) ### Lab4 - 跟隨自走車 ![](https://i.imgur.com/HiKbNMm.png) #### 實作說明: * 實作目的:完成跟隨自走車。 * 實作方法:利用<font color="red">兩組超音波測距模組</font>,並搭配<font color="red">L298N驅動馬達</font> * L298N接腳: | L298N馬達模組 | Arduino | | -------- | -------- | | INT1 | Pin2 | | INT2 | Pin4 | | INT3 | Pin5 | | INT4 | Pin6 | | ENA | Pin3(PWM) | | ENB | Pin9(PWM) | * 超音波模組接腳: | 超音波模組 | Arduino | | -------- | -------- | | trigPin_left | Pin13 | | trigPin_right | Pin12 | | echoPin_left | Pin11 | | echoPin_right | Pin10 | * 實作步驟: * 組裝自走車。 * 先試著建立好副程式,讓自走車轉動 ![](https://i.imgur.com/e0gfVIC.png) ![](https://i.imgur.com/c3eH4OV.png) * 加入超音波感測,並從序列埠監控室窗觀察。 * 並藉由超音波測距判斷決策來操控馬達,決策條件說明: <font color="red">★50cm範圍外為機器人停止。 ★50~20cm範圍內為機器人前進。 ★10cm範圍內為機器人倒退。 ★20~10cm範圍內為機器人停止。 ★兩組超音波測距比較去決策機器人轉動方向。 —以上可依照感測器架設狀況做微調—</font> * 本實作部分程式提點: ![](https://i.imgur.com/ch9Gm51.png) <!--![](https://i.imgur.com/Lnu5fzj.png) ![](https://i.imgur.com/yfek1Ds.png) ![](https://i.imgur.com/PRflCaW.png)--> <!--![](https://i.imgur.com/wq3UAb0.png)--> * 影片 * https://youtu.be/iUuJmY7A3Og ### Lab4-2 - 牙控車 #### 實作說明: * 實作目的:完成可接受藍牙操控之牙控車。 * 實作方法:利用<font color="red">HC-05藍牙模組</font>,並搭配<font color="red">LAB4跟隨自走車</font>,設計由傳輸指定字元控制牙控車型走之方向,以W、R、L、B控制前進、右轉、左轉、後退之功能 ### Lab4-3 - 牙控車2 #### 實作說明: * 實作目的:改善以手機發送指令時的不順暢,改由第二塊arduino和按鈕進行操控。 * 實作方法:須設定兩顆藍芽模組之間之主從關係,主端(遙控器)必須是HC-05,而從端(車體)則是HC-05和HC-06皆可。 * 主端:將HC-05進入AT模式,用AT+ROLE?指令查詢主從關係,若為1則為主端,若為0則以AT+ROLE=1之指令更改(主從關係更改後會自動離開AT模式,欲再次查詢需重新進入AT模式)。確認為主端後在AT模式以AT+BIND=XXXX,XX,XXXXXX之格式設定從端連線位址。![](https://i.imgur.com/lFzgsR0.png) * 從端:若從端為HC-05,可用AT+ADDR?指令查詢位址。若為HC-06,則必須以安卓手機下載Serial bluetooth terminal查詢並將其格式改為XXXX,XX,XXXXXX輸入至主端 ![](https://i.imgur.com/IHMW55U.jpg) * 實作影片 :<iframe width="730" height="553" src="https://www.youtube.com/embed/pG8rqLXQHN4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> # 加分題1 - 跟隨自走車2 #### 實作說明: * 實作目的:以不停頓的方式跟隨至足夠距離。 * 實作方法:利用PWM之功能不斷修正馬達轉速達到順暢跟隨 * <iframe width="740" height="553" src="https://www.youtube.com/embed/057iBgh0C2o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> # 加分題2 - 自動避障車 #### 實作說明: * 實作目的:完成具有自動避障功能之自走車。 * 實作方法:利用<font color="red">LAB4跟隨自走車</font>,設計會不斷前進,遇到障礙物會自行規避並尋找新方向繼續移動,並在進入死路後能退出無路可走之困境 ## 課後問題 (三題) :::info * Q1.詳述 超聲波模組感測原理。 * Q2.詳述 超聲波的英文。 * Q3.詳述 Arduino是如何產生PWM訊號(與內部Timer有關)。 :::

    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