analogRead()

描述

從指定的 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;     // 可變電阻 (中間腳) 與類比pin3連接
                       // 外部接地與輸入電壓 5 伏特
int val = 0;           // 儲存讀入數值的變數

void setup()
{
  Serial.begin(9600);          //  設置序列埠
}

void loop()
{
  val = analogRead(analogPin);    // 讀取輸入 pin 腳
  Serial.println(val);            // 除錯數值
}

See also

analogReadResolution()
Tutorial: Analog Input Pins


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.