Wheatstone bridge

I want to find the value of Rg from the one in the picture with the formula listed there

in my experiments, I used four 1000 ohm resistor

I have this code,

float Vin = 5.0;
float Vout = 0;
float R1 = 1000;
float R2 = 1000;
float R3 = 1000;
float Rg = 0;
int Vbin = 0;


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

void loop()

{

  Vbin = analogRead(A0);
  Vin = Vbin * (Vin / 1023.0);
  Rg = (( R3 * Vin) / (Vout + (( R2 * Vin) / (R1 + R2))) - R3);
  Serial.print(Rg);Serial.println(" Ohm");
  delay(1000);
}

can you help me?

sorry if my english so bad :frowning:

RG = ( R3 / ( (V_out / V_in) + ( R2 / (R1+R2) ) ) ) - R3

School excercise?

An expression can't have too many parentheses, can it?

after I tried this formula, the calculation of which I can still not right

Docedison:
School excercise?

no,

In your case , yes it can. You have one set of redundant parentheses. Good equation writing uses the minimum necessary. In the case of a computation the same form is generally used but using different forms will make it a little easier for a reader to interpret

RG = R3 / { (V_out / V_in) + [ R2 / (R1+R2) ] } - R3

The compiler doesn't care, no. If in doubt, put them in.

OTOH it can be much better/safer to do the calculation in smaller steps, eg.:

const float B = ( R2 * Vin) / (R1 + R2);
Rg = (( R3 * Vin) / (Vout + B) - R3);

This makes it far easier to read/debug.

An expression can’t have too many parentheses, can it?

In theory yes (compiler has its limits) in practice no (the limits are high).

to measure Vin quite precisely you might want to mix in this code

//
// https://code.google.com/p/tinkerit/wiki/SecretVoltmeter
//
long 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 = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

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

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

As far as having “too many parentheses”… I forgot to insert that darned smiley!

Well, I think that I rearranged the expression for RG right.
If that doesn’t produce the desired result then what is the question (again)?

I did not know that we can use { } and , too. (I know that we do in Algebra class, but, anyway.)

We can't...not in C++.

ThinkI found the error in your code …

You recalculate Vin instead of Vout and as Vout is defined 0 the formula will fail…

void loop()
{
  Vbin = analogRead(A0);
  Vin = Vbin * (Vin / 1023.0);  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< should be something like Vout = Vbin * (5.0 /1023);  ???
  Rg = (( R3 * Vin) / (Vout + (( R2 * Vin) / (R1 + R2))) - R3);
  Serial.print(Rg);Serial.println(" Ohm");
  delay(1000);
}

Give it a try

robtillaart:
ThinkI found the error in your code …

You recalculate Vin instead of Vout and as Vout is defined 0 the formula will fail…

void loop()

{
  Vbin = analogRead(A0);
  Vin = Vbin * (Vin / 1023.0);  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< should be something like Vout = Vbin * (5.0 /1023);  ???
  Rg = (( R3 * Vin) / (Vout + (( R2 * Vin) / (R1 + R2))) - R3);
  Serial.print(Rg);Serial.println(" Ohm");
  delay(1000);
}




Give it a try

i try this code and i I get a negative result,
and i try all code in this thread but it seems all is wrong, because when I change his resistor can result in 1000ohm
in the original case I use 4 resistors 1000ohm and I also use the RG 1000ohm and sometimes I replace the RG resistor value is smaller and its results remain 1000ohm

I apologize if you are very troublesome, because I was just learning Arduino :frowning:

robtillaart:

An expression can’t have too many parentheses, can it?

In theory yes (compiler has its limits) in practice no (the limits are high).

to measure Vin quite precisely you might want to mix in this code

//

// https://code.google.com/p/tinkerit/wiki/SecretVoltmeter
//
long 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 = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

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

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

I do not understand this code can you explain it?

Rg = (( R3 * Vin) / (Vout + (( R2 * Vin) / (R1 + R2))) - R3);

It works fine…

res.jpg

res1.jpg

You misinterpret my the intent of my comment. The use of curly or square brackets generally makes things easier for "humans" to read. Machines, being somewhat short on cognitive-power use the standard curved bracket.

As it is or was for a sketch, I figured I ought to stay inside that machine paradigm.
I probably should have posted a pic of my solution.
Anyway.

"I apologize if you are very troublesome,..."

You know me only too well.

i try this code and i I get a negative result,
and i try all code in this thread but it seems all is wrong, because when I change his resistor can result in 1000ohm

You should not measure Vin but Vout.
Vin is a given reference voltage.

I posted your original code with in the comments the proposed code. It fails as it did before.

I will propose the final code which is in line with the other posts:
in the comments the result of the formulas.

float Vin = 5.0;
float Vout = 0;
float R1 = 1000;
float R2 = 1000;
float R3 = 1000;
float Rg = 0;

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

void loop()
{
  Vout = analogRead(A0) * (Vin / 1023.0);  // Vout = 0. (assuming all resistors are 1000

  float B = ( R2 * Vin) / (R1 + R2);  // B = 5000/2000 = 2.5
  Rg =  R3 * Vin / (Vout + B) - R3;  // Rg = 5000 / (0 + 2.5) - 1000 = 1000.

  Serial.print(Rg);
  Serial.println(" Ohm");
  delay(1000);
}