arduino autorange ohmmeter

i’m trying to make a ohmmeter with an arduino and i’m having a program making it auto range. i have been trying it a few different ways, both on the hardwear side and in the code but i cant seem to make any head way.
i have some diodes(1n4148 and have tried 1n4003) in series with my contral resisters. im using a voltage devider with R1 as my constant and the lower resister is what im putting between leads with analogRead delow the diode.

now A1 is telling me, with a 6k7 as R6 ohm is .057v, 1299.4350ohm, range 2, analog pin0 115
that all seems a little random to me. i know the forword voltage of the diode is going to change the reading i get some but going to a different restister just seems to give a nother random reading.

does any one know another approach to this?

  // _____ 
  // - | _R1__ | - VCC 
  // AnalogPin - '_____ 
  // '- | _R2__ | - GND 

  // R2 = resistance to be measured 

int AnalogPin = 0;     // potentiometer wiper (middle terminal) connected to analog pin 3
int Alog1;
int Alog2;
int Alog3;
int Alog4;
int Alog5;

                       // outside leads to ground and +5V
int raw = 0;           // variable to store the raw input value
int Vin = 5;           // variable to store the input voltage
float Vout = 0;        // variable to store the output voltage
float R1 = 10;         // variable to store the R1 value
float R2 = 0;          // variable to store the R2 value
float buffer = 0;      // buffer variable for calculation
int range = 200;


  void setup () 
  { 
  Serial.begin (9600); 
  Serial.println ("Measure resistance"); 
  Serial.println (); 
  } 

  void loop () 
  { 
	digitalWrite(9, LOW);
	digitalWrite(10, LOW);
	digitalWrite(11, LOW);
	digitalWrite(12, LOW);
	range = 0;
	R1 =1000000;//1g ohm
	digitalWrite(8, HIGH);
	raw = analogRead(AnalogPin);

	if(raw < 100){
		digitalWrite(8, LOW);
 		digitalWrite(9, HIGH);
		raw = analogRead(AnalogPin);
		range = 1;
		R1 =100000;//100k ohm
  		} 

	if(raw < 100){
		digitalWrite(9, LOW);
 		digitalWrite(10, HIGH);
		raw = analogRead(AnalogPin);
		range = 2;
		R1 =10000;//10k ohm
  		} 

	if(raw < 100){
		digitalWrite(10, LOW);
 		digitalWrite(11, HIGH);
		raw = analogRead(AnalogPin);
		range = 3;
		R1 =1000;//1k ohm
  		} 

	if(raw < 100){
		digitalWrite(11, LOW);
 		digitalWrite(12, HIGH);
		raw = analogRead(AnalogPin);
		range = 4;
		R1 =100;//100 ohm
  		} 
Alog1 = analogRead(1);
Alog2 = analogRead(2);
Alog3 = analogRead(3);
Alog4 = analogRead(4);
Alog5 = analogRead(5);




  Vout = (5.0 / 1000.0) * raw; 
  buffer = (Vin / Vout) - 1;
  R2 = (R1 / buffer); 

  Serial.print("Voltage: ");      //
  Serial.println(Vout);           // Outputs the information
  Serial.print ("resistance "); 
  Serial.print (R2, DEC); 
  Serial.println (" ohm.");  
  Serial.print ("range: "); 
  Serial.println(range, 1);
  Serial.print ("analogPin: ");
  Serial.println (raw, DEC);
  Serial.print ("Alog1: ");
  Serial.println (Alog1, DEC);
  Serial.print ("Alog2: ");
  Serial.println (Alog2, DEC);
  Serial.print ("Alog3: ");
  Serial.println (Alog3, DEC);
  Serial.print ("Alog4: ");
  Serial.println (Alog4, DEC);
  Serial.print ("Alog5: ");
  Serial.println (Alog5, DEC);
  Serial.println ("**************************");
  delay (3000);


  }

You don't need diodes. The Arduino ports are excellent switches, you set them to be an output when you want to connect your "range" resistor, and you set them to be an input (but written "low" so you do not enable the internal pull-up) when you want to disconnect the particular range resistor.

What you do need is a "current mirror" using a transistor (or pair of transistors) to make a constant current source rather than simply a resistor in series with your measurement point.

i have never used a current mirror before. i just googled it but im not sure i understand how it works. if the left side has 5v at 50mA and the right side has ?v at 50mA? so the so the left side changes the resistances of the right transister to keep the apms the same??

i will have to find some transisters and make one so i can put a volt meter to it and see what it does.

but please, is this how your are talking about taking the reading?

That would be the general idea except that you would want it the other way up so that your measured resistance goes to ground, and use a matched pair of PNP transistors on the "high side". They make transistor pairs - a single package - for this specific purpose though a pair of SMD transistors from the same tape and placed close together would most likely serve the purpose.

The other advantage of the mirror on the "high side" is that you set the control ports to always output low (disabling pull-ups) and simply switch between input (open circuit) and output (resistor is selected).

eliofall:
i have never used a current mirror before. I just googled it but I'm not sure I understand how it works. if the left side has 5v at 50mA and the right side has ?v at 50mA? so the so the left side changes the resistances of the right transistor to keep the arms the same??

Pretty much. Don't need to understand how it works, just use it!

i have been reading up on current mirrors and i am still not fully sure what i need to do.

your talking about using a circuit like this, and i think, between Q2 and RE is where i should take analogRead()

am i on there right track?

why would i use SMD rather then a something i can solder with out a reflow or a microscope?

why pnp rather then npn or mosfet?

the Wilson current mirror that you linked to uses three npn transistors. i am getting a little lost in the math of how to solve for load.

i understand that i set current with R1. i have the unknown R between the collector of Q2 and ground. then analogRead at Load float=(5.0/1023.0)/(setAmps) am i understanding it right?

eliofall:
I have been reading up on current mirrors and i am still not fully sure what i need to do.

It should boil down to “just use the circuit”. :smiley:

eliofall:

your talking about using a circuit like this, and i think, between Q2 and RE is where I should take analogRead(). Am I on the right track?

Err, no. In that circuit, the important thing is that the two RE are equal. The i1 source sets the current and the mirror feeds (or sinks) the same current to i2 which is where you put your measuring terminal which is also where you measure the voltage. The trick is that using a resistor to set i1 requires it to be calculated for the voltage drop which then depends on the Vbe of the transistors (about 0.65V) and the drop across RE. Essentially your resistor plus RE will correspond to the supply voltage minus Vbe divided by the desired current.

But another problem may be that you want the voltage across RE to be significant to make the mirror work at its best, so you are losing more voltage than you might prefer to. The “Wilson” mirror loses much less voltage on the “Load” side (though R1 must be calculated on the supply voltage minus 2 times Vbe) so would be easier to work with. Transistors are really cheap.

Do not be confused by the fact that the transistor with collector connected to base is on the opposite side in the two circuits - that is precisely how they work!

eliofall:
Why would I use SMD rather then a something i can solder with out a reflow or a microscope?

Because they can be mounted much closer on a PCB to ensure thermal compensation. Maybe that matters, maybe it does not, or you can get transistor packs designed for the purpose with both (or the Wilson three) transistors in the one pack specifically for this purpose - they might actually be leaded (such as in TO-3 cans!) or SMD.

eliofall:
Why pnp rather then npn or mosfet?

Because you have to turn the circuit upside down so that the load goes to earth as does “R1”. MOSFETs have too high a VGS threshold to be used at 5V.

eliofall:
The Wilson current mirror that you linked to uses three npn transistors. I am getting a little lost in the math of how to solve for load.

I understand that I set current with R1. I have the unknown R between the collector of Q2 and ground. Then analogRead at Load float=(5.0/1023.0)/(setAmps) am I understanding it right?

I think that is correct.

Again, the math is dead easy. Do remember that it is turned upside down, so that all are PNP and the emitters of Q1 and Q2 are going to 5V (you could use a higher voltage if it was accurately regulated). The unknown and your analog measuring input are on the collector of Q3.

Each R1 switched to ground will be supplied by 5V minus 2 x Vbe (the drop of Q2 plus that of Q3), so 3.7V. That allows you to calculate the values. That’s all the math you need to know (just as well since I have not looked into it in any more detail myself :D).