Reading an analog value with the ESP32 means you can measure varying voltage levels between 0 V and 3.3 V.
12-bit resolution. This means that when you read an analog input, its range may vary from 0 to 4095.
Non-linear
ESP32 is not able to distinguish 3.3 V from 3.2 V. You'll get the same value for both voltages: 4095.
ESP32 is not able to distinguish 0 V from 0.13 V. You'll get the same value: 0.
ESP32 supports measurements in 18 different channels
ADC1_CH0 (GPIO 36)
ADC1_CH1 (GPIO 37)
ADC1_CH2 (GPIO 38)
ADC1_CH3 (GPIO 39)
ADC1_CH4 (GPIO 32)
ADC1_CH5 (GPIO 33)
ADC1_CH6 (GPIO 34)
ADC1_CH7 (GPIO 35)
ADC2_CH0 (GPIO 4):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH1 (GPIO 0):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH2 (GPIO 2):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH3 (GPIO 15):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH4 (GPIO 13):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH5 (GPIO 12):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH6 (GPIO 14):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH7 (GPIO 27):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH8 (GPIO 25):ADC2 pins cannot be used when Wi-Fi is used
ADC2_CH9 (GPIO 26):ADC2 pins cannot be used when Wi-Fi is used
完成之積木程式
Arduino IDE 開啟載入程式碼(.ino),選擇正確控制板,選擇正確連接埠,上傳燒錄。開啟序列埠視窗,baudrate 9600
analogReadResolution(resolution); // 設定解析度, 數值:9(0 – 511)~12(0 – 4095), 預設:12 bits(0 – 4095)
...
analogSetWidth(width); // 設定解析度, 數值:9(0 – 511)~12(0 – 4095), 預設:12 bits(0 – 4095)
...
analogSetCycles(cycles); // set the number of cycles per sample. Default is 8. Range: 1 to 255.
...
analogSetSamples(samples); // set the number of samples in the range. Default is 1 sample. It has an effect of increasing sensitivity.
...
analogSetClockDiv(attenuation); // set the divider for the ADC clock. Default is 1. Range: 1 to 255.
...
analogSetAttenuation(attenuation); // sets the input attenuation for all ADC pins. Default is ADC_11db. Accepted values:
ADC_0db: sets no attenuation. ADC can measure up to approximately 800 mV (1V input = ADC reading of 1088).
ADC_2_5db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1100 mV. (1V input = ADC reading of 3722).
ADC_6db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1350 mV. (1V input = ADC reading of 3033).
ADC_11db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 2600 mV. (1V input = ADC reading of 1575).
...
analogSetPinAttenuation(pin, attenuation); // sets the input attenuation for the specified pin. The default is ADC_11db. Attenuation values are the same from previous function.
...
adcAttachPin(pin); // Attach a pin to ADC (also clears any other analog mode that could be on). Returns TRUE or FALSE result.
...
adcStart(pin);, adcBusy(pin); and resultadcEnd(pin); // starts an ADC convertion on attached pin’s bus. Check if conversion on the pin’s ADC bus is currently running (returns TRUE or FALSE). Get the result of the conversion: returns 16-bit integer.
analogRead(GPIO); // 讀值
由序列埠視窗擷取儲存
將資料串接給 EXCEL (PAL_DAQ) 儲存,即時繪圖
將資料串接給通訊埠軟體儲存,再利用 Kst-Plot 即時繪圖
將資料儲存於 SD 卡
將資料串接給手機 App 儲存
將資料傳上雲端(IoT)
將資料透過通訊模組傳送,手機 App 接收儲存繪圖(IoT)
將資料串接給 EXCEL 儲存,即時繪圖
下載 PLX-DAQ-v2.11.zip。接壓縮後,參考資料夾內 Beginners Guide to PLX DAQ v2 (rev1).doc,依說明,要透過序列部串接 arduino 感測資料給 EXCEL 檔,序列埠資料格式如下:
setup():
Serial.println("CLEARSHEET"); //清空工作表所有資料(包含欄位名)
Serial.println("CLEARDATA"); //保留欄位名,刪除欄位名下所有資料
Serial.println("LABEL,欄位名一,欄位名二,欄位名三,......"); //設定作用工作表的第一列(標題列),將指定之欄位名寫入第一列
loop():
Serial.println("DATA,DATE,TIME,資料一,資料二,資料三,......."); //將感測資列由第一欄起依序寫入一列中。DATE:寫入系統日期,TIME:寫入系統時間
Serial.println("AUTOSCROLL_20"); 隨著資料一列列寫入,向上捲動,顯示最後 20 列資料。
完成之積木程式
Arduino IDE 開啟載入程式碼(.ino),選擇正確控制板,選擇正確連接埠,上傳燒錄。開啟序列埠視窗,baudrate 9600
關閉序列埠視窗(一定要)
開啟 PortableArduino資料夾(C:\Arduino)\Excel\PLX-DAQ-v2.11.xlsm
自訂 EXCEL 欄位名:這兒需四個欄位。日期、時間、光敏電阻類比感測值、光敏電阻數位感測值
設定ESP8266序列埠(COMxx)
設定序列埠傳輸速率(9600)
連線,開始接收數據
資料導入 EXCEL了,可做進一步科學上的統計分析
選取 Plot 工作表,可見即時繪圖