Summary of How to Use ADC (Analog to Digital Converter) in AVR – Atmega32
This article explains how to use the Atmega32 microcontroller's built-in ADC to read analog signals from an LM35 temperature sensor, enabling a fire alarm system that triggers at 60°C. It details configuring the reference voltage via the ADMUX register and outlines essential hardware connections for stability, such as linking VCC to AVCC and adding capacitors to filter noise.
Parts used in the Fire Alarm Project:
- LM35 Temperature Sensor
- Atmega32 Microcontroller
- Buzzer
- Coil (Inductor)
- Capacitor
- VCC Power Supply
- GND Ground Connection
Sensors such as the LM35 temperature sensor give an analog voltage, and the ADC in the ATmega32 turns it into a number. This page covers the reference voltage options, the hardware connections that give clean readings, and the integer maths for converting the reading to degrees, then ends with a small fire alarm that sounds a buzzer above 60 degrees Celsius.
- The LM35 gives 10 mV per degree Celsius, so 60 C is 0.600 V.
- With the 5 V (AVCC) reference, one ADC count is 4.88 mV or 0.49 C. With the internal 2.56 V reference it is 2.5 mV or 0.25 C.
- Temperature in tenths of a degree = reading x 5000 / 1024, and a reading of 124 is the first one above 60 C.
- The program compiles for the ATmega32 to 240 bytes and the maths is checked by a short Python test; we did not run it on hardware.

Reference voltage
The ADC measures the input as a fraction of a reference voltage. The ATmega32 gives three choices, set in ADMUX: the AVCC pin (usually your 5 V supply), an internal 2.56 V reference, or a voltage you supply to AREF. A smaller reference gives finer steps if the signal stays small, which is the case for a sensor that stays below 100 C.
| Reference | Millivolts per count | Degrees per count (LM35) | Full scale |
|---|---|---|---|
| AVCC, 5 V | 4.88 | 0.49 C | 5 V = 500 C, beyond the sensor range |
| Internal, 2.56 V | 2.50 | 0.25 C | 2.56 V = 256 C |
The program on this page uses AVCC. To use the internal reference, set REFS1 and REFS0 together in ADMUX and change the factor in the conversion from 5000 to 2560 (the arithmetic is then reading x 2560 / 1024 tenths of a degree).
Hardware connections
| Pins | Connection |
|---|---|
| VCC (10) and AVCC (30) | Connect them; the ADC takes its supply from AVCC. A small inductor between them and a capacitor from AVCC to ground reduce noise. |
| AREF (32) | A capacitor to ground steadies the reference. |
| PA0 (ADC0) | LM35 output |
| PB0 | Buzzer through a transistor |
The arithmetic
| Step | Value |
|---|---|
| Alarm voltage | 60 C x 10 mV = 0.600 V |
| Reading at the limit | 0.600 / (5 / 1024) = 122.9 |
| First reading above 60.0 C | 124, because 124 x 5000 / 1024 = 605 tenths |
| ADC clock | prescaler 128: 8 MHz / 128 = 62.5 kHz, inside the 50 to 200 kHz the datasheet recommends |
The reading uses integer maths with a 32-bit intermediate so that reading x 5000 does not overflow. The Python file in the download checks the numbers above.
The program
/* LM35 temperature sensor on ADC0 (PA0) of an ATmega32, 5 V (AVCC) reference. The LM35 gives 10 mV per degree C.
Temperature in tenths of a degree = adc x 5000 / 1024 mV / 10 mV per degree x 10 = adc x 5000 / 1024 (integer maths).
A buzzer on PB0 sounds above 60 C. Build: avr-gcc -Os -mmcu=atmega32 -DF_CPU=8000000UL -o lm35.elf adc_lm35_alarm.c
(compiled only, not run) */
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define LIMIT_TENTHS 600
static uint16_t adc_read(uint8_t ch)
{
ADMUX = (1 << REFS0) | (ch & 0x07); /* AVCC reference, right adjusted */
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC)) { }
return ADC;
}
static int16_t tenths_c(uint16_t adc)
{
return (int16_t)(((uint32_t)adc * 5000UL) / 1024UL);
}
int main(void)
{
DDRB |= (1 << PB0);
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); /* clk/128 = 62.5 kHz at 8 MHz */
for (;;) {
uint16_t sum = 0;
for (uint8_t i = 0; i < 8; i++) sum += adc_read(0); /* average 8 readings */
int16_t t = tenths_c(sum / 8);
if (t > LIMIT_TENTHS) PORTB |= (1 << PB0); else PORTB &= (uint8_t)~(1 << PB0);
_delay_ms(200);
}
}
The program averages 8 conversions to smooth noise, then compares the result with LIMIT_TENTHS, which is 600 for 60.0 degrees. For a display, print the tenths value as t / 10 and t % 10. The basic LM35 connection reads only positive temperatures from 2 to 150 C; for negative readings the datasheet shows a different circuit.
If it does not work
| What you see | What to check |
|---|---|
| Reading jumps around | Add the capacitors at AVCC and AREF, and keep the sensor wire short or twisted with ground. |
| Always zero | Check the sensor power and that PA0 is the ADC0 input; the ADC must be enabled (ADEN). |
| Reading about double | The reference or the factor is not the one you think: 2.56 V and 5 V need different maths. |
| Buzzer always on | The limit constant is in tenths of a degree, so 600 means 60.0 C. |
Quick answers
More from this site
- Introduction to the AVR ADC: the basics
- ATmega32 ADC for light and temperature: more sensors
- AVR analog comparator: a yes or no instead
Downloads and references
- 💾 Code (avr-adc-lm35-code.zip)
adc_lm35_alarm.c and test_lm35.py.
- What is the purpose of the ADC in this project?
The ADC converts analog signals from sensors like the LM35 into digital words so the microcontroller can process them. - How many input channels does the Atmega32 ADC support?
The Atmega32 ADC can accept inputs from up to eight channels simultaneously. - Which reference voltage option is selected in this article?
The internal voltage option set to 2.56V within the Atmega32 MCU is used. - Why should pins VCC and AVCC be connected?
Pins 10 and 30 must be connected because the ADC gets its power supply from the AVCC pin. - How can you make the ADC supply voltage more stable?
Connecting a coil between the two power pins and a capacitor between AVCC and GND stabilizes the supply voltage. - Does the AREF capacitor connection matter if using internal voltage?
Yes, it is preferable to connect a capacitor between AREF and GND even when using internal voltage to prevent noise. - Can you use an external reference voltage with the Atmega32?
Yes, you can connect a customized voltage source to the AREF pin (pin 32) to use an external reference. - What happens before starting a conversion on the ADC?
You must first choose the specific input channel to convert before starting the conversion process.





