AN2572 ADC Oversampling
AN2572 ADC Oversampling
Introduction
® ®
The Microchip tinyAVR 0- and 1-series, and megaAVR 0-series controller offers an Analog-to-Digital
Converter with 10-bit resolution. In most cases, 10-bit resolution is sufficient, but in some cases, higher
accuracy is desired. Special signal processing techniques can be used to improve the resolution of the
measurement. By using a method called ‘Oversampling and Decimation’, higher resolution might be
achieved without using an external ADC. For example, by using 10-bit ADC, a 12-bit result could be
achieved with oversampling technique. This application note explains the method and conditions needed
to be fulfilled to make this method work properly. This application note also provides source code
according to the explained theory to achieve this oversampling technique.
Features
® ®
• Increasing the ADC resolution for the Microchip tinyAVR 0- and 1-series, and megaAVR 0-series
devices by oversampling
• Averaging and decimation
• Software has been implemented as an Atmel START example project for the ATtiny817 to achieve
12-bit resolution from 10-bit resolution
• Shows configuration option in the source code to select:
– ADC input pin
– ADC sampling rate
• ADC results are sent through USART to the serial terminal:
– Measured analog input voltage (in volts) is displayed
– For comparison, both oversampled and normal results are displayed.
Table of Contents
Introduction......................................................................................................................1
Features.......................................................................................................................... 1
1. Relevant Devices.......................................................................................................3
1.1. tinyAVR 0-series...........................................................................................................................3
1.2. tinyAVR 1-series...........................................................................................................................3
®
1.3. megaAVR 0-series......................................................................................................................4
2. Theory of Operation...................................................................................................5
2.1. Sampling Frequency.................................................................................................................... 5
2.2. Oversampling and Decimation..................................................................................................... 5
2.3. Noise............................................................................................................................................ 5
2.4. Averaging..................................................................................................................................... 8
2.5. When Will ‘Oversampling and Decimation’ Work?....................................................................... 9
5. Macro Configurations.............................................................................................. 13
8. Revision History.......................................................................................................16
Customer Support......................................................................................................... 17
Legal Notice...................................................................................................................18
Trademarks................................................................................................................... 18
1. Relevant Devices
This chapter lists the relevant devices for this document.
2 KB ATtiny202 ATtiny204
Pins
8 14 20 24
Devices with different Flash memory size typically also have different SRAM and EEPROM.
2 KB ATtiny212 ATtiny214
Pins
8 14 20 24
Devices with different Flash memory size typically also have different SRAM and EEPROM.
48 KB ATmega4808 ATmega4809
32 KB ATmega3208 ATmega3209
16 KB ATmega1608 ATmega1609
8 KB ATmega808 ATmega809
Pins
28/32 48
Devices with different Flash memory size typically also have different SRAM and EEPROM.
2. Theory of Operation
This chapter explains how oversampling works with all the necessary mathematical details.
2.3 Noise
To make this method work properly, the signal component of interest may not vary greatly during a
conversion. However, another criterion for a successful enhancement of the resolution is that the input
signal has to vary slightly when sampled. This may look like a contradiction, but in this case, variation
means just a few Least Significant Bytes (LSB). The variation may be seen as the noise component of
the signal. When oversampling a signal, there may be noise present to satisfy this demand of small
variations in the signal. The quantization error of the ADC is at least 0.5 LSB. Therefore, the noise
amplitude has to exceed 0.5 LSB to toggle the LSB. Noise amplitude of 1-2 LSB is even better because
this will ensure that several samples do not end up getting the same value.
Another reason to use this method is to increase the signal-to-noise ratio. Enhancing the Effective
Number of Bits, ENOB, will spread the noise over an increased binary number. The noise influence on
each binary digit will decrease. Doubling the sampling frequency will lower the in-band noise by 3 dB, and
increase the resolution of the measurement by 0.5 bits.
2.4 Averaging
The conventional meaning of averaging is adding m samples, and dividing the result by m, which is
referred to as normal averaging. Averaging data from an ADC measurement is equivalent to a low-pass
filter and has the advantage of attenuating signal fluctuation or noise, and it will flatten out peaks in the
input signal. The moving average method is very often used to do this. It works by taking m readings,
place them in a cyclic queue and average the most recent m. This will give a slight time delay because
each sample is a representation of the last m samples. This can be done with or without overlapping
windows. The figure below shows seven (Av1-Av7) independently moving average results without
overlapping.
Figure 2-2. Moving Average Principle
It is important to remember that normal averaging does not increase the resolution of the conversion.
Decimation, or interpolation, is the averaging method, which combined with oversampling increases the
resolution.
The extra samples, m, achieved by oversampling the signal are summed up, just as in normal averaging,
but the result is not divided by m as in normal averaging. Instead, the result is right shifted by n, where n
is the desired extra bit of resolution, to scale the answer correctly. Right shifting a binary number once is
equal to dividing the binary number by a factor of 2.
As seen from Equation 2-2, increasing the resolution from 10 to 12 bits (that is, additional 2-bit
resolution), requires the summation of 4^2 (16) 10-bit values. A sum of 16 10-bit values generates a 14-
bit result where the last two bits are not expected to hold valuable information.
To get ‘back’ to 12-bit representation, it is necessary to scale the result. The scale factor, sf, given by the
equation below, is the factor, which the sum of 4n samples should be divided by, to scale the result
properly. n is the desired number of extra bit.
Equation 2-3. Scale Factor
�� = 2�
As explained in the case above (increasing resolution from 10-bit to 12-bit), the scaling factor, sf, is 2^2,
which is equal to 4.
The stippled line illustrates the averaged value of the saw-tooth signal. The sampling shown in figure (a)
above will cause a negative offset, while the sampling in (b) will cause a positive offset. In figure (c), the
®
sampling is sufficient, and offset is avoided. To create an artificial noise signal, one of the AVR counters
can be used. Since the counter and the ADC are using the same clock source, this gives the possibility of
synchronizing the noise and the sampling frequencies to avoid offset.
5. Macro Configurations
Below are the macro configurations in the main.c file.
• Maximum input voltage
#define MAX_VOL 2.5
Maximum input voltage is configured to 2.5 to calculate the voltage for measured ADC reading.
Note: ADC reference has been configured to 2.5V.
• ADC input channel
#define ADC_CHANNEL 5
The ADC input signal has been connected to channel 5: AIN5 (pin PA5).
8. Revision History
Doc. Rev. Date Comments
C 10/2018 Updated figures 1-1, 1-2, 1-3 in chapter "Relevant Devices". Fixed grammar and
punctuation.
B 02/2018 Added support for tinyAVR 0-series and megaAVR 0-series.
A 11/2017 Initial document release.
Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Field Application Engineer (FAE)
• Technical Support
Customers should contact their distributor, representative or Field Application Engineer (FAE) for support.
Local sales offices are also available to help customers. A listing of sales offices and locations is included
in the back of this document.
Technical support is available through the web site at: http://www.microchip.com/support
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their
code. Code protection does not mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the
code protection features of our products. Attempts to break Microchip’s code protection feature may be a
violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software
or other copyrighted work, you may have a right to sue for relief under that Act.
Legal Notice
Information contained in this publication regarding device applications and the like is provided only for
your convenience and may be superseded by updates. It is your responsibility to ensure that your
application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY
OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS
CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life
support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend,
indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting
from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual
property rights unless otherwise stated.
Trademarks
The Microchip name and logo, the Microchip logo, AnyRate, AVR, AVR logo, AVR Freaks, BitCloud,
chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq,
Kleer, LANCheck, LINK MD, maXStylus, maXTouch, MediaLB, megaAVR, MOST, MOST logo, MPLAB,
OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, Prochip Designer, QTouch, SAM-BA, SpyNIC, SST,
SST Logo, SuperFlash, tinyAVR, UNI/O, and XMEGA are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
ClockWorks, The Embedded Control Solutions Company, EtherSynch, Hyper Speed Control, HyperLight
Load, IntelliMOS, mTouch, Precision Edge, and Quiet-Wire are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom,
CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM,
dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming,
ICSP, INICnet, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, memBrain, Mindi, MiWi,
motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient
Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE,
Ripple Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total
Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA are
trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of
Microchip Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-3674-4
ISO/TS 16949
Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer
fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California
® ®
and India. The Company’s quality system processes and procedures are for its PIC MCUs and dsPIC
®
DSCs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design and manufacture of development
systems is ISO 9001:2000 certified.