Trying to offset analog input reading without effecting another

Hello.

I'm making a DC voltmeter project that can measure both negative and positive voltages. I realise that the Arduino can only handle positive DC voltages so I've made a simple DC inverter to change the negative to positive voltage and am feeding both signals into A4 & A5 inputs.

My inverter circuit has a constant dc offset of 2.21volts which I am trying to hide from my voltmeter, so that when the input sees 3.21 volts, it actually measures 1.21 volts.

The problem I'm having is that when I try to subtract an amount from input A4, it has the same result on input A5.

I'm sure it can't be so difficult to do, but after trying everything I can think of and researching the forums, I cannot find an answer.

Any help would be greatly appreciated.

Scott.

This is the code I'm using :

// read the value on analog inputs for DC measurement A4 & A5

result = analogRead(dcpos);
result1 = analogRead(dcneg) -451;

{
// Convert result
vout= (result * 5000) / 1024;
vout = (result1) * 5000 / 1024);
vin = vout;
}

{
  // Convert result 
  vout= (result * 5000) / 1024;
  vout = (result1)  * 5000 / 1024);
  vin = vout;
}

This code makes little sense as you are overwriting the results of the first vout calculation (the one using result) with the second calculation that uses result1. It's the same as if the "vout= (result * 5000) / 1024;" statement never existed. You need to rethink your logic.

Lefty

Thanks for the reply.

Maybe I didn't explain enough about what I'm doing.

I am making a DC volt meter with a single readout on a LCD. What I'm effectively trying to do is to ascertain if an input has anything on it, and if it does, display it on the LCD. I only want to read the input which is active.

The only way I could think to do that is to do what I've done with both readings being combined then displayed. The theory behind my thinking works, I just can't work out how to lose the DC offset from one pin without effecting the other.

If you can think of an alternative way to do it I'd be more than happy to learn!

You might want something like this:

  result = analogRead(dcpos);
  result1 = analogRead(dcneg) -451;

  // Convert result 
  if (result1 > result)
  {
    // input is negative
    vin = (result1  * 5000) / 1024;
  }
  else
  {
    // input is positive
    vin = (result * 5000) / 1024;
  }

DC42 - Thanks so much, your solution worked brilliantly!

Be aware that the ADC in an Arduino is multiplexed so to get a more stabile reading it is often advised to do the analogRead() twice and use the last value.

To modify dc42's example:

analogRead(dcpos);
result = analogRead(dcpos);

analogRead(dcneg);
result1 = analogRead(dcneg) -451;

// Convert result
if (result1 > result)
{
// input is negative
vin = (result1 * 5000) / 1024;
}
else
{
// input is positive
vin = (result * 5000) / 1024;
}

Furthemore you should be aware that formulas like vin = (result1 * 5000) / 1024; can have an overflow.

If result1 is an int with a value of 1023, it will overflow when multiplied by 5000; the division by 1024 will not change.

Types long and float will not have this overflow.

skott101:
DC42 - Thanks so much, your solution worked brilliantly!

Thanks, but robtillaart has made 2 very good points:

  • you definitely need to convert the readings to long before doing the multiplication, e.g. "vin = (((long)result1) * 5000L)/1024L;" and similarly for the other one

  • when you are reading from an analog pin with a source resistance greater than 10K, and the last time you did an analogRead was from a different pin, do the reading twice. If the source resistance is greater than 100k, you also need a delay between the readings.

I've taken onboard your comments and suggestions.

I've used an emitter array to make the circuit impedance 4.75K. I've also converted the strings to use the long command as per your example. My project is working really nicely.

I have only had an Arduino for 2 weeks and have never written code before! There is certainly a learning curve but things seem to be logical and with the help of some very nice forum members, most things seem possible! Despite working with analog electronics for 16 years, this is the most excited I've been about electronics for about 15 years!

Thanks again for everyones help.
Scott.