什么是讀取模擬收入()
該示例展示了如何讀取模擬輸入信號兩個模擬信號如何同時輸入到單片機,實例中采用了一只電位器。電位器()是一個簡單的機械式器件兩個模擬信號如何同時輸入到單片機,當(dāng)轉(zhuǎn)動旋轉(zhuǎn)軸時,它可以提供不同的電阻值。通過將經(jīng)由一個電位器的電壓傳遞給板的模擬輸入端,就有可能以模擬量的形式測量出該電位器分出的電阻值。在本示例中,當(dāng)或與電腦建立好串行通信并且運行 軟件(IDE)后,就可以監(jiān)測電位器的狀態(tài)。
所需硬件 控制板10K電位器 電路連接方式
通過轉(zhuǎn)動電位器的旋轉(zhuǎn)軸,將改變電刷兩側(cè)的電阻值,該電刷與電位器中間的引腳相連。這樣就可以改變中心引腳上的電壓值。當(dāng)中間引腳與連接5伏引腳之間的電阻接近于0時(同時另一側(cè)引腳的電阻值接近于10k),中間引腳上的電壓接近于5V。反之,中間引腳上的電壓接近于0V。該電壓就是要讀取的模擬電壓作為輸入信號。
和板內(nèi)部有一個模擬數(shù)字轉(zhuǎn)換器(-to- )的電路,可以讀取這種變化的電壓并將其轉(zhuǎn)換成0到1023之間的數(shù)字。當(dāng)電刷完全轉(zhuǎn)向一側(cè)時,引腳上的電壓為0V,此時輸入值為0。當(dāng)電刷轉(zhuǎn)向反方向時,引腳上的電壓為5V,此時輸入值為1023。當(dāng)電刷在中間某個位置時,()將根據(jù)引腳分得的電壓值成比例返回一個0到1023之間的數(shù)值。
代碼
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}