ATmega328 Temperature Sensor : A Complete Guide

The ATmega328 microcontroller, a popular choice among embedded systems enthusiasts and DIY electronics enthusiasts, offers a wide range of functionalities. One of the most commonly used applications of the ATmega328 is interfacing with a temperature sensor, such as the LM35. In this article, we will delve into the world of ATmega328 microcontroller and LM35 temperature sensor, exploring their circuit diagram, program code, and the vast potential they hold for various projects.

ATmega328

The ATmega328, a high-performance microcontroller from the AVR family, is widely used in embedded systems and Arduino-based projects. It features a 8-bit RISC architecture with 32KB of Flash memory, 1KB of EEPROM, and 2KB of SRAM, making it a powerful choice for a wide range of applications. With its versatile I/O ports, timers, and UART, the ATmega328 offers immense flexibility for interfacing with sensors and other peripherals.

LM35 Temperature Sensor

The LM35 is a precision temperature sensor that provides an analog output voltage proportional to the temperature in Celsius. It has a wide temperature range of -55°C to 150°C, making it suitable for a variety of temperature sensing applications. The LM35 temperature sensor operates on a single power supply and requires minimal external components, making it easy to interface with microcontrollers like the ATmega328.

Circuit Diagram of ATmega328 with LM35

To interface the LM35 temperature sensor with the ATmega328, a simple circuit can be designed. The LM35 can be connected to one of the analog input pins of the ATmega328, and the temperature proportional voltage can be read using the built-in Analog-to-Digital Converter (ADC).

The circuit diagram is as follows.

ATmega328 with LM35 and LCD circuit diagram
In the ATmega328 with LM35 and LCD circuit diagram above, we have connected the LCD to port D of ATmega328. The LM35 temperature sensor is connected to analog pin ADC0(PC0). See Programming ATmega328P ADC in C to learn how to use ATmega328p ADC. A LED is connected to Port B pin 1(PB1). The temperature is read and displayed on the LCD. If the temperature is equal to or higher than 40 degree Celsius then the LED is turned on to indicate that there is high temperature.

 ATmega328 Program Code

Once the circuit is set up, the ATmega328 can be programmed to read the temperature values from the LM35 and perform various tasks based on the temperature readings. Here's an example of a simple program code in C language that reads the temperature from the LM35 and displays it on an LCD display. Also when the temperature is higher than 40 degree Celsius the LED is turned on. Below is the ATmega328 program code for temperature sensor.

#include <avr/io.h>
#include <stdlib.h>
#include "lcd.h"
#include "adc.h"

int main()
 { 
    lcdinit();
    adcinit();
	//LED
	DDRB |= (1<<PB1);

    char Ctemp[10], Ftemp[10];

	float c, f;
	lcdstr("***Temperature***");

   while(1){
		c = (adcread(0)*4.88);
		c = (c/10.00);
        f = (c*9)/5 + 32;
		
		if(c >=40){
			PORTB |= (1<<PB1);
		}
		else{
			PORTB &= ~(1<<PB1);
		}

		dtostrf(c, 6, 2, Ctemp);
		dtostrf(f, 5, 2, Ftemp);

		lcdgoto(0,2);
		lcdstr(Ctemp);
		lcdchar(0xDF); 
	    lcdstr("C");
	    lcdstr("(");
	    lcdstr(Ftemp);
		lcdchar(0xDF); 
	    lcdstr("F)");
      }
   return 0;
 }

The provided C program is designed to read data from an LM35 temperature sensor, display the temperature on an LCD (Liquid Crystal Display), and control the state of an LED based on the temperature reading. It uses two custom libraries for LCD and ADC(Analog to Digital converter) which you can download from below link.

Download adc.c Download adc.h

Download lcd.c Download lcd.h

Let's go through the program step by step:

1. The necessary header files are included at the beginning of the program:

 The avr/io.h header file provides definitions for AVR microcontroller's I/O registers, while stdlib.h provides standard library functions such as dtostrf() used for converting floating-point numbers to strings. lcd.h and adc.h are custom header files for interfacing with an LCD and an ADC (Analog-to-Digital Converter) respectively.

2. The main() function is the entry point of the program. It begins with initializing the LCD and ADC using the lcdinit() and adcinit() functions, respectively.

3. The while(1) loop continuously reads the temperature from the LM35 temperature sensor, converts it to Celsius and Fahrenheit, displays the values on the LCD with appropriate units, and controls the state of the LED based on the temperature reading. The lcdgoto(), lcdstr(), lcdchar() functions are used to display text and characters on the LCD. The dtostrf() function is used to convert the floating-point temperature values to strings with the desired number of digits and decimal places.

4. Inside the loop, the program reads the ADC value from channel 0 using the adcread() function, which reads the voltage from the LM35 temperature sensor connected to the ADC input and converts it to a digital value. The obtained ADC value is then converted to a voltage value in volts by multiplying with a factor of 4.88, as per the LM35 sensor's specifications.

5. The voltage value is then divided by 10 to convert it to Celsius temperature, as the LM35 sensor has a linear output of 10 mV/°C. The Celsius temperature is then converted to Fahrenheit temperature using the formula (Celsius * 9) / 5 + 32.

6. The program then checks if the Celsius temperature is greater than or equal to 40°C, indicating a high temperature condition. If so, it sets the PB1 pin (connected to the LED) to HIGH, turning on the LED. Otherwise, it sets the PB1 pin to LOW, turning off the LED.

7. The dtostrf() function is used to convert the Celsius and Fahrenheit temperature values to strings with the desired number of digits and decimal places. The converted temperature values are then displayed on the LCD using the lcdgoto(), lcdstr(), and lcdchar() functions, along with the appropriate units (°C and °F).

8. The program continues to loop indefinitely, continuously reading and displaying the temperature values on the LCD, and controlling the LED based on the temperature reading.

9. The return 0; statement at the end of the main() function indicates the successful completion of the program. 

Once the code is compiled the next step is to Program ATmega328p using ATMEL Studio & AVR ISP MKII.

Conclusion

The ATmega328 with the LM35 temperature sensor is a powerful combination that opens up a world of possibilities for temperature sensing applications. From weather monitoring systems to home automation projects, the ATmega328 and LM35 offer a reliable and accurate solution for temperature measurement. With the right circuit design and program code, you can unleash the full potential of these components and create innovative projects that cater to

 References

- Reading and displaying temperature using ATmega32

- LM35 Temperature Sensor with ATmega32 and LCD

Post a Comment

Previous Post Next Post