阿好伯
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    3
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- disqus: ahb0222 GA : G-VF9ZT413CG --- # micropython 讀取ADC繪製歷史曲線(Line Plot) > [color=#40f1ef][name=LHB阿好伯, 2022/06/03][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Micropython` [TOC] ![](https://hackmd.io/_uploads/SkpOylHO9.jpg) ![](https://hackmd.io/_uploads/rJauygrd5.jpg) # ADC介紹 在 ESP32 上,引腳 32-39(ADC 模組 1)和引腳 0、2、4、12-15 和 25-27(ADC 模組 2)上提供 ADC 功能 :::info ADC模組2也被WiFi使用,因此當WiFi處於活動狀態時 從模組2引腳讀取模擬值將引發異常。 **輸入引腳的最大額定電壓為3.6V** ::: ```python= from machine import Pin, ADC import time adc_pin = Pin(32) adc = ADC(adc_pin) # 創建作用在PIN上的ADC對象 #val_2 = adc.read_uv() # 讀取微伏值_測試無法使用 adc.atten(ADC.ATTN_11DB) def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) while True: val_1 = adc.read() # 讀取0-4095中讀原始模擬值 print("val_1:",val_1) val_2 = map(val_1,0,4095,0, 100) # 轉換數值 print("val_2:" , val_2) time.sleep(1) ``` ``` ADC.ATTN_0DB:0dB衰減,輸入電壓上限1.00V,此為預設值。 ADC.ATTN_2_5DB:2.5dB衰減,輸入電壓上限1.34V。 ADC.ATTN_6DB:6dB 衰減,輸入電壓上限2.00V。 ADC.ATTN_11DB:11dB 衰減,輸入電壓上限3.6V。 ADC.WIDTH_9BIT:9位元 (2^9,0~511) ADC.WIDTH_10BIT:10位元(2^10,0~1023) ADC.WIDTH_11BIT:11位元(2^11,0~2047) ADC.WIDTH_12BIT:12位元(2^12,0~4095) ``` 在相同取樣位元的情況下,輸入電壓範圍越小越精確 例如,1V分割成4096份,對比3.6V分割成4096份 1V的分割比較細緻 若是需要更精準的ADC可以選用16位精密模數轉換器(ADC) 開發板模組 ![](https://hackmd.io/_uploads/S1ImfqP_q.png) :::warning 在Arduino 中有一個很好用的map函數 他可以將輸入值依照最大最小值依自己需求進行轉換 例如ADC 12bit讀取到0 ~ 4095的數值要轉換為0~100 就可以使用map函數進行轉換 而micropython則只需兩行程式碼自行定義出這個函數 ```python= def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) ``` ::: ![](https://hackmd.io/_uploads/S1_R-5Du9.png) ```python= from machine import Pin, I2C, SoftI2C, ADC import ssd1306 import time # using default address 0x3C # i2c = I2C(sda=Pin(23), scl=Pin(19)) #硬體 I2C i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) #慧手科技套件腳位 #i2c = SoftI2C(scl=Pin(19), sda=Pin(23), freq=400000) # 軟體 I2C display = ssd1306.SSD1306_I2C(128, 64, i2c) display.invert(0) # 螢幕顏色正常 #display.invert(1) # 螢幕顏色反轉 #display.rotate(True) # 螢幕旋轉180度 display.rotate(False) # 螢幕旋轉0度 display.text('Hello, World~~~', 0, 0, 1) display.show() # 螢幕顯示 display.fill(0)# 填充螢幕 0 = 全黑, 1 = 全亮 time.sleep(1) display.show() # 螢幕顯示 adc_pin = Pin(32) adc = ADC(adc_pin) # 創建作用在PIN上的ADC對象 adc.atten(ADC.ATTN_11DB) adc.width(ADC.WIDTH_12BIT) #定義map函數 def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) val_1 = adc.read() # 讀取0-4095中讀原始模擬值 print("val_1:",val_1) val_2 = map(val_1,0,4095,63, 0) #讀取座標初始值 val_3 = val_2 x1 = 0 x2 = 0 while True: val_1 = adc.read() # 讀取0-4095中讀原始模擬值 print("val_1:",val_1) val_2 = map(val_1,0,4095,63, 0) print("val_2:" , val_2) display.line( x1,val_2, x2,val_3, 1) #繪製線條(x1,y1, x2, y2, colour)colour=1為亮, 0 為滅 x2 = x1 x1 = x1+1 val_3 = val_2 print("x1:",x1) print("x2:",x2) print("val_3 :",int(val_3)) display.text(str(val_1), 90, map(val_2,1,63,1,54), 1) # 繪製文字(test, x, y, colour) colour=1為亮, 0 為滅 display.show() time.sleep_ms(200) if x1>90: display.scroll(-1, 0) # 偏移 x1 = 90 time.sleep_ms(10) display.fill_rect(90, 0, 128, 64, 0) # 繪製填充矩形(x1,y1, x2, y2, colour) colour=1為亮, 0 為滅 清除右側數值 display.show() # 螢幕顯示 ``` 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/nHEcVmm.jpg)

    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