ATmega16 Temperature Controller with LM35, 16×2 LCD & Fan Relay

Build an ATmega16 temperature controller with an LM35 sensor, 16×2 LCD, relay-driven fan and alarm. Wiring table, diagram, fuses and full AVR C code.

  • DifficultyIntermediate
  • Build time2–4 hours
  • Board / MCUATmega16
  • Parts14 items
  • CodeAVR C

Need it ready-made? Buy Ready Project

This ATmega16 temperature controller reads an LM35 sensor, shows the temperature on a 16×2 LCD, and switches a 12 V cooling fan through a relay when it gets too warm. If the temperature keeps rising past a second limit, a red LED and buzzer warn you. It is a practical project for a cabinet, an inverter box, an incubator or a small server shelf.

It is a medium-level build. You use the ATmega16’s built-in 10-bit ADC, drive an HD44780 LCD in 4-bit mode without any library, and write a simple control loop with hysteresis, all in plain AVR C.

How the ATmega16 Temperature Controller Works

  • Sensing: the LM35 gives 10 mV per °C, so 30 °C is 0.30 V. Its output goes to PA0 (ADC0).
  • Measuring: the ADC uses the ATmega16’s internal 2.56 V reference. Each ADC step is 2.5 mV, which is 0.25 °C. The code averages 16 readings to remove noise.
  • Fan control with hysteresis: the fan relay on PC7 turns on at 35 °C and stays on until the temperature falls to 32 °C. The 3 °C gap stops the relay chattering on and off around one value.
  • Alarm: at 45 °C the LED on PD7 and the buzzer on PD6 blink, and the LCD shows ALARM!.
  • Display: line 1 shows the temperature to 0.1 °C, line 2 shows the fan state and the set point.

One ATmega16 detail matters here: PC2–PC5 are also the JTAG pins, and JTAG is on from the factory. The code writes the JTD bit twice at start-up to turn JTAG off, and the fuse values below also disable it, so the LCD data lines work normally.

Components Required

  • ATmega16 or ATmega16A microcontroller (DIP-40) – 1
  • 16 MHz crystal – 1, with 22 pF ceramic capacitors – 2
  • LM35 temperature sensor (TO-92) – 1
  • 16×2 character LCD (HD44780 compatible) – 1
  • 10 kΩ potentiometer for LCD contrast – 1
  • 1-channel 5 V relay module – 1
  • 12 V DC cooling fan – 1
  • Active 5 V buzzer – 1
  • Red LED – 1, with 330 Ω resistor – 1
  • 10 kΩ resistor (reset pull-up) – 1, 220 Ω resistor (LCD backlight) – 1
  • 7805 voltage regulator – 1, 10 µF and 100 µF electrolytic capacitors, 100 nF ceramic capacitors – 3
  • 12 V DC adapter (powers the fan and, through the 7805, the 5 V rail)
  • USBasp or another AVR ISP programmer
  • Breadboard or perfboard and jumper wires

Wiring Table (Pin-to-Pin)

PartPart PinATmega16 PinNotes
7805IN / GND / OUT12 V in / GND / 5 V rail10 µF on input, 100 µF + 100 nF on output
ATmega16VCC (10), AVCC (30)5 VAVCC must be connected for the ADC
ATmega16GND (11, 31)GND100 nF between VCC and GND near the chip
ATmega16RESET (9)10 kΩ to 5 VAlso goes to the ISP header
16 MHz crystalBoth legsXTAL1 (13), XTAL2 (12)22 pF from each pin to GND
ATmega16AREF (32)100 nF to GNDDo not tie AREF to 5 V (code uses 2.56 V internal)
LM351 (+Vs)5 VFlat face toward you, pins left to right
LM352 (Vout)PA0 / ADC0 (40)10 mV per °C
LM353 (GND)GND
LCD1 VSS, 5 RW, 16 KGNDRW tied low (write only)
LCD2 VDD5 V
LCD3 V010 kΩ pot wiperPot ends to 5 V and GND
LCD4 RSPC0 (22)
LCD6 EPC1 (23)
LCD11–14 D4–D7PC2–PC5 (24–27)JTAG pins, disabled in code and fuses
LCD15 A5 V through 220 ΩBacklight
Relay moduleIN / VCC / GNDPC7 (29) / 5 V / GNDUsually active-low
Relay contactsCOM / NO12 V + / fan +Fan − to 12 V −
Buzzer+ / −PD6 (20) / GNDActive buzzer, under 20 mA
LEDAnode / cathodePD7 (21) via 330 Ω / GNDAlarm indicator

Connection Diagram

The illustration below is also the featured image and gives an overview of the build. Use the pin-accurate drawing under it, which matches the wiring table, when you connect the parts.

Wiring diagram of an ATmega16 temperature controller with an LM35 sensor, 16x2 LCD, relay-driven cooling fan, status LED, buzzer and 7805 power supply
ATmega16 temperature controller overview: LM35, 16×2 LCD, relay and fan, LED, buzzer and 7805 supply.
ATmega16 Temperature Controller – Wiring ATmega16 (DIP-40) PA0/ADC0 (40) AREF (32) VCC (10), AVCC (30) GND (11, 31) RESET (9) XTAL1 (13), XTAL2 (12) PC0 (22) RS PC1 (23) E PC2–PC5 (24–27) D4–D7 PC7 (29) relay PD6 (20) buzzer PD7 (21) LED LM35 (flat face front) Pin 1 +Vs → 5 V Pin 2 Vout → PA0 Pin 3 GND → GND AREF 100 nF→GND RESET: 10 kΩ to 5 V 16 MHz crystal XTAL1–XTAL2 22 pF from each XTAL pin to GND 100 nF VCC–GND near chip 16×2 LCD (HD44780) Temp: 31.5 CFan:ON Set:35 1 VSS→GND, 2 VDD→5V, 3 V0→10k pot wiper 4 RS→PC0, 5 RW→GND, 6 E→PC1 11–14 D4–D7→PC2–PC5, 15 A→5V(220Ω), 16 K→GND 5 V Relay Module + 12 V Fan IN→PC7, VCC→5V, GND→GND 12 V + → COM, NO → fan + fan − → 12 V − PD6 → active 5 V buzzer (+), buzzer (−) → GND PD7 → 330 Ω → red LED anode, cathode → GND 7805 supply: 9–12 V in → 7805 → 5 V rail 10 µF on input, 100 µF + 100 nF on output; all grounds common Yellow = sensor signal, green = LCD bus, blue = relay, orange = alarm outputs, red = power. Pin numbers are for the DIP-40 package.
Pin-accurate connection diagram: LM35 on PA0, LCD on PC0–PC5, relay on PC7, buzzer on PD6 and LED on PD7.

ATmega16 Code (AVR-GCC)

Create a new GCC C project for the ATmega16 in Microchip Studio, or save the file as main.c if you use avr-gcc from the command line. No external libraries are needed. You can change the fan and alarm temperatures at the top of the file.

/*
  ATmega16 Temperature Controller
  LM35 sensor + 16x2 LCD (4-bit) + relay-driven fan + LED and buzzer alarm
  Compiler : AVR-GCC (Microchip Studio, or avr-gcc + avrdude)
  Clock    : 16 MHz external crystal  (fuses: lfuse 0xFF, hfuse 0xD9)

  Wiring
    LM35 Vout -> PA0 (ADC0)    AREF -> 100 nF -> GND    AVCC -> 5 V
    LCD RS -> PC0   E -> PC1   D4..D7 -> PC2..PC5   RW -> GND
    Relay IN -> PC7            Buzzer -> PD6         LED -> PD7 (330 ohm)
*/

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>

// ---- user settings (temperatures in tenths of a degree C) ----
#define FAN_ON_X10     350   // fan turns ON at 35.0 C
#define FAN_OFF_X10    320   // fan turns OFF at 32.0 C (3 C hysteresis)
#define ALARM_X10      450   // buzzer + LED alarm at 45.0 C
#define RELAY_ACTIVE_LOW 1   // most blue relay modules switch ON when IN is LOW

// ---- LCD pins on PORTC ----
#define LCD_RS PC0
#define LCD_E  PC1
// D4..D7 are PC2..PC5

#define RELAY_PIN  PC7
#define BUZZER_PIN PD6
#define LED_PIN    PD7

// Send the low 4 bits of n to D4..D7 and pulse E
static void lcd_nibble(uint8_t n) {
  PORTC = (PORTC & 0xC3) | ((n & 0x0F) << 2);   // keep PC0, PC1, PC6, PC7
  PORTC |= (1 << LCD_E);
  _delay_us(1);
  PORTC &= ~(1 << LCD_E);
  _delay_us(50);
}

static void lcd_send(uint8_t value, uint8_t is_data) {
  if (is_data) PORTC |= (1 << LCD_RS); else PORTC &= ~(1 << LCD_RS);
  lcd_nibble(value >> 4);
  lcd_nibble(value);
  if (!is_data && value < 4) _delay_ms(2);        // clear / home are slow
}

static void lcd_cmd(uint8_t c)  { lcd_send(c, 0); }
static void lcd_char(char c)    { lcd_send((uint8_t)c, 1); }
static void lcd_print(const char *s) { while (*s) lcd_char(*s++); }
static void lcd_goto(uint8_t row, uint8_t col) { lcd_cmd(0x80 | (row ? 0x40 : 0x00) | col); }

static void lcd_init(void) {
  _delay_ms(50);                 // wait for LCD power-up
  PORTC &= ~(1 << LCD_RS);
  lcd_nibble(0x03); _delay_ms(5);
  lcd_nibble(0x03); _delay_ms(1);
  lcd_nibble(0x03);
  lcd_nibble(0x02);              // switch to 4-bit mode
  lcd_cmd(0x28);                 // 4-bit, 2 lines, 5x8 font
  lcd_cmd(0x0C);                 // display on, cursor off
  lcd_cmd(0x06);                 // auto-increment
  lcd_cmd(0x01);                 // clear
}

// ADC with the internal 2.56 V reference: 1 step = 2.5 mV = 0.25 C for the LM35
static void adc_init(void) {
  ADMUX  = (1 << REFS1) | (1 << REFS0);                               // 2.56 V ref, channel ADC0
  ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);  // enable, 16 MHz / 128 = 125 kHz
}

static uint16_t adc_read(void) {
  ADCSRA |= (1 << ADSC);
  while (ADCSRA & (1 << ADSC)) { }
  return ADC;
}

// Average 16 readings and convert to tenths of a degree C
static int16_t read_temp_x10(void) {
  uint32_t sum = 0;
  for (uint8_t i = 0; i < 16; i++) { sum += adc_read(); _delay_ms(2); }
  // temp_x10 = adc * 2.5 mV / 10 mV per C * 10  = adc * 2.5
  return (int16_t)((sum * 25UL) / 160UL);
}

static void set_relay(uint8_t on) {
  uint8_t high = RELAY_ACTIVE_LOW ? !on : on;
  if (high) PORTC |= (1 << RELAY_PIN); else PORTC &= ~(1 << RELAY_PIN);
}

int main(void) {
  // PC2..PC5 are JTAG pins on the ATmega16. Turn JTAG off in software
  // (JTD must be written twice within 4 clock cycles) so the LCD can use them.
  uint8_t m = MCUCSR | (1 << JTD);
  MCUCSR = m;
  MCUCSR = m;

  // Relay off level first, then make the pins outputs (no click at power-up)
  set_relay(0);
  DDRC = 0xFF;                                   // LCD + relay
  DDRD |= (1 << BUZZER_PIN) | (1 << LED_PIN);
  PORTD &= ~((1 << BUZZER_PIN) | (1 << LED_PIN));

  lcd_init();
  adc_init();
  lcd_print(" ATmega16 Temp ");
  lcd_goto(1, 0);
  lcd_print("  Controller   ");
  _delay_ms(1500);
  lcd_cmd(0x01);

  uint8_t fan_on = 0;
  uint8_t blink = 0;
  char line[17];

  while (1) {
    int16_t t = read_temp_x10();

    // Fan control with hysteresis
    if (!fan_on && t >= FAN_ON_X10)  fan_on = 1;
    if (fan_on  && t <= FAN_OFF_X10) fan_on = 0;
    set_relay(fan_on);

    // Over-temperature alarm: LED and buzzer blink together
    uint8_t alarm = (t >= ALARM_X10);
    blink ^= 1;
    if (alarm && blink) PORTD |= (1 << BUZZER_PIN) | (1 << LED_PIN);
    else                PORTD &= ~((1 << BUZZER_PIN) | (1 << LED_PIN));

    // Line 1: temperature
    snprintf(line, sizeof(line), "Temp:%3d.%d C    ", t / 10, t % 10);
    lcd_goto(0, 0); lcd_print(line);
    // Line 2: fan state and set point (or ALARM)
    if (alarm) snprintf(line, sizeof(line), "Fan:ON  ALARM!  ");
    else       snprintf(line, sizeof(line), "Fan:%s Set:%2d  ", fan_on ? "ON " : "OFF", FAN_ON_X10 / 10);
    lcd_goto(1, 0); lcd_print(line);

    _delay_ms(400);
  }
}

Compile, Set Fuses and Upload

A new ATmega16 runs from its internal 1 MHz oscillator with JTAG enabled. Set the fuses once so it uses the 16 MHz crystal and frees PORTC. With a USBasp and avrdude:

avr-gcc -mmcu=atmega16 -Os -o main.elf main.c
avr-objcopy -O ihex -R .eeprom main.elf main.hex
avrdude -c usbasp -p m16 -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m
avrdude -c usbasp -p m16 -U flash:w:main.hex:i

Connect the crystal before writing the fuses. If the fuse is set to an external crystal and none is fitted, the chip will not respond to the programmer until you give it a clock.

Testing and Calibration

  1. Power the board and turn the contrast pot until the LCD text is clear.
  2. Check the room temperature on the LCD against a known thermometer. The LM35 is usually within ±0.5 °C at room temperature.
  3. Warm the LM35 gently with your fingers or a hair dryer on low. At 35 °C the relay should click and the fan should start.
  4. Let it cool. The fan should stop at 32 °C, not at 34.9 °C, which shows the hysteresis is working.
  5. Heat it carefully past 45 °C to check that the LED, buzzer and ALARM! message work.

Troubleshooting

ProblemFix
LCD shows only black boxesAdjust the contrast pot. If it still shows boxes, check RS, E and D4–D7, and confirm JTAG is off (hfuse 0xD9).
LCD shows random charactersThe clock is wrong, so the delays are wrong. Check the crystal, the 22 pF capacitors and lfuse 0xFF.
Temperature reads 0 or jumps wildlyConnect AVCC (pin 30) to 5 V and fit the 100 nF capacitor on AREF. Keep the LM35 wire short or use shielded cable.
Temperature reads far too highAREF is probably tied to 5 V. Remove that link; the internal 2.56 V reference needs AREF free with only a capacitor.
Fan works the wrong way roundYour relay module is active-high. Set RELAY_ACTIVE_LOW to 0.
Chip resets when the relay clicksAdd a 100 µF capacitor on the 5 V rail and keep fan wires away from the LM35 and the reset line.

Ideas to Extend This Project

  • Add two push-buttons on PD2 and PD3 to change the set point, and save it in EEPROM.
  • Replace the relay with a logic-level MOSFET and use PWM on OC1A (PD5) for variable fan speed.
  • Add a second LM35 on PA1 to compare inside and outside temperature.
  • Send the readings over the UART (PD0/PD1) to a PC or a Bluetooth module for logging.

FAQ

Can I use an ATmega32 instead? Yes. The ATmega32 has the same pinout and registers for everything used here, so change only the -mmcu and avrdude part names.

Why use the 2.56 V reference and not 5 V? The LM35 output stays under 1.5 V for normal temperatures. A smaller reference gives 0.25 °C steps instead of about 0.5 °C, so the reading is finer.

Can the relay switch a mains fan? The relay contacts may be rated for 250 V AC, but start with a 12 V DC fan. Switch mains only inside a closed, insulated enclosure, and ask a qualified electrician if you are not sure.

Related Projects

Browse more ATmega16 on Arduino Project Hub.

1 thought on “ATmega16 Temperature Controller with LM35, 16×2 LCD & Fan Relay”

Leave a Comment