[Analog I/O]
描述
從指定的 pin 腳讀取類比電壓訊號,86Duino 板擁有數個通道 (在 ZERO 和 EduCake 上有 6 個,ONE 上有 7 個) 可以將類比訊號轉換成 10-bit 的數位訊號,換句話說,此函式會將 0 到 3.3 伏特的輸入電壓轉換成 0 到 1023 的整數 (或者是 0 到 2047,藉由 analogReadResolution()
來設定,可以讓此函式的回傳值解析度提升至最高 11-bit)。預設的解析度為 3.3 伏特/1024 單位或相當於 0.0032 伏特 (3.2 毫伏特)/1 單位。
此函式每次會花費 15 微秒 (0.000015s) 的時間來讀取類比訊號,所以讀取頻率約是一秒 66666 次。
另外,由於 86Duino PLC 上沒有類比電壓通道,使用此函式取得的 10-bit 值將不具任何意義。
語法
analogRead(pin)
參數
pin
: 類比訊號輸入 pin 腳的編號 (在 Zero 和 EduCake 上是 0~5,在 ONE 上是 0~6)
回傳
int (預設是 0 到 1023)
注意
如果類比訊號輸入 pin 腳上沒有連接任何東西, analogRead()
將回傳由某些因素 (例:其他的數位訊號輸入、使用者的手靠近板子、…) 所造成的浮動數值。
在 86Duino 板上,類比轉數位訊號的最大輸入電壓可以達到 3.3 伏特,所以使用者必須注意每個類比訊號輸入不要大過這個限制,如果輸入類比訊號 pin 腳的電壓超過 3.3 伏特,轉換出來的數值會變得很奇怪,甚至有可能會燒毀電路板。
範例程式碼
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3 // outside leads to ground and +5V int val = 0; // variable to store the value read void setup() { Serial.begin(9600); // setup serial } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value }
參考
- [程式語法] analogReadResolution()
- [Example] Tutorial: Analog Input Pins
語法參考主頁面
86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。