Hello,
I'm working on an Arduino based Ohm meter for a little instrument panel. I followed a tutorial some time back that gave a design like this:
This is the code:
//the LCD display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//for timing purposes to avoid delay()
unsigned long timer;
//resistor meter
int resistorPin = A0;
float R1 = 0;
float R2 = 0;
int v = 0;
float vin = 5; //it actually is!
float vout = 0;
float buffer = 0;
int voltOn = 2;
void setup(){
Serial.begin(9600);
pinMode(12, INPUT);
pinMode(11, INPUT);
pinMode(10, INPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
pinMode(voltOn, OUTPUT);
pinMode(resistorPin, INPUT);
//Use external voltage reference
//analogReference(EXTERNAL); //Didn't work as well as NOT using it
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop(){
if (millis() - timer >= 1000UL){
timer = millis();
digitalWrite(voltOn, HIGH);
//start searching with 470000
//470000 (this worked better that 1M)
R1 = 470000;
pinMode(12, INPUT);
pinMode(11, INPUT);
pinMode(10, INPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
v = analogRead(resistorPin);
buffer = v * vin;
vout = (buffer)/1023;
R2 = (vout*R1)/(vin-vout);
if (R2 > 100000 && R2 < 2000000){
if (R2 > 1000000){
R2 = R2/1000000;
clearLine(1);
lcd.print(R2);
lcd.print("M");
lcd.print(char(244));
}
else{
R2 = R2/1000;
clearLine(1);
lcd.print(R2);
lcd.print("K");
lcd.print(char(244));
}
return;
}
else{
//100000
R1 = 100000;
pinMode(12, INPUT);
pinMode(11, INPUT);
pinMode(10, OUTPUT);
pinMode(9, INPUT);
digitalWrite(10, LOW);
v = analogRead(resistorPin);
buffer = v * vin;
vout = (buffer)/1023;
R2 = (vout*R1)/(vin-vout);
if (R2 > 10000 && R2 < 100000){
R2 = R2/1000;
clearLine(1);
lcd.print(R2);
lcd.print("K");
lcd.print(char(244));
return;
}
else{
//10000
R1 = 10000;
pinMode(12, INPUT);
pinMode(11, OUTPUT);
pinMode(10, INPUT);
pinMode(9, INPUT);
digitalWrite(11, LOW);
v = analogRead(resistorPin);
buffer = v * vin;
vout = (buffer)/1023;
R2 = (vout*R1)/(vin-vout);
if (R2 > 1000 && R2 < 10000){
R2 = R2/1000;
clearLine(1);
lcd.print(R2);
lcd.print("K");
lcd.print(char(244));
return;
}
else{
//1000
R1 = 1000;
pinMode(12, OUTPUT);
pinMode(11, INPUT);
pinMode(10, INPUT);
pinMode(9, INPUT);
digitalWrite(12, LOW);
v = analogRead(resistorPin);
buffer = v * vin;
vout = (buffer)/1023;
R2 = (vout*R1)/(vin-vout);
if (R2 > 0 && R2 < 1000){
if (R2 > 1000){
R2 = R2/1000;
clearLine(1);
lcd.print(R2);
lcd.print("K");
lcd.print(char(244));
}
else{
clearLine(1);
lcd.print(R2);
lcd.print(char(244));
return;
}
}
else{
clearLine(1);
lcd.print("Insert resistor");
return;
}
}
}
}
}
}
void clearLine(int line){
lcd.setCursor(0,line);
lcd.print(" ");
lcd.setCursor(0,line);
}
The only way this circuit seems to work is by alternately setting the correct known resistors to HIGH (and the unused to INPUT) and replacing D2 with ground. I can't replace D2 with ground as The terminal D2 is wired to will be using other connections later that are planned but not yet implemented. I figured I could turn off D2 by setting it to INPUT. Unfortunately The way it stands above it isn't even close to accurate. As I said, replacing D2 with a ground connecting during testing does give extremely close results but I was hoping I wouldn't have to do that. Any thoughts? Is there any substance to this design?