Arduino - 高階 I/O 函式



本章我們將學習一些高階的輸入輸出函式。

analogReference() 函式

配置用於模擬輸入的參考電壓(即用作輸入範圍上限的值)。選項包括:

  • DEFAULT - 預設模擬參考電壓為 5 伏(在 5V Arduino 開發板上)或 3.3 伏(在 3.3V Arduino 開發板上)

  • INTERNAL - 內建參考電壓,在 ATmega168 或 ATmega328 上等於 1.1 伏,在 ATmega8 上等於 2.56 伏(Arduino Mega 上不可用)

  • INTERNAL1V1 - 內建 1.1V 參考電壓(僅限 Arduino Mega)

  • INTERNAL2V56 - 內建 2.56V 參考電壓(僅限 Arduino Mega)

  • EXTERNAL - 將應用於 AREF 引腳的電壓 (0 到 5V) 用作參考電壓

analogReference() 函式語法

analogReference (type);

型別 - 可以使用以下任何型別 (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, EXTERNAL)

不要在 AREF 引腳上使用低於 0V 或高於 5V 的外部參考電壓。如果使用 AREF 引腳上的外部參考電壓,則必須在呼叫 analogRead() 函式之前將模擬參考電壓設定為 EXTERNAL。否則,您將短路活動參考電壓(內部生成的)和 AREF 引腳,可能會損壞 Arduino 開發板上的微控制器。

MicroController

或者,您可以透過 5K 電阻將外部參考電壓連線到 AREF 引腳,從而允許您在外部和內部參考電壓之間切換。

請注意,由於 AREF 引腳上有一個內部 32K 電阻,因此電阻會改變用作參考的電壓。兩者充當分壓器。例如,透過電阻施加 2.5V 將在 AREF 引腳上產生 2.5 * 32 / (32 + 5) = ~2.2V。

示例

int analogPin = 3;// potentiometer wiper (middle terminal) connected to analog pin 3 
int val = 0; // variable to store the read value

void setup() {
   Serial.begin(9600); // setup serial
   analogReference(EXTERNAL); // the voltage applied to the AREF pin (0 to 5V only) 
      // is used as the reference.
}

void loop() {
   val = analogRead(analogPin); // read the input pin
   Serial.println(val); // debug value
}
廣告