Hello!
Recently I wrote a program that measures voltage from external source via analog input. It works great on Arduino UNO, but I need to have this program on something smaller & cheaper, so I uploaded my program to ATmega8 microcontroller and there is a problem. It shows different voltages than the same program on Arduino board (readings from Arduino are correct. I checked them with multimeter).
If you used the internal reference voltage, they differ.
The 5V voltage has a great influence. You should measure them for the Arduino (the 5V output pin) and the ATmega8.
Do you use a resistor divider ? Those resistors can be 5% tolerance.
There are also differences between ATmega chips.
For voltage measurement I use the internal reference. The actual reference voltage differs even more between ATmega chips.
In most cases I use a good quality multimeter to check the voltage, and I adapt the calculation in the sketch with a few percentages.
I am planning to use a resistor divider lately, when it will be finished. Now I am plugging in 3 x 1.2 V rechargeable batteries for testing. When Arduino measures about 2.40 V (which is correct result) from them, Atmega8 shows that it is about 2.70 V. I am not using AREF. It was just a single try from tutorial found on google.
That is more difference than expected.
0.3V / 5.0V = 6% too high
I don't think that something is damaged. It could be a combination of one of these:
I have seen differences between ATmega chips. For accurate measurements I calibrate my sketch for every ATmega.
Since the ATmega8 measures a higher voltage, it is likely that it runs at a lower 5V, for example 4.8V instead of 5.0V
The ATmega8 could have bad decoupling, for example a missing 100nF at 5V and GND.
If the ATmega8 does not have a 100nF at Aref, perhaps there is more noise than normal.
Perhaps using the average of 5 to 20 samples will lower the difference.
After setting the internal mux, some delay should be added before reading the analog value. Perhaps that delay is longer for the old ATmega8 chip. You can add a delay, or throw away the first analogRead() value.
Could you upload the test sketch ?
And a photo or you ATmega 8 circuit ?
That link in your first post shows how to read its own Vcc against the internal reference. After doing that the ATmega8, ATmega328P and ATmega2560 react all different to the next analogRead().
I can try to program 2 other Atmega8 microcontrollers and compare results, but I don't think that it would help.
BTW. for 5V and 0V results are correct.
You say that the 5V is correct. I would like to know if the voltage 4.98 or 5.02V.
The ATmega8 need ground at GND pin 8 and GND pin 22, and 5V at VCC pin 7 and AVCC pin 20.
I think you missed two of those.
There must be a decoupling capacitor of 100nF next to the ATmega8 to 5V and GND. That is not optional, it must be there. Any measurement will be useless without that.
Optional you could add a 100nF capacitor to AREF and GND. That might improve the analog measurements a little.
Thank you so much for your help. I will try it when I come back home.
Can you also tell me how to correctly connect AVcc? In official datasheet I read that I have to use low-pass filter and on some forums that I found on google they suggest to use 10 uH choke. Now I don't know what to do ;p
The first step for a more accurate analog value is the capactor at AREF.
The second step is the low-pass filter with the 10uH.
For a simple circuit that is battery powered, that low-pass filter is only a small improvement. I get more improvement with the average of a few samples.
In my opinion, when an analog value is needed with a high sample rate, and every ADC value must be as accurate as possible, that low-pass filter is needed to get at least 9-bits accuracy for the ADC. With slow analog signals, I always use some averaging, which improves the accuracy a lot. I start many times with the average of 5 samples, but sometimes I use the average of 1000 or 10.000 samples to reject mains 50Hz/60Hz noise.
Ok, thank you for your reply. I will try it when i buy required parts (I don't have some of them at this time, maybe I will find some on old circuit boards), and then I will give you an answer if it works