Summary of Programming AVR ADC module with WinAVR using Atmega8 microcontroller
Most AVR microcontrollers include an on-chip ADC simplifying hardware and software design. Using the Atmega8 as an example, the ADC offers up to eight inputs (mostly 10-bit), selectable references (AREF, AVCC, internal 2.56V), separate AVCC/AGND pins, ±2 LSB accuracy, ±0.5 LSB INL, and up to 15 kSPS. It supports single and free-running modes and an ADC noise reduction sleep mode. The article shows initializing ADC (ADMUX/ADCSRA), starting conversions with channel multiplexing, enabling interrupts, and reading results (displaying on LCD).
Parts used in theAtmega8 ADC example:
- Atmega8 microcontroller
- Photoresistor (LDR) connected to ADC0
- Potentiometer connected to ADC2
- LCD display for output
- Proteus simulator (optional for simulation)
- AVCC and AGND power supply connections
- AREF with external capacitor
The ATmega8 has a 10-bit analog-to-digital converter (ADC) that turns a voltage on a Port C pin into a number from 0 to 1023. This page explains the settings that matter, with the datasheet page for each one, and gives a short C program that reads one input and prints the number and the voltage on the serial port. It is written for the ATmega8; other ATmega chips use similar registers but with different bits.
Datasheet verified by us. The channels, reference options, register bits, prescaler table, conversion timing and supply rules were read from the ATmega8 datasheet (2486AA), with the page number quoted next to each figure. The program compiled without warnings for the ATmega8 with avr-gcc 7.3.0 (-Os -Wall -Wextra).
Simulated in Proteus 9.0 SP2, run by us. We built the circuit and ran the program in Proteus 9.0 SP2 Professional (Build 40482); the screenshots and files are below. It has not been tested on real hardware. Figures marked “calculated” are our arithmetic from the datasheet formulas. The datasheet is linked at the end of the page.
- Resolution is 10 bits. The result is ADC = 1024 × Vin / Vref (ATmega8 datasheet p. 199).
- Six input channels (ADC0 to ADC5) on the 28-pin DIP; ADC6 and ADC7 exist only on the TQFP and QFN packages (ATmega8 datasheet p. 189).
- The ADC clock should be 50 kHz to 200 kHz for full resolution (ATmega8 datasheet p. 191), so choose the prescaler to suit your CPU clock.
- AVCC must stay within 0.3 V of VCC (ATmega8 datasheet p. 189).
Our Proteus simulation: screenshots and files
We ran the program on the ATmega8 model at its factory 1 MHz internal clock with a 5 V supply, a 1 kΩ potentiometer on PC2 (ADC2) and a virtual terminal at 4800 baud, then moved the potentiometer.

Each voltage equals the reading × 5000 / 1024, rounded down, as the program calculates it.
Proteus project, compiled program, source code, screenshots and a README. Open the .pdsprj in Proteus 9.0 SP2 or newer, set Program File to the .hex from the zip and press Play.
What this run does not show: it is a simulation with an ideal 5 V supply, not a test on a real chip. The AREF capacitor and the AVCC filter described below were not simulated, and noise and accuracy cannot be judged from it.
Which pins can be read
The converter reads single-ended voltages (measured against ground) from Port C. On the 28-pin DIP the inputs are ADC0 to ADC5 on pins 23 to 28 (PC0 to PC5); the TQFP and QFN packages add ADC6 and ADC7 (ATmega8 datasheet p. 189). Two more selections are internal: 1.30 V from the bandgap reference and 0 V (ATmega8 datasheet p. 200). ADC4 and ADC5 are also the SDA and SCL pins of the two-wire interface. The datasheet gives one resolution, 10 bits, for single-ended conversion (ATmega8 datasheet p. 241) and does not describe any channel as 8-bit.
| MUX3..0 | Input |
|---|---|
| 0000 to 0101 | ADC0 to ADC5 (PC0 to PC5) |
| 0110, 0111 | ADC6, ADC7 (TQFP and QFN only) |
| 1110 | 1.30 V bandgap reference |
| 1111 | 0 V (GND) |
The channel is chosen with the MUX bits of ADMUX (ATmega8 datasheet p. 199).
Choosing the reference voltage
The reference voltage Vref sets the top of the range: 0 V reads as 0 and Vref minus one step reads as 1023. The REFS1 and REFS0 bits of ADMUX choose it (ATmega8 datasheet p. 199):
| REFS1 | REFS0 | Reference |
|---|---|---|
| 0 | 0 | The AREF pin, internal reference off |
| 0 | 1 | AVCC, with a capacitor at the AREF pin |
| 1 | 0 | Reserved |
| 1 | 1 | Internal 2.56 V, with a capacitor at the AREF pin |
If a fixed voltage is wired to the AREF pin you must not select the other options, because they would be shorted to it. When no outside voltage is applied you can switch between AVCC and 2.56 V, but the first result after a switch may be inaccurate and should be discarded (ATmega8 datasheet p. 194). An input that goes above Vref reads close to 1023 (ATmega8 datasheet p. 194). Never feed a pin more than the supply allows.
ADC clock and conversion time
The ADC has its own prescaler, set by the ADPS2:0 bits of ADCSRA, that divides the CPU clock by 2, 4, 8, 16, 32, 64 or 128 (ATmega8 datasheet p. 201). For full 10-bit accuracy the ADC clock should be between 50 kHz and 200 kHz (ATmega8 datasheet p. 191). A normal conversion takes 13 ADC clock cycles, and the first conversion after the ADC is switched on takes 25 (ATmega8 datasheet p. 191).
| CPU clock | Prescaler | ADC clock | One conversion |
|---|---|---|---|
| 1 MHz | 8 | 125 kHz | 104 µs |
| 4 MHz | 32 | 125 kHz | 104 µs |
| 8 MHz | 64 | 125 kHz | 104 µs |
| 8 MHz | 128 | 62.5 kHz | 208 µs |
All values in this table are calculated from the figures above. The datasheet lists a conversion time of 13 µs to 260 µs and up to 15 kilosamples per second at full resolution (ATmega8 datasheet p. 189).
Supply and wiring
- The ADC has its own supply pin, AVCC (pin 20). It must not differ from VCC by more than 0.3 V (ATmega8 datasheet p. 189). The datasheet suggests connecting it to VCC through a 10 µH inductor with a 100 nF capacitor to the analog ground (ATmega8 datasheet p. 196).
- With the AVCC or 2.56 V reference the datasheet asks for an external capacitor at the AREF pin (pin 21) (ATmega8 datasheet p. 199). A 100 nF ceramic capacitor to ground is the usual choice.
- The ADC works best with a source impedance of about 10 kΩ or less. A higher-impedance source, such as a large potentiometer, can read low or slowly (ATmega8 datasheet p. 195).
- Keep analog wires short, away from fast digital signals, and avoid switching other Port C pins used as outputs during a conversion (ATmega8 datasheet p. 196).
The three registers
| Register | What it does |
|---|---|
| ADMUX | REFS1:0 choose the reference, ADLAR left-adjusts the result, MUX3:0 choose the channel (ATmega8 datasheet p. 199). |
| ADCSRA | ADEN turns the ADC on, ADSC starts a conversion and reads 1 until it finishes, ADFR selects free running, ADIF is the done flag, ADIE enables the interrupt, ADPS2:0 set the prescaler (ATmega8 datasheet p. 200). |
| ADCL, ADCH | The 10-bit result. Read ADCL first, then ADCH, so both halves belong to the same conversion. With ADLAR set and 8-bit precision enough, ADCH alone is the answer (ATmega8 datasheet p. 201). |
Read one input and print it
The program below turns the ADC on with the AVCC reference and a prescaler of 8 (125 kHz at the factory 1 MHz clock), reads ADC2 (PC2, pin 25) every half second, and sends the number and the voltage to the serial port at 4800 baud. Set VREF_MV to your real supply voltage in millivolts. The first conversion is read and thrown away because it is the longer start-up one.
// atmega32-avr.com
// Read one analog input on an ATmega8 and print it over the serial port.
// ADC2 (PC2, pin 25) is read with AVCC as the reference, so a full-scale reading means "VCC".
#define F_CPU 1000000UL // factory clock of a new ATmega8: 1 MHz internal RC
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define VREF_MV 5000UL // set to your real VCC in millivolts (the reference is AVCC)
static void uart_init(void)
{
UBRRH = 0;
UBRRL = 12; // 4800 baud at 1 MHz, U2X = 0 (0.2 % error)
UCSRB = (1 << TXEN); // transmitter on
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); // 8 data bits, no parity, 1 stop bit
}
static void uart_putc(char c)
{
while (!(UCSRA & (1 << UDRE))) { }
UDR = c;
}
static void uart_puts(const char *s)
{
while (*s) uart_putc(*s++);
}
static void uart_putu(uint16_t v) // print an unsigned number without printf
{
char buf[6];
uint8_t i = 0;
do { buf[i++] = '0' + v % 10; v /= 10; } while (v);
while (i) uart_putc(buf[--i]);
}
static void adc_init(void)
{
ADMUX = (1 << REFS0); // REFS1:0 = 01, AVCC reference
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // ADC on, prescaler 8: 1 MHz / 8 = 125 kHz
}
static uint16_t adc_read(uint8_t channel)
{
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F); // choose ADC0 to ADC7
ADCSRA |= (1 << ADSC); // start one conversion
while (ADCSRA & (1 << ADSC)) { } // ADSC clears when it is done
return ADC; // ADCL then ADCH, 0 to 1023
}
int main(void)
{
uart_init();
adc_init();
adc_read(2); // first conversion is longer: discard it
for (;;) {
uint16_t raw = adc_read(2);
uint32_t mv = (uint32_t)raw * VREF_MV / 1024; // datasheet equation: ADC = 1024 * Vin / Vref
uart_puts("ADC2 = ");
uart_putu(raw);
uart_puts(" ");
uart_putu((uint16_t)mv);
uart_puts(" mV\r\n");
_delay_ms(500);
}
}
It compiled with avr-gcc -Os -Wall -Wextra -mmcu=atmega8 without warnings and takes 408 bytes of flash. In our Proteus run it printed lines such as ADC2 = 512 2500 mV. On the ATmega8 the UART bits are named UBRRH, UCSRC and so on, so other ATmega chips need the register names from their own datasheets.
If the readings look wrong
| Symptom | Things to check |
|---|---|
| Always 1023 | The input is above Vref, or the pin is floating, or AREF is wired to a lower voltage than the input. |
| Always 0 | The wrong channel is selected, or the input is not connected to ground and the signal. |
| Noisy last digits | Normal to a degree (±2 LSB). Add the AVCC filter and the AREF capacitor, and average a few readings. |
| First reading after a reference change is off | Discard it (ATmega8 datasheet p. 194). |
| Reading drifts with a high-impedance sensor | Buffer the signal or reduce the source resistance to about 10 kΩ or less (ATmega8 datasheet p. 195). |
Quick answers
More from this site
Project variants: ADC on the AVR
- ADC example: ATmega8 digital voltmeter and ammeter: a measuring project built on the ADC
- Simplest digital voltmeter with an AVR: a minimal voltmeter
- Light intensity measurement using an LDR and an AVR: a light sensor on an ADC input
Related projects: serial output and where to start
- AVR serial communication (UART) programming tutorial: the serial port used for the output above
- AVR microcontroller tutorials for beginners: where to start with AVR
Datasheets and downloads
- 💾 Schematic and simulation files
Zip, about 84 KB: Proteus project, .hex, source code, screenshots and README. - 📄 ATmega8 / ATmega8L datasheet
PDF from the maker, Microchip. Opens in a new tab at the ADC chapter (page 189). Electrical characteristics on page 241.
Quick Solutions to Questions related toAtmega8 ADC example:
- What ADC resolution does Atmega8 provide?
Most ADC inputs provide 10-bit resolution; ADC4 and ADC5 are 8-bit. - How accurate is the AVR ADC?
Typical accuracy is ±2 LSB, which at 5V AREF may be up to ±0.04V; integral nonlinearity is ±0.5 LSB. - Can the ADC sample audio signals at 20 kHz?
No; maximum conversion speed is up to 15 kSPS at maximum resolution, which is insufficient for 20 kHz audio sampling. - What reference sources can be used for the ADC?
References selectable in ADMUX are AREF pin, AVCC with external capacitor, or internal 2.56V reference. - How do you select ADC input channels?
ADC channels are selected by setting the four channel bits in the ADMUX register. - What modes does the ADC support?
It supports single conversion mode (25 ADC clock cycles per conversion) and free running mode (13 ADC cycles per conversion). - How does ADC noise reduction work?
Noise reduction puts the MCU to sleep (stopping CPU clock) during conversion and wakes it with an interrupt when conversion completes. - How is the ADC initialized in the example?
ADC is initialized by selecting AVCC as reference in ADMUX, enabling ADC and ADC interrupt in ADCSRA, performing a dummy conversion, and setting sleep mode to SLEEP_MODE_ADC. - How does the example start a conversion on a given channel?
It stores the channel, sets ADMUX lower bits to the channel, enables global interrupts (sei), and sets ADSC and ADIE bits in ADCSRA to start conversion with interrupt. - Where are conversion results displayed in the example?
The interrupt service routine reads the ADC value and displays it on an LCD.



