技术碎片
技术记录
帮助交流
recommend
Arduino检测剩余电池电量
代码示例:

void setup() {
  Serial.begin(115200);
  while (!Serial);
  pinMode(A0, INPUT_PULLUP);
}
void loop() {
  double voltage = (double)analogRead(A0) / 1023 * 5;
  double percent = voltage - 1.2;
  percent = percent / 0.4 * 100;    if (percent > 100)
  {
    percent = 100;
  }
  Serial.print(voltage);
  Serial.print("Volt     ");
  Serial.print(percent);
  Serial.println("%");
  delay(500);
}

把代码烧录至开发板内

拿2根杜邦线一根插入A0端口 一根插入GND端口

A0触碰电池正极 GND触碰电池负极

打开串口调试就能看到电池的具体数据了
转载请注明来源地址:www.zhyunxuan.com 更新时间 2021-09-18  3149
梦想有多远,路就有多远