基于Arduino的电阻计
wasdpkj 于 2013-1-30 23:59 编辑
晚上闲着,写了一个测电阻的程序,
原理是通过基准电阻,对比测试端电压,比较后经过算法测出电阻,经测试,以20k为基准,测大于1k电阻时比较准确。
2013-1-30 22:27 上传
(35.7 KB)
可以根据待测量的范围更换基准电阻以求准确
接线如下(电容用的104瓷片):
2013-1-30 22:24 上传
(241.75 KB)
程序,用了均值滤波算法:
ARDUINO 代码复制打印
#define N 12#define basis 20000.0int potpin = A4;float val, r;void setup(){ Serial.begin(9600);}void loop(){ val = analogRead(potpin); r = ((basis * 1023.0) / (1023.0 - val)) - basis; delay(100); Serial.print(" R:"); if(filter() >= 1000) { if(filter() >= 500.01000.0) { Serial.println("Infinity!!"); } else { Serial.print(filter() / 1000.0); Serial.println("K ohm"); } } else { Serial.print(filter()); Serial.println(" ohm"); }}float filter(){ float sum = 0; for(int count = 0; count < N; count++) { sum += r; delay(5); } return (float)(sum / N);}
#define N 12
#define basis 20000.0
int potpin = A4;
float val, r;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin);
r = ((basis * 1023.0) / (1023.0 - val)) - basis;
delay(100);
Serial.print(" R:");
if(filter() >= 1000)
{
if(filter() >= 500.01000.0)
{
Serial.println("Infinity!!");
}
else
{
Serial.print(filter() / 1000.0);
Serial.println("K ohm");
}
}
else
{
Serial.print(filter());
Serial.println(" ohm");
}
}
float filter()
{
float sum = 0;
for(int count = 0; count < N; count++)
{
sum += r;
delay(5);
}
return (float)(sum / N);
}
via - 极客工坊