本站改版新增arduino频道
arduino 电压检测模块
const int voltagePin = 0; // 使用GPIO34作为测量引脚
float R1 = 30000.0; // 上分压电阻值(欧姆)
float R2 = 7500.0; // 下分压电阻值(欧姆)
float refVoltage = 3.3; // ESP32 ADC参考电压(通常为3.3V)
int adcResolution = 4095; // ESP32 ADC分辨率(12位)
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 设置ADC为12位分辨率
}
void loop() {
int adcValue = analogRead(voltagePin);
float voltage = adcValue * (refVoltage / adcResolution);
// 计算实际电压(考虑分压比)
float actualVoltage = voltage * ((R1 + R2) / R2)-0.48; // 0.48 用万用表校正
Serial.print("ADC值: ");
Serial.print(adcValue);
Serial.print(" | 测量电压: ");
Serial.print(voltage, 3);
Serial.print("V | 实际电压: ");
if(actualVoltage>0){Serial.print(actualVoltage, 2);}
if(actualVoltage<0){Serial.print("0");}
Serial.println("V");
delay(1000);
}
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0093798637390137 seconds