Yun ADC resolution

I am reading the voltage from a MG811 CO2 sensor, the range is very small for the complete scale of the the C02 levels (rough 0.5v).

The code just reads the analogue input voltage but the resolution is only two decimal point. Reading the A0 input an sending the reading to serial or console provides a x.xx which is not enough for the range of the co2 sensor.

How can I improve the accuracy of the adc to provide more resolution and ideally five decimal points?

void loop() 
{
  float CO2_ADC_Volatge;
  // put your main code here, to run repeatedly:

  CO2_ADC_Volatge = analogRead(A0);
  Serial.println(CO2_ADC_Volatge);
  delay(1000);

}

brendonshaw:
The code just reads the analogue input voltage but the resolution is only two decimal point.

The ADC is 10 bits, which gives 1024 possible values. analogRead() returns an integer in the range of 0 to 1023.

The resolution is the reference voltage divided by 1024. By default the reference voltage is VCC, which on the Yun, due to some polarity protection diodes, is only about 4.5V. That means the default resolution is about 4.4 mV per bit.

Reading the A0 input an sending the reading to serial or console provides a x.xx which is not enough for the range of the co2 sensor.

And what code is doing that? Not the code you posted. That code reads a value from the analog input, which is an integer from 0 to 1023, and converts it to a floating point number. You then print the number, and because you are passing a floating point value to Serial.println(), but not passing in a number of digits to print, it is printing it to two decimal places. So the numbers you are getting out for your 0.5V maximum input would range from about 113.00 to 0.00, with the decimal places always .00. (They may actually vary slightly from 0.00, due to floating point rounding, but they will always be near .00)

You can add an optional second parameter to Serial.print() and Serial.println() when you are printing floating point numbers, and it will print the specified number of digits. But those extra digits will always be zeros, given the code you posted.

How can I improve the accuracy of the adc to provide more resolution and ideally five decimal points?

  • Add an op-amp to amplify the sensor voltage before sending it to the ADC
  • Use the internal 1.1V reference
  • Use an external ADC reference that is slightly greater than the expected maximum sensor voltage

Option 1 will give you the best accuracy and the least noise, if you use a good quality amplifier and careful circuit design.

Option 2 will allow you to use about half of the converter range, giving you about 500 counts (with a 0.5V input that would be about 1 mV resolution.) Noise (random variations) in the values will be a factor since the measured voltage is so low.

The resolution and accuracy of Option 3 will vary depending on the precision and regulation of the reference voltage, and the value of the voltage. It can give you the widest range of values, but noise will be even greater than Option 2 since you are increasing the sensitivity per bit.

But none of this is going to help if you simply convert the output from analogRead() to a floating point value. You will have to convert from raw ADC units (0 - 1023) to your scaled units (voltage, percent CO2, or whatever) using either a rough theoretical scaling factor, or a more precise one derived from experimental calibration.

Plan B:

sonnyyu:
Apple to apple comparison:

Yun:

Analog Input Channels:12, 10 bit ADC channels
Analog Output Pins:0

Tian:

Analog Input Pins:6, 12-bit ADC channels
Analog Output Pins:1, 10-bit DAC

Tian is far better than Yun if you need analog I/O.

12-bit ADC channels v.s. 10 bit ADC channels. 400% better.

http://forum.arduino.cc/index.php?topic=350002.msg2414954#msg2414954

Many thanks for the quick response, I never realised the ADC read was only a integer so makes sense on the conversion. I have to look at the recommendations and understand how I work out if i need any external ADC or implement something else

Hi

ShapeShifter. After your commets I have I doubt.

In the Analog pins in the Yun following the specifications we get:

0 V -->  0 bits
5 V --> 1023 bits

Your are telling that the 5V are not real, instead we are geting 4.5V. So the the relation changes like that:

0 V -->  0 bits
4.5 V --> 1023 bits

or we have to interpolate and the relations changes like that;

0 V -->  0 bits
4.5 V --> 921 bits

That's very important to determine the accuracy when reading from sensors.

Thanks

aabm01:
In the Analog pins in the Yun following the specifications we get:

0 V -->  0 bits
5 V --> 1023 bits

Yes, assuming that you are using Vcc as the reference voltage (the default) and that Vcc is actually 5V.

Your are telling that the 5V are not real, instead we are geting 4.5V. So the the relation changes like that:

0 V -->  0 bits
4.5 V --> 1023 bits

Mostly. The Yun has some polarity protection diodes in the power supply lines. So while 5V may be passed in on the USB line, or the VIN line, by the time the power passes through the diode and reaches the processor, the voltage is approximately 4.5 volts. So your numbers above are mostly right, but they might vary based on variations from board to board. 4.5V is not a regulated value, it's just the input voltage minus the diode voltage drop.

or we have to interpolate and the relations changes like that;

0 V -->  0 bits
4.5 V --> 921 bits

No. A 4.5V input to the ADC will only give you an output reading of 921 if the reference voltage is 5V. That's not the case on the Yun if you use the default configuration.

That's very important to determine the accuracy when reading from sensors.

If you need accuracy, you should be calibrating your conversion values to give yourself the right answer. Do not assume that you will get 4.88 mV/bit with a 5V reference (or 4.39 mV/bit with a 4.5 volt reference.) Those theoretical numbers will get you close, but there will always be variations from board to board that will cause a loss of accuracy. If Vcc is stable, you should be able to calibrate out the difference between 5V and 4.5 V, but remember that the maximum voltage that you can convert is still limited to the actual Vcc voltage used as the reference - any voltages higher than that will be clipped with a value of 1023.

Besides board to board component variations, remember that the Vcc voltage reference is not regulated - the Yun has no on-board 5V regulation: it either gets its power from the USB jack, or the VIN pin. In either case, the power passes through a protection diode (which causes a voltage drop) but is not otherwise regulated. Any variations on input voltage can cause variations in the ADC readings.

Using Vcc as your ADC reference voltage gives you an approximation, at best. For a lot of projects, that's good enough. But if you need better accuracy, or better stability, you need to use a better reference voltage. You can switch to an internal reference voltage, and that will give you an increase in accuracy, although it will reduce your measurable range of voltages - the upper limit that you can convert will be the same as the reference voltage. So if you select the internal 1.1 volt reference, you will get 1.074 mV/bit resolution, but the largest voltage you can convert is 1.1 volts.

The internal reference voltages are more accurate than using the Vcc as the reference. If you need more accuracy than that, you can use an external precision voltage regulator, and use that as your external reference. They make high precision reference voltage regulators for just this purpose.

I supply 5V with a transformer to the YUN via the Vin Pin. So I understand that with the lost through the diode I will get more or less 4.5V
How can I know which is the real maximum voltage in the analog pins so I can do an accurate calibration?.

There are some way to force any pin to give me always the maximum votage so I can calibrate every X time the YUN programatically?

I am using pressure sensors with this operational range:

0.5 V --> -2000 Pa
2.5 V --> 0 Pa
4.5 V --> 2000 Pa

So If I take as a reference voltage i.e 4.5V I will get

0.5 V --> -2000 Pa --> 114 bits
2.5 V --> 0 Pa --> 568 bits
4.5 V --> 2000 Pa --> 1023 bits

Am i right?

On the other hand with this sensors voltage range I couldn't use the 1.1V Internal reference voltage If I understood.

Thanks

aabm01:
How can I know which is the real maximum voltage in the analog pins so I can do an accurate calibration?.

The most accurate way is to use a precision voltage regulator that feeds only the AREF pin, and then use the external reference mode.

The issue with using the Vcc reference is that it can vary, especially due to switching transients and other loads that are on the power bus. These transients will cause noise in your converted values.

There are some way to force any pin to give me always the maximum votage so I can calibrate every X time the YUN programatically?

I think you might be misunderstanding what I mean by calibration. It does not involve taking the reference voltage and calculating bits per volt, and then scaling that to the pressure reading. Those are all theoretical values. There will always be some part-to-part variations in the reference voltage, the analog converter, the sensor, and any conditioning circuitry. By calculating theoretical numbers, you will get close.

But to really calibrate it, you need to take a series of measurements at a wide range of known pressures. For each known pressure, record that physical value and the corresponding raw analog reading. When you have a bunch of such samples, calculate a linear estimation fit of the points to come up with a slope and offset values (y = ax + b) where "a" is your slope, and "b" is the offset. Once you have those calibration values, you can convert any raw analog reading "x" into a scaled physical value "y".

Using a method like this, you fine tune the theoretical scaling factors, and end up with a more accurate converted value. This will calibrate out any variations on the analog reference voltage, and any part-to-part variations. In reality, the actual reference voltage is not so important (as long as it covers the required range) as it will be taken care of during calibration.

If you are making multiple units, each one will need to be individually calibrated.

So If I take as a reference voltage i.e 4.5V I will get

0.5 V --> -2000 Pa --> 114 bits
2.5 V --> 0 Pa --> 568 bits
4.5 V --> 2000 Pa --> 1023 bits

Am i right?

Theoretically, yes. But you won't necessarily get exactly these values due to the variations inherent in every part of the chain. That's why you calibrate, to get more exact values.

On the other hand with this sensors voltage range I couldn't use the 1.1V Internal reference voltage If I understood.

Correct, the 1.1 volt reference will not be useful in your application, as it will severely limit your range of measured pressures. You could divide the sensor voltage to get it down to the 1.1V range, but that could increase the amount of noise in your samples.

Thank you very much!!!

You explained very well. It has been very useful.

So the best way is calibrate if I have the way to take measurements a differents pressures (In this case the reference voltage doesn't matter because the equation give us the real pressure through the equation).
In this case I need to determine a equation for every sensor (calibrate).

If I have no way to take the measurements to determine a equation. I have to apply an external regulated voltage through the AREF Pin to get more accuracy and then scale to get the pressure.

However it's supposed that the sensors are calibrated, so having an external regulated voltage to ensure accuracy and making an auto zero (determine the bits corresponding to atmospheric pressure) to substract in case of variations would be enough.

Are you agree?

And a last question.
In the arduino YUN there are 3 GROUND pins, 2 in the power connector aside the 5V and Vin, and one aside the AREF pin.
Are the 3 ground pins the same?
When I add an external voltage regulator I have to connect it to the AREF pin but, Can I connect the GRD to any of the 3 grounds pins in the board or it has to be the one aside of the AREF?
Are the 3 ground pins in the board connected internally (so they are the same) ?

I am asking that because I designed a PCB to place the sensors, that PCB is connected to the YUN and I made a ground plane on it, but I only connected 1 of the 3 the grounds to this ground plane thinking that the 3 grounds of the board were connected internally.
And now I don't know If I made a mistake.

I am powering my YUN with a transformer (MEAN WELL RS-155). Can I apply a precision voltage regulator in the AREF taking advantatge of this trafo. Do you know somethig that I could apply?

Thanks again!!

aabm01:
If I have no way to take the measurements to determine a equation. I have to apply an external regulated voltage through the AREF Pin to get more accuracy and then scale to get the pressure.

Of course, what hasn't been mentioned is how much accuracy do you actually need? There's no real point in going with high precision circuitry if you don't need it.

However it's supposed that the sensors are calibrated, so having an external regulated voltage to ensure accuracy and making an auto zero (determine the bits corresponding to atmospheric pressure) to substract in case of variations would be enough.

The sensor may be calibrated, but that is only one part of the equation. Is the sensor ratiometric? If so, it will be sensitive to variations in the sensor voltage supply. What other conditioning circuitry and connections do you have in the system? Are there going to be parts tolerances and signal losses involved?

Doing what you propose sounds like a reasonable compromise on accuracy. You will use a theoretical scale factor, but the offset value will be computed based on your auto zero algorithm. So you will likely get quite accurate low pressure values, bit may be off some at the higher pressures. And that makes sense: at 1Pa, a variation of +/- 0.1 Pa is often much more significant than a +/- 0.1 Pa variation at 2000 Pa.

Are the 3 ground pins the same?

Yes. They are all connected together internally.

I am powering my YUN with a transformer (MEAN WELL RS-155). Can I apply a precision voltage regulator in the AREF taking advantatge of this trafo.

The output of that power supply is 5volts. A regulator needs some headroom to operate - if you want 5 volts out, you need a little more voltage in.

Your best bet might be to put the 5V output of the power supply to the AREF input: be sure to add some filtering and bypass capacitors to make the input to that pin as clean as possible. You need to do some studying of the ATmega32U4 data sheet before hooking up to the AREF pin. There needs to be a series resistor in place, because the chip will start up with the internal reference turned on and feeding power OUT the AREF pin. Until your code gets to the point where the internal reference is turned off and the external reference is selected, the two supplies will fight each other, which could cause a damaging current flow. The external resistor will limit the current. I think the datasheet calls for a 5k resistor feeding AREF. The gotcha is that there is an internal impedance (32k, I think) that will act as a voltage divider in concert with your external resistor: 5V in to the 5k resistor will result in something like 4.3V on AREF, which will limit your upper measurement range.

Of course, what hasn't been mentioned is how much accuracy do you actually need? There's no real point in going with high precision circuitry if you don't need it.

The sensors are MPXV7002GP and they are ratiometric. They has an accuracy (1mV = 1Pa)
And I want to measure Pressure loss in filters (I check the difference between the pressure before and after the filter), however variations of 1Pa are important.

What other conditioning circuitry and connections do you have in the system? Are there going to be parts tolerances and signal losses involved?

there are 10 mini PCBs with the sensors (with noise filter following the datasheet) connected via connectors to a main PCB. The arduino YUN is connected to the main PCB.

Apply a precision voltage regulator in the AREF taking advantatge of this trafo. Do you know somethig that I could apply?
The YUN and the sensors are powered y the trafo (MEAN WELL RS-155). The design is simple.

Apply a precision voltage regulator in the AREF taking advantatge of this trafo doesn't sound trivial (I am not electronic). So I think, I will take the voltage in the 5V pin as a VCC reference to scale.

I suppose That the voltage in the 5V pin, will be the maximum voltage in the analogic pins. If it for example is 4.7V. is it stable on time or can vary?
If it can vary, there are some way to take its value every X seconds programatically?

Thanks