changed 3 years ago
Published Linked with GitHub

micropython 讀取ADC繪製歷史曲線(Line Plot)

LHB阿好伯, 2022/06/03

tags: Micropython

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 →

ADC介紹

在 ESP32 上,引腳 32-39(ADC 模組 1)和引腳 0、2、4、12-15 和 25-27(ADC 模組 2)上提供 ADC 功能

ADC模組2也被WiFi使用,因此當WiFi處於活動狀態時
從模組2引腳讀取模擬值將引發異常。

輸入引腳的最大額定電壓為3.6V

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) 開發板模組

在Arduino 中有一個很好用的map函數
他可以將輸入值依照最大最小值依自己需求進行轉換
例如ADC 12bit讀取到0 ~ 4095的數值要轉換為0~100
就可以使用map函數進行轉換
而micropython則只需兩行程式碼自行定義出這個函數

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)

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/

有所錯誤歡迎指教

全部文章列表

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 →

Select a repo