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);
type - 可以使用任何型別的跟隨(DEFAULT,INTERNAL,INTERNAL1V1,INTERNAL2V56,EXTERNAL)
對於 AREF 引腳上的外部參考電壓,請勿使用低於 0V 或高於 5V 的電壓。如果在 AREF 引腳上使用外部參考,則必須在呼叫 analogRead() 函式之前將模擬參考設定為 EXTERNAL。否則,你將短接有效參考電壓(內部產生)和 AREF 引腳,可能會損壞 Arduino 板上的微控制器。

或者,你可以通過 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
}