Zoyam
    • 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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [社團]mPython讀書會 ## [mPython基本課程](https://hackmd.io/x3cd61mySQyJ9Op5iN3jeA?view) :::info 本講義網址 http://gg.gg/ncbjp ![](https://i.imgur.com/uGf507Q.png) ::: ### 麵包板 [基本]https://kknews.cc/zh-tw/news/pqprzqj.html 轉自台灣MicroPython 陳會安老師 ![](https://i.imgur.com/95tiytW.png) ### AnalogRead應用 [ADC參考資源]http://www.1zlab.com/wiki/micropython-esp32/adc/ #### ADC用法 ``` from machine import ADC adc = ADC(Pin(32)) # create ADC object on ADC pin adc.read() # read value, 0-4095 across voltage range 0.0v - 1.0v adc.atten(ADC.ATTN_11DB) # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v) adc.width(ADC.WIDTH_9BIT) # set 9 bit return values (returned range 0-511) adc.read() # read value using the newly configured attenuation and width ``` #### PWM法用 ``` from machine import Pin, PWM pwm0 = PWM(Pin(0)) # create PWM object from a pin pwm0.freq() # get current frequency pwm0.freq(1000) # set frequency pwm0.duty() # get current duty cycle pwm0.duty(200) # set duty cycle pwm0.deinit() # turn off PWM on the pin pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go ``` ## 可變電阻 ## 光敏電阻 ``` from machine import Pin, PWM, ADC from time import sleep adc = ADC(Pin(32)) # create ADC object on ADC pin adc.atten(ADC.ATTN_11DB) # set 11dB input attenuation (voltage range roughly 0.0v - 3.3v) adc.width(ADC.WIDTH_12BIT) # set 12 bit return values (returned range 0-1023) i = 0 frequency = 5000 PWM_led = PWM(Pin(4), frequency) duty_cycle = 0 while True: i = adc.read() print("value =",i) V = i/4096*3.3 #Caculate the true voltage print("V = ", V) if V < 2: duty_cycle = 0 else: duty_cycle = 1023 PWM_led.duty(duty_cycle) sleep(0.25) ``` ![](https://i.imgur.com/7BXAgOq.png) --- ## 調光器 x WS2812 ### 測試參考 https://docs.singtown.com/micropython/zh/latest/esp32/esp32/tutorial/neopixel.html https://mc.dfrobot.com.cn/thread-305498-1-1.html ---- ### 接線方法 WS2812 DIN -> D4 可變電阻中間角 -> D2 ![](https://i.imgur.com/ZmFl26o.png) ---- ### 控制方法 ``` from machine import Pin from neopixel import NeoPixel pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels np[0] = (255, 255, 255) # set the first pixel to white np.write() # write data to all pixels r, g, b = np[0] # get first pixel colour ``` --- ## Servo Motor 伺服馬達 ![](https://i.imgur.com/gFb3lCT.gif) ### 接線方法 Servo Motor控制訊號為D4(在此接線圖情況下才成立) 輸入訊號為D2(電阻改變 產生電壓改變 進一步控制Servo Motor的PWM數值) ![](https://i.imgur.com/zUo5dcm.png) ### 伺服馬達應用 1. 先使用PWM語法讓馬達可以順利轉動到某個位置 2. 再設計For迴圈 讓馬達可以達到0度自動轉到180度的效果 3. 進一步搭配之前的可變電阻當作輸入,依照轉動的大小,馬達轉動到對應位置 ### 測試範例 [參考資料| ESP32 教學 | MicroPython | PWM Control | 202 |](https://jimirobot.tw/esp32-micropython-tutorial-pwm-control-202/) ``` import machine import time #设置PWM 引脚G5,频率50Hz servo = machine.PWM(machine.Pin(5), freq=50) servo.duty(40)#舵机角度的设定 time.sleep(2)#延时2秒 servo.duty(115) time.sleep(2) servo.duty(180) ``` ESP32 v.s. Servo ``` from machine import Pin,PWM import time sg_pin=PWM(Pin(14),freq=50,duty=0) #duty range 0-1023 d_90=(int)(1023*0.0725) #1.45ms d_zero=(int)(1023*0.025) #0.5ms d_180=(int)(1023*0.12) #2.4ms de_map=[d_90,d_zero,d_90,d_180] try: while 1: for i in de_map: sg_pin.duty(i) time.sleep(1) except Exception as e: print(e) sg_pin.deinit() ``` --- ## [MPU6050 I2C 三軸陀螺儀加速計感測模組](https://micronote.tech/2020/07/I2C-Bus-with-a-NodeMCU-and-MicroPython/) {%youtube 2ufkfd-oFrY %} ### 接線方法 VCC 選3.3V i2c = I2C(scl=Pin(22), sda=Pin(21), freq=) ![](https://i.imgur.com/TT495Yw.png) ### 應用 ### 測試範例 ``` from machine import I2C, Pin import mpu6050, time i2c = I2C(scl=Pin(22), sda=Pin(21)) acc = mpu6050.accel(i2c) count = 600 delta = 0.1 sum_x, sum_y, sum_z = 0, 0, 0 for i in range(count): time.sleep(delta) d = acc.get_values() sum_x += d['GyX'] sum_y += d['GyY'] sum_z += d['GyZ'] avg_x = float(sum_x) / count avg_y = float(sum_y) / count avg_z = float(sum_z) / count with open("avg_values.txt", "wt", encoding='utf-8') as fp: fp.write("x:{}, y:{}, z:{}\n".format(avg_x, avg_y, avg_z)) ``` --- ## [OLED I2C 螢幕](https://nkust.gitbook.io/nodemcu/nodemcu/oled-shi-qi) {%youtube UbYYXtI-0P0 %} ### 接線方法 VCC 選3.3V i2c = I2C(scl=Pin(22), sda=Pin(21), freq=) ![](https://i.imgur.com/87O4Jk5.png) ![](https://i.imgur.com/T1Go5rD.jpg) ### 應用 [power consumption1](https://bitbanksoftware.blogspot.com/2019/06/how-much-current-do-oled-displays-use.html) [power consumption2](https://www.smart-prototyping.com/OLED-0.96inch-12864-display-module-blue.html) 約耗20-30mA 約0.06W ### 測試範例 顯示訊息程式: ``` from machine import I2C, Pin import ssd1306 i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000) oled = ssd1306.SSD1306_I2C(128, 64, i2c) oled.fill(0) oled.text("Hello world!", 0, 0) oled.show() ``` 底下的程式碼是和溫濕度感測器一起工作的例子 ``` import dht, time from machine import I2C, Pin import ssd1306 i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000) oled = ssd1306.SSD1306_I2C(128, 64, i2c) d = dht.DHT11(Pin(0)) while True: d.measure() oled.fill(0) oled.text("{},{}%".format(d.temperature(), d.humidity()), 0, 0) oled.show() time.sleep(0.5) ``` --- ## [NodeMCU與WiFi](https://nkust.gitbook.io/nodemcu/nodemcu/oled-shi-qi) --- ## [把資料傳送到ThingSpeak.com](https://nkust.gitbook.io/nodemcu/nodemcu/ba-liao-song-dao-thingspeak.com) --- ## [MQTT](https://nkust.gitbook.io/nodemcu/nodemcu/mqtt) [GitHub](https://github.com/gloveboxes/ESP32-MicroPython-BME280-MQTT-Sample?fbclid=IwAR18yd1hdN-mb8vMXgFpv006uSllqLU1VuBWrqgPcJcR5YBknsFIAiJdtx4) --- ## [資料傳送給LINE](https://nkust.gitbook.io/nodemcu/nodemcu/ba-liao-song-line) --- ## [即時繪製數據圖檔](https://nkust.gitbook.io/nodemcu/nodemcu/shi-yong-python-cheng-shi-ji)

    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