极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12493|回复: 2

求助arduino 测量大于5v电压疑问

[复制链接]
发表于 2013-8-4 23:45:10 | 显示全部楼层 |阅读模式
      下述代码例子是通过2个1K电阻进行分压,来测量范围0-10v的电压,例子中resistorFactor = 1023.0 / (R2/(R1 + R2));  这句是不是写错了?应该是resistorFactor = 1023.0 * (R2/(R1+R2)); 这样结果正好是511,与下述图表中的value of resistorFactor 吻合。





/*
  DisplayMoreThan5V sketch
  prints the voltage on analog pin to the serial port
  Do not connect more than 5 volts directly to an Arduino pin.
*/

const float referenceVolts = 5;   // the default reference on a 5-volt board
//const float referenceVolts = 3.3;  // use this for a 3.3-volt board

const float R1 = 1000; // value for a maximum voltage of 10 volts
const float R2 = 1000;

// determine by voltage divider resistors, see text
const float resistorFactor = 1023.0 / (R2/(R1 + R2));  //此处是否应该是resistorFactor = 1023.0 * (R2/(R1+R2));
const int batteryPin = 0;   // +V from battery is connected to analog pin 0

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

void loop()
{
   int val = analogRead(batteryPin);  // read the value from the sensor
   float volts =
     (val / resistorFactor) * referenceVolts ; // calculate the ratio
   Serial.println(volts);  // print the value in volts
}
      
   

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2013-8-5 06:26:22 | 显示全部楼层
you are right!
回复 支持 反对

使用道具 举报

发表于 2013-8-5 10:49:01 | 显示全部楼层
对不对,自己实际测试一下便知~
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-6 20:11 , Processed in 0.057512 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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