从模拟引脚获取电压
模拟引脚可用于读取电压,这对电池监控或与模拟设备接口非常有用。默认情况下,AREF 引脚与 arduino 的工作电压相同,但可以在外部设置为其他值。如果要读取的电压大于输入电压,则需要潜在的分压器来降低模拟电压。
#define analogPin 14 //A0 (uno)
#define AREFValue 5 //Standard for 5V Arduinos
#define ADCResolution 1023 //Standard for a 10bit ADC
int ADCValue = 0;
float voltage = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
readADC();
Serial.print(voltage); Serial.println("V");
}
void readADC()
{
ADCValue = analogRead(analogPin);
float = ( ( (float)ADCValue/ADCRange ) * AREFValue ); //Convert the ADC value to a float, devide by the ADC resolution and multiply by the AREF voltage
}