第04週- IoT 類比及基本指令
在了解 ESP32 數位基本指令之後,學習較為生動貼近生活的類比訊號以及相關指令。
類比訊號
真實世界的樣貌
連續變化Sin型波:強弱、明暗、冷熱、快慢、深淺、高低音、大小力…
ESP32 同時擁有 DAC 和 ADC (Analog to Digital Converter) 功能
- 輸入:ADC 將類比電壓(Analog voltage)轉換成數位位階(Digital level)
- 輸出:DAC 將數位數字(Digital number) 轉換成類比電壓(Analog voltage)
- 但只有 8-bit (2的8次方) 的品質,別冀望有Hi-Fi的聲音品質
- 只有兩腳位:GPIO25 (DAC channel1), GPIO26 (DAC channel2)
數位如何輸出成類比?
- 方波
- tone(pin, frequency, duration)
- PWM
PWM 技術:
微處理器無法直接輸出類比訊號,而PWM技術使得這種轉換變成普遍且方便。
- 利用數位腳位發送一定頻率的方波,即輸出高電位和低電位,交替持續一段時間。
- 週期(Period, T)是固定
- 輸出高電位的時間一般稱為“脈衝寬度”(Pulse width)
- 佔空比是脈衝持續時間或脈衝寬度(PW)佔波形總週期(T)的百分比
- PWM 佔空比越長,輸出功率就越高
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Arduino IDE 程式語言 PWM
在 Arduino IDE 2.0.4 安裝 ESP32 AnalogWrite 程式庫
- 點選程式庫(Library mgr)
- 鍵入蒐尋關鍵字 esp32 analogWrite
- 選擇 ERROPiX 版本安裝
- 已安裝
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Arduino IDE 程式語言 PWM
analogWrite(pin, value)
- pin: 輸出腳位,任何 ADC,不限於 DAC
- value: 0~255, (2^8 -1, 為 8-bit 解析度)
例題4-1: [漸亮]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Arduino IDE 程式語 for 迴圈
for (initialization; condition; increment) { // statement(s); }
- ex: for (int i = 0; i < 256; i++) {
analogWrite(ledpin, i); delay(20); }
- 呼吸燈:燈光亮滅節奏如呼吸
例題4-2: [呼吸燈]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
閃爍快慢可調的燈號
- analogRead(pin)
- 配合旋鈕可變電阻
例題4-3: [可調變亮滅時間_1]
例題4-4: [可調變亮滅時間_2]
例題4-5: [輸出感測值]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
來點聲音
- 安裝程式庫 ESP32Servo (參照安裝 ESP32 analogWrite 步驟)
- tone(pin, frequency, duration)
- tone(18, 262, 2000): GPIO#18, 262Hz, 2000ms
例題4-6: [蜂鳴器_單長音]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
閃爍快慢可調的燈號和可變的聲音
例題4-7: [蜂鳴器_單長音sine波]
例題4-8: [蜂鳴器_連續sine波]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
使用 analog output (DAC) 輸出聲音
使用 dacWrite(pin, value) 函數做輸出:
- 第一個參數給定 pin
- 對應的 pin 不用特別宣告他的型態
- 通常是 DAC1,DAC2
- 第二個參數給定要輸出的值
例題4-9: [蜂鳴器_for迴圈,變調]
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
來點挑戰吧
- 控制RGB LED變成紫色 (參考課本54頁的資料)
- 加個按鈕來發聲和消音
- 如何使用 Wokwi 的模擬器,加上邏輯分析儀(Logic Analyzer),以 Pulseview 觀看波型。參考影片
- 若已熟悉光敏電阻、人體偵測器,請設計一個自動浴室燈光開關:
- 浴室內有人、昏暗(夜間) :開燈,並持續3分鐘
- 浴室內有人、明亮(白天) :無
- 浴室內無人、昏暗(夜間) :無
- 浴室內無人、明亮(白天) :無
林水忠老的銜接課程
- 課程銜接說明
- 觸控電子琴影片
參考資料
1)IOT物聯網應用第十四章 – 尤濬哲(夜市小霸王) 編著