I want to measure a voltage on an AnalogInput. However, I'm not getting an accurate reading. I'm +/- 100mV off.

If I use:
Replacing 5.000 to the output of my voltage regulator it is accurate until temperature changes, or input voltage changes enough to affect output voltage.

Then, I tried using the Bandgap to calculate VCC/AREF.
I couldn't get a reading to measure as described in the below thread, so I took a known input worked it backwards to get the bandgap. Again, I got accurate readings until something changed.

http://forum.arduino.cc/index.php/topic,38119.0.html

When I get to:

1. Measure and record the voltage across the AREF capacitor. In my case the voltage is 1.083.

I only receive 0.003-0.005v with nothing other than 0.1uF cap to ground on AREF.

Is there a certain board layout I should setup to get this measurement?

As I understand it you can expect +/- 1 difference in consecutive readings. There are also a lot of interference affect that will cause the reading to change. Just off the top of my head these include vibration, hand capacitance, that steel wrist strap on my watch the A/C in your home ............. And remember the circuit has to "round" the value to an int, if the turn reading where 512.49999999 then it would not take much to give you a +/- 1 variation. And you can't get rid of this using AREF as no voltage supply is that constant or accurate. Nor is your meter all instruments have a built in error!

Mark

I only receive 0.003-0.005v with nothing other than 0.1uF cap to ground on AREF.

That could be a true voltage reading with the input "floating" and nothing to discharge the capacitor. A resistor to ground will bring the voltage to (or closer to ) zero volts.

Or, I suppose it could be a small offset error in the ADC.

Processor? (The technique does not work for all AVR processors.)

DVDdoug:
That could be a true voltage reading with the input "floating" and nothing to discharge the capacitor.

The point of the exercise is to measure the bandgap voltage present on the AREF pin. When @smileymattj does everything correctly the measured voltage (for the ATmega328 processor) will be >= 1.0 and <= 1.2.

The voltage on the analog input pin (pin zero in the example) is irrelevant. The primary purpose of the code is to configure the ADC to use the internal bandgap with a filter on AREF. The rest of the code is just window dressing. It's purpose is to entertain the human and to help the human ensure everything is working as expected.

I don't understand what you tried so far.
Powering with usb is very inaccurate.

Using a good power supply to get a good 5.0V, that results in moderate accurate analog values.
Using the internal voltage reference of 1.1V (or 2.56) is accurate after tuning the calculation. The 1.1V is never precisely 1.1V.
The 1.1V also depends on the temperature.

If you want to measure the Vcc 5.0V against the internal voltage reference, you need to know the exact value of the 1.1V and it will only be valid at the same temperature. I do that in an Arduino that is always at room temperature and measuring the Vcc 5.0V is accurate to about 15mV (after averaging a few measurements).

You would need an external voltage refererence if you want very accurate volt readings.

Caltoa:
The 1.1V also depends on the temperature.

The variance is ~0.5% over the entire temperature range (-40 to +85). Over the range of the analog-to-digital convert (0-1023) that comes out to ±3 counts. For the vast majority of Arduino applications that difference is irrelevant. For the rest, a dedicated analog-to-digital converter is a much better choice than trying to squeeze the AVR converter for more accuracy.

Thanks for the quick replies.

The processor is Duo ATMega328.
Are those instructions for a different processor?

I can power the processor via:
LM78L05
LM7805 (heatsinked)
Tektronix variable 0-20v DC bench power supply (directly or through regulators)
12v SLA battery through regulators
USB UNO R3 development board
USB2SERIAL board

Right now, it's on a breadboard but it's nothing to switch to something else.

When I say the accuracy was affected by temperature. I mean when the voltage regulator warms up. It's far enough away from the processor on the beard board not to give off any heat to it. And no room temperature changes. The temperature of the linear voltage regulator changed it's voltage output slightly. So I don't think I'm even going into the normally expected fluctuation.

I think it's more something I'm not doing right rather than normal expected limitations/errors of the processor.
The meter I'm using is a Fluke 179.

I'd be happy with +/-15mV accuracy as Caltoa describes.

I think my issue is figuring out how to properly measure the bandgap reference.
I was expecting to get something around 1.000-1.200v. 0.00X isn't close to that.
I believe I tried without the 0.1uF cap also with the same result.

Thanks.

smileymattj:
The processor is Duo ATMega328.
Are those instructions for a different processor?

No. The instructions are always the same. The reason I asked about the processor is exactly what I wrote earlier.

I was working with an ATmega328P which is functionally equivalent to an ATmega328 for this discussion.

I can power the processor via:

Makes no difference. Power it however you want.

Right now, it's on a breadboard but it's nothing to switch to something else.

Breadboard. Or not. Makes no difference.

I think it's more something I'm not doing right rather than normal expected limitations/errors of the processor.

Or the processor has been damaged.

The meter I'm using is a Fluke 179.

Good choice. Will work fine.

I believe I tried without the 0.1uF cap also with the same result.

Capacitor is very likely necessary. Don't leave it off.

The full Processor name is:
ATMEGA328P-PU

I used the BareMinimum example as the blank sketch. For step #1.

Then I copied the code outlined in the post into a new completely blank sketch. From Step #5.

Should I try another processor to rule out mine being bad?

Should I try another processor to rule out mine being bad?

I can think of four possibilities...

1. I made a mistake with those instructions.

2. Your meter needs fresh batteries.

3. You are making a mistake.

5. Did I make a mistake?

smileymattj:
The full Processor name is: ATMEGA328P-PU

Perfect. The same processor my desk Uno has. I followed the instructions (this time using Arduino 1.5.5; they were written using 1.0.x). The end result is 1.089 volts across the capacitor. As a sanity check I uploaded an empty sketch (BareMinimum) and checked the voltage. Zero. (Well, very nearly zero. I get 0.004 volts.) As far as I can tell the instructions are correct for your processor.

1. Do you need fresh batteries? Only you can answer that question.

2. Are you making a mistake? If you are, I can't find it in what you've written.

3. Is the processor toast? Certainly seems to be the most likely culprit. My answer is "yes", it is time to try another processor.

A breadboard is known to give a lot of problems due to bad connections.
We need to know everything that is out of the ordinary, so next time please add some links and photos so we know what you have.
Did you use this ?

That is terribly wrong. It shortcuts the analog reference to ground.

Can you upload a photo, so we can see the wiring ? I bet we can point out at least 5 problems
Did you for example use a decoupling capacitor of 100nF to 5V and GND ?

Can you do the same test with an Arduino Uno ?

1. Check - So we don't have any issues in your instructions?
2. Battery was at 8.1v, I suppose that's still with-in operating specs, but I replaced with fresh new 9.4v - Checked with Fluke 376.
3. You mean written in code, or written by as I described in the forum posts? As far as code, for the testing to get bandgap, I was copy-pasting. Or you mean it sounds like I'm following your instructions correctly?
4. I have been waiting on more processors, Mouser was my source and was on back order. I haven't gotten email verifying they shipped yet. Once I started playing with my Uno, I liked them and ordered more to have on hand when I got a new idea for more projects.

What all should be connected to the AREF and Ground pins during each sketch?
I've had ground connected each time.
During the blank sketch, I had 5V+ connected to AREF. I may have tried disconnected as well.
During the sketch with the code to measure the bandgap, I had ground connected and AREF connected to nothing.
Also the AVCC I kept to 5V+ all the time.

Pins 20-22, Should I have them connected to 5V+ and ground? disconnected? Varies between each step?
Pin 21 to 22 is where I was inserting the 0.1uF cap.

The images were way too large to embed in the post, made the viewing horrible.

Yes, I did use that tutorial. How is it shorting the AREF to ground?
It didn't have a wiring schematic, so I came up with this:

Here is my voltage Regulator:
LM78L05
2x 10uF Caps, one input, one output

10K resistor on reset pin
22pF caps on the 16MHz Crystal
0.1uF cap on power pins (100nF = 0.1uF, this the decoupling cap you were referring to?)
White, Orange, Green, Blue jumpers on left side goto 6 pin header for USB2SERIAL adapter. 0.1uF cap on between reset.
(see below image)

The same test on an Arduino Uno?
You mean put it on the development Uno board rather than the breadboard?

I ment using the same sketch on a Arduino Uno.

The 100nF = 0.1uF = decoupling for the power when placed at 5V and GND.

You also need 100nF decoupling at the 78L05, on the input and output to GND.

You have Aref connected to something. That is not okay. It should be connected only to a 100nF.

5V at both VCC and AVcc.
Both GND to GND
100nF to Aref and GND, but that is only to reduce some noise. As a start, don't connect anything to Aref.

smileymattj:

1. Check - So we don't have any issues in your instructions?

I carefully followed the instructions as written and everything worked as expected.

1. You mean written in code, or written by as I described in the forum posts?

Or you mean it sounds like I'm following your instructions correctly?

Yes.

What all should be connected to the AREF and Ground pins during each sketch?

On a breadboard you should have a two 0.1uF capacitors connecting {A}VCC to {A}GND as close as possible to the processor (there are two VCC pins {labeled VCC and AVCC} and two GND pins {labeled GND and AGND}; they all need to be correctly connected) and a 10K ohm resistor connecting RESET to 5V. If the processor is clocked with a crystal you should have the crystal + two capacitors (or a resonator) correctly connected. Start with AREF unconnected.

The filter capacitor described in my instructions is connected from AREF to GND.

During the blank sketch, I had 5V+ connected to AREF.

Do not do that. For this experiment AREF is either not connected or is connected to GND through a 0.1uF capacitor.

If you get the AREF connection wrong, you will damage the processor. I've tossed two processors in the trash because of that mistake.

During the sketch with the code to measure the bandgap, I had ground connected and AREF connected to nothing.

Do not connect AREF directly to GND. Ever.

At step #3 you connect AREF to GND using a 0.1uF capacitor.

Also the AVCC I kept to 5V+ all the time.

Correct. AVCC and VCC should always be connected to 5V. AGND and GND should always be connect to GND. There should be a 0.1uF capacitor between VCC and GND. There should be a second 0.1uF capacitor between AVCC and AGND.

Pins 20-22, Should I have them connected to 5V+ and ground?

Correct.

Pin 21 to 22 is where I was inserting the 0.1uF cap.

Correct.

smileymattj:
Yes, I did use that tutorial. How is it shorting the AREF to ground?
It didn't have a wiring schematic, so I came up with this:
Imgur: The magic of the Internet

Do not connect pin 21 to 5V. Until you have a firm grasp of the Analog Reference options, never connect pin 21 to 5V. For the majority of Arduino work you do you will never connect pin 21 to 5V.

All the rest of the circuit looks good.

smileymattj:
The same test on an Arduino Uno? You mean put it on the development Uno board rather than the breadboard?

That's what I did but the same steps work fine on a breadboard.

Thanks guys. I now know to leave AREF pin alone.

I got a new batch of processors in and got the Arduino bootloader on them last night.

So I started playing with them this morning. I do believe my previous processor was damaged due to me hooking stuff up to it. Trying to learn from trial and error.
I also think I killed three of them by forgetting to remove the 0.1uF/100nF cap after running the sketch to measure the bandgap. (not fully dead, just seems the ADC is dead) I think having the cap in while running a sketch to read any of the analog pins is probably not good.

I received 1.086v when measuring with my current processor. It was exciting to see a proper result rather than the 0.00x voltage my old processor was giving. This seems to calculate the voltage pretty accurately. I'm getting 10mV-30mV -/+ of accuracy after a voltage divider. So I think that's quite acceptable for my current project. Honestly I think it will be more accurate on a PCB, the breadboard seems to need multiple jumper wires to get good connections on the power distribution.

The bandgap reading seemed to move around about 3mV. I measured it a couple of times to get a more accurate reading.
Would say loading up the blink sketch prior to the blank and letting it run for 2-3 minutes to get the processor warmed up to operating temperature help pull a more accurate reading the first go around? Just run the processor with a simple sketch as a warm up first?

From your suggestions, I see that I might should use more 0.1uF/100nF caps. Here is a schematic of what I'm using.
I got the 47uF and 100uF ratings from Adafruit.
Would this be a good capacitor configuration to start off with as like a base breadboard setup?

smileymattj:
I received 1.086v when measuring with my current processor.

Excellent.

I've never witnessed anything like that. The few dozen I've measured have always been rock solid. (USB power; mid level Extech meter).

How stable is the power supply?

Would say loading up the blink sketch prior to the blank and letting it run for 2-3 minutes to get the processor warmed up to operating temperature help pull a more accurate reading the first go around?

The reading may change a millivolt or two because the processor is at a different temperature but while you are taking a reading it should be very stable.

Just run the processor with a simple sketch as a warm up first?

Try it.

Would this be a good capacitor configuration to start off with as like a base breadboard setup?

Looks good to me.

I think trying a "warm-up" sketch was about the same result as just running the blank for a few minutes.
So it's probably just redundant to do a sketch prior to the blank.

Coming off my bench power supply is very stable. However after going through the LM78L05 it was jumping all over the place.
Added another 10uF capacitor in the power rail, but didn't seem to stabilize it.
The way I stabilized it was to double up the jumper wires. They just don't feel like they are making a "tight" connection to the breadboard like the components are. Plus they are cheap jumpers from eBay China. So highly doubt they are good copper wires inside.

After adding more jumpers to give multiple paths to supply power to the processor, it was much more stable on the output side of the LM78L05. This gave a solid stable reading on the bandgap measurement.

Some of my connections are good Belden 23 awg cat6 wires I stripped out to make breadboard jumpers. (U shaped flat style)
Then some others that either are long lengths or where I was going to move around a lot would be these type:

Looks like these would be much better jumpers: