- 10KΩ可變電阻
利用類比輸入腳位讀取電壓。將可變電阻的兩端分別接到Arduino的5V和GND,可變電阻中間腳接到A0腳位。
int sensorValue = analogRead(A0) ;
讀取到類比腳位A0的值將介於0~1023之間,透過簡單的數學運算,將讀取到的值轉換成0~5伏特。
float voltage = sensorValue* (5.0/1023.0) ;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600) ;
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(A0) ;
float voltage = sensorValue* (5.0/1023.0) ;
Serial.println(voltage) ;
delay(30) ;
}