zintiger 发表于 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);
}

zcbzjx 发表于 2012-11-28 22:55:57

用内部基准电压,或者外部弄个tl431作为基准电压

Malc 发表于 2012-11-29 18:53:15

电源和地之间串两个电阻分压,ad口读取两个电阻中间点
比如9v电源,串两个5k1电阻,再从中间读数,这样最大电压就被限制为4.5v

zintiger 发表于 2012-11-29 23:43:45

Malc 发表于 2012-11-29 18:53 static/image/common/back.gif
电源和地之间串两个电阻分压,ad口读取两个电阻中间点
比如9v电源,串两个5k1电阻,再从中间读数,这样最大 ...

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

Malc 发表于 2012-11-30 12:11:35

zintiger 发表于 2012-11-29 23:43 static/image/common/back.gif
谢谢答复。你说的这种情况对于供电电压大于5v应该是有效的,因为电源大于5V是,有电路把它降到5V作为参考 ...

貌似avr里头有个1.1v的内部基准电压

muggle 发表于 2012-11-30 13:14:32

外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

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


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

zintiger 发表于 2012-11-30 16:41:39

muggle 发表于 2012-11-30 13:14 static/image/common/back.gif
外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

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

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

弘毅 发表于 2012-11-30 17:53:18

分压可能会用到0.1%精度的精密电阻~

muggle 发表于 2012-11-30 18:08:27

精度不够,实际测量得到比例也可以吧。

zintiger 发表于 2012-11-30 21:08:25

Malc 发表于 2012-11-30 12:11 static/image/common/back.gif
貌似avr里头有个1.1v的内部基准电压

avr是咋回事?能说说吗?

弘毅 发表于 2012-11-30 21:30:47

zintiger 发表于 2012-11-30 21:08 static/image/common/back.gif
avr是咋回事?能说说吗?

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

zhujunsan 发表于 2012-12-1 21:03:24

这个是用内部基准电压修正过的测量方法:http://hacking.majenko.co.uk/making-accurate-adc-readings-on-arduinolong readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1125300L / result; // Back-calculate AVcc in mV
return result;
}

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println( readVcc(), DEC );
delay(1000);
}

zintiger 发表于 2012-12-5 22:20:01

弘毅 发表于 2012-11-30 21:30 static/image/common/back.gif
调用内部基准可以参考这篇帖子
http://www.geek-workshop.com/thread-1848-1-1.html

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

zintiger 发表于 2012-12-5 22:28:12

muggle 发表于 2012-11-30 13:14 static/image/common/back.gif
外部参考电压,确实是个问题,一比一永远是1023,内部基准电压也许能解决。

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

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

muggle 发表于 2012-12-5 23:03:23

zintiger 发表于 2012-12-5 22:28 static/image/common/back.gif
谢谢,我的问题解决了。就是启用内部的参考电压。需要请随时问我,我把我写的源代码给你。

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


This Arduino based current, voltage, and power sensor/meter tutorial
http://www.hacktronics.com/Tutorials/arduino-current-sensor.html
页: [1] 2
查看完整版本: 电压表的悖论