极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27786|回复: 15

电压表的悖论

[复制链接]
发表于 2012-11-28 22:46:50 | 显示全部楼层 |阅读模式
本帖最后由 zintiger 于 2012-11-28 22:48 编辑


用A0端检测给Arduino供电的电池电压,犯了个低级错误,Arduino用电池电压作为基准电压进行模数转换。这样它是测不出给自己供电的电压的(高于5v时应该是可以的),因为A0测得的值永远是1023。请高手给个建议。
下面是源代码:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Voltage of A0");
}

void loop() {
    // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print("               ");
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  delay(500);
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2012-11-28 22:55:57 | 显示全部楼层
用内部基准电压,或者外部弄个tl431作为基准电压
回复 支持 反对

使用道具 举报

发表于 2012-11-29 18:53:15 | 显示全部楼层
电源和地之间串两个电阻分压,ad口读取两个电阻中间点
比如9v电源,串两个5k1电阻,再从中间读数,这样最大电压就被限制为4.5v
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-29 23:43:45 | 显示全部楼层
Malc 发表于 2012-11-29 18:53
电源和地之间串两个电阻分压,ad口读取两个电阻中间点
比如9v电源,串两个5k1电阻,再从中间读数,这样最大 ...

谢谢答复。你说的这种情况对于供电电压大于5v应该是有效的,因为电源大于5V是,有电路把它降到5V作为参考电压,但当电源电压低于5V时,它是不会被提升到5V的,这时参考电压与电源电压相等,于是AD的结果是1023.看来我只能提高电源电压了。
回复 支持 反对

使用道具 举报

发表于 2012-11-30 12:11:35 | 显示全部楼层
zintiger 发表于 2012-11-29 23:43
谢谢答复。你说的这种情况对于供电电压大于5v应该是有效的,因为电源大于5V是,有电路把它降到5V作为参考 ...

貌似avr里头有个1.1v的内部基准电压
回复 支持 反对

使用道具 举报

发表于 2012-11-30 13:14:32 | 显示全部楼层
外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

内部基准电压太低,需要分压,可以参考下面帖子。
http://www.hacktronics.com/Tutorials/arduino-current-sensor.html


我也要做类似测试,有进展再互相分享。

回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-30 16:41:39 | 显示全部楼层
muggle 发表于 2012-11-30 13:14
外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

内部基准电压太低,需要分压 ...

谢谢,他的帖子我看了。也是属于电池电压高于5V而被分压测量的例子。但我还是给他发了电子邮件,向他请教了这个问题。
回复 支持 反对

使用道具 举报

发表于 2012-11-30 17:53:18 | 显示全部楼层
分压可能会用到0.1%精度的精密电阻~
回复 支持 反对

使用道具 举报

发表于 2012-11-30 18:08:27 | 显示全部楼层
精度不够,实际测量得到比例也可以吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-30 21:08:25 | 显示全部楼层
Malc 发表于 2012-11-30 12:11
貌似avr里头有个1.1v的内部基准电压

avr是咋回事?能说说吗?
回复 支持 反对

使用道具 举报

发表于 2012-11-30 21:30:47 | 显示全部楼层
zintiger 发表于 2012-11-30 21:08
avr是咋回事?能说说吗?

调用内部基准可以参考这篇帖子
http://www.geek-workshop.com/thread-1848-1-1.html
回复 支持 反对

使用道具 举报

发表于 2012-12-1 21:03:24 | 显示全部楼层
这个是用内部基准电压修正过的测量方法:http://hacking.majenko.co.uk/mak ... readings-on-arduino
  1. long readVcc() {
  2.   long result;
  3.   // Read 1.1V reference against AVcc
  4.   ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  5.   delay(2); // Wait for Vref to settle
  6.   ADCSRA |= _BV(ADSC); // Convert
  7.   while (bit_is_set(ADCSRA,ADSC));
  8.   result = ADCL;
  9.   result |= ADCH<<8;
  10.   result = 1125300L / result; // Back-calculate AVcc in mV
  11.   return result;
  12. }

  13. void setup() {
  14.   Serial.begin(9600);
  15. }

  16. void loop() {
  17.   Serial.println( readVcc(), DEC );
  18.   delay(1000);
  19. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-12-5 22:20:01 | 显示全部楼层
弘毅 发表于 2012-11-30 21:30
调用内部基准可以参考这篇帖子
http://www.geek-workshop.com/thread-1848-1-1.html

谢谢斑竹,你这篇文章对我很有帮助。现在问题已经解决。顺便提及一下,内部参考电压不都是1.1v,还有2.56v的,这与芯片的型号有关。详细情况请参考Arduino的网站:http://arduino.cc/en/Reference/AnalogReference
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-12-5 22:28:12 | 显示全部楼层
muggle 发表于 2012-11-30 13:14
外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

内部基准电压太低,需要分压 ...

谢谢,我的问题解决了。就是启用内部的参考电压。需要请随时问我,我把我写的源代码给你。
回复 支持 反对

使用道具 举报

发表于 2012-12-5 23:03:23 | 显示全部楼层
zintiger 发表于 2012-12-5 22:28
谢谢,我的问题解决了。就是启用内部的参考电压。需要请随时问我,我把我写的源代码给你。

我在看这个资料,要测试光伏系统的电压和电流,ACS712在路上,很快就到手,可以试验试验。


This Arduino based current, voltage, and power sensor/meter tutorial
http://www.hacktronics.com/Tutorials/arduino-current-sensor.html
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-5 01:14 , Processed in 0.059192 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表