How to Use ADC (Analog to Digital Converter) in AVR – Atmega32

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.

IN SHORT
  • 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.
0.49 C
per count at 5 V
0.25 C
per count at 2.56 V
124
alarm reading
240 B
program size
Chain: LM35 output, ADC0 input with 10-bit conversion, averaging of eight readings, buzzer above 60 degrees
From heat to a buzzer

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

Why average?
The ADC has noise of a count or two; averaging eight readings reduces it.
Can I read more than one sensor?
Yes, change the channel in ADMUX before each conversion.
Can I use the internal reference?
Yes, and it doubles the resolution for the LM35, as the table shows.

Downloads and references

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to Fire Alarm Project:

  • 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.

About The Author

Author avatar

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Stay in the loop

New AVR projects, tutorials and schematics, every week.

Subscribe Browse projects

Scroll to Top