Menu

728x90

468x60

Navbar

Showing posts with label LM35. Show all posts
Showing posts with label LM35. Show all posts

Friday, January 30, 2026

ATMega644P Analog to Digital Converter (ADC) Example

Overview

The Analog to Digital Converter (ADC) convert analog voltage value to a digital representation that the micro-processor could understand. Most of modern micro-controller has at least one built-in ADC module that can be use by software configuration. The most common ADC resolution is between 8-bit and 12-bit with an acceptable conversion rate.

ATMega644P Analog to Digital Converter (ADC) Example

 

The resolution give more precise digital data for the micro-controller. However its reference voltage and lower clock also effect the precision of the analog input value. Some 32-bit micro-controller especially the STM32 micro-controller series has an ADC module with the resolution of 12-bit with the conversion rate up to 2MSPS. Some 16-bit dsPIC micro-controller series has an ADC module with the resolution of 10-bit and 1~2MSPS conversion rate.

Commonly we use the ADC to read temperature value, flex sensor and strain gauge etc.

ATMega644P ADC Programming Example

The ADC of the ATMega644P has a 10-bit analog to digital converter with the maximum conversion rate up to 15kSPS. It has up to 8 analog input channel (ADC7:ADC0) via PortA. The conversion triggers by user software (set ADC to start) or by any interrupt sources.

ATMega644P Analog to Digital Converter (ADC) Example
Analog-to-digital Converter Block Schematic

 

The software configuration of the ADC module of AVR micro-controller is simpler than other micro-controllers. It has a few registers to configure. 

I don't write its detail here since the ADC process of the ATMega644P is very similar to the ATMega32. 

ADC and LED Example 1

In this introductory example the ATMega644P read the analog input from ADC0(PA0). The 10-bit ADC result will converts to 8-bit by shifting right that will display on PORTB.

C Source Code:

  1. /*
  2. * 9-ADC_LED.c
  3. *
  4. * Created: 1/30/2026 3:28:06 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>

  8. int main(void)
  9. {
  10. DDRB=0xFF;
  11. //PA0 or ADC0 as an analog input
  12. DDRA=0;
  13. //Turn on the ADC module
  14. ADCSRA=(1<<ADEN);
  15. uint16_t temp;
  16. while (1)
  17. {
  18. //Start the conversion
  19. ADCSRA|=(1<<ADSC);
  20. //Wait for the completion
  21. while((ADCSRA&(1<<ADSC))==1);
  22. //Read the result
  23. temp=ADCL+(ADCH<<8);
  24. PORTB=temp>>2;
  25. }
  26. }
  27.  

Proteus Simulation:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Simulation #1

AVR Prototype Board Test:

ATMega644P Analog to Digital Converter (ADC) Example

ATMega644P Analog to Digital Converter (ADC) Example

 

ADC and LED Example 2

I use this prototype board's LED and POT to make a LED shifting using the ADC voltage level. It's very similar to a ring counter. However I need to convert between ADC value counter value.

Source Code:

  1. /*
  2. * 9-ADC_BarGraph.c
  3. *
  4. * Created: 1/30/2026 4:21:13 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>

  8. int main(void)
  9. {
  10. DDRB=0xFF;
  11. //PA0 or ADC0 as an analog input
  12. DDRA=0;
  13. //Turn on the ADC module
  14. ADCSRA=(1<<ADEN);
  15. long temp,div;
  16. while (1)
  17. {
  18. //Start the conversion
  19. ADCSRA|=(1<<ADSC);
  20. //Wait for the completion
  21. while((ADCSRA&(1<<ADSC))==1);
  22. //Read the result
  23. temp=ADCL+(ADCH<<8);
  24. div=(temp*8)/1024;
  25. PORTB=(1<<div);
  26. }
  27. }


Proteus Schematic and Simulation:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Schematic and Simulation

AVR Prototype Board Test: 

ATMega644P Analog to Digital Converter (ADC) Example

ATMega644P Analog to Digital Converter (ADC) Example

 

ADC and LED Example 2

There are two on-board ADC inputs, POT and LM35 temperature sensor. The LCD will display the analog input from POT(ADC0) and LM35(ADC1). The LM35 temperature (in degree Celsius) is equivalent to 10mV that's 2 in digital value (+5.0VDC voltage reference).

ATMega32 Interfaces To LM35 And 16x2 Character LCD
LM35DZ in TO-92 Package

 

Source Code:

  1. /*
  2. * 9-ADC_LCD.c
  3. *
  4. * Created: 1/30/2026 4:47:25 PM
  5. * Author : Admin
  6. */

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

  11. void adc_init(void){
  12. /*Set PA0 and PA1 to input*/
  13. DDRA&=~(1<<0);
  14. DDRA&=~(1<<1);
  15. ADCSRA=(1<<ADEN);
  16. /*Select AD0*/
  17. ADMUX=0x03;
  18. }

  19. unsigned int read_adc(char channel){
  20. //Select Channel
  21. ADMUX=channel;
  22. //Start Convsersion
  23. ADCSRA|=(1<<ADSC);
  24. //Wait for Completion
  25. while(ADCSRA&(1<<ADSC));
  26. //Get the 10-bit values
  27. return (ADCL+(ADCH<<8));
  28. }
  29. int main(void)
  30. {
  31. /* Replace with your application code */
  32. lcd_init();
  33. lcd_text("ATMega644P ADC");
  34. lcd_xy(1,2);
  35. lcd_text("POT AND LM35");
  36. adc_init();
  37. _delay_ms(1000);
  38. lcd_clear();
  39. unsigned int temp;
  40. float voltage;
  41. char msg[16];
  42. lcd_command(0x0C);
  43. while (1)
  44. {
  45. //Read POT
  46. temp=read_adc(0);
  47. //Convert To Voltage
  48. voltage=temp*5.0/1023;
  49. sprintf(msg,"ADC0: %4d %.2fV",temp,voltage);
  50. lcd_xy(1,1); lcd_text(msg);
  51. //Read LM35
  52. temp=read_adc(1);
  53. voltage=temp*5.0/1023;
  54. voltage*=100;
  55. sprintf(msg,"Temp: %0.1f%cC",voltage,223);
  56. lcd_xy(1,2); lcd_text(msg);
  57. _delay_ms(1000);
  58. }
  59. }

Proteus Schematic:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Schematic

AVR Prototype Board: 

ATMega644P Analog to Digital Converter (ADC) Example
Program Tested on my AVR Prototype Board

Unfortunately I bough some fake LM35DZ due to their local availability and low cost, around 0.50USD. The original LM35DZ(TO92) temperature chip costs around 1USD. However some online store also sell this chip in bulk price at very low cost.

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote

 


 

 

 

Tuesday, September 1, 2020

ATMega32 Interfaces To LM35 And 16x2 Character LCD

Overview

In some previous post, I have wrote some posts about analog to digital convert. This peripheral converts any analog output devices and converter them to digital value. Some analog output sensor like LM35 creates an analog voltage output linear to its 10 mV step temperature value.

A display device show the information or status of the MCU and other input output devices. I posted some examples on using the industrial standard HD44780 LCD controller.

ATMega32 Interfaces To LM35 And 16x2 Character LCD
Example Program

In this example post, ATMega32 reads the analog temperature value and just display this information to the display. The temperature is in both degree Celsius and degree Fahrenheit.

Interfacing And Programming

LM35 connects to the analog input pin ADC7 - the user could select any analog pin within its 8 analog inputs. The MCU clocks at 16 MHz due to a pre-soldered crystal on the learning board. However, I set the ADC conversion clock rate to 1:128.

ATMega32 Interfaces To LM35 And 16x2 Character LCD
LM35DZ in TO-92 Package

The display operates in 4-bit interfacing mode. A single PORTD of the MCU is sufficient to do this task.

ATMega32 Interfaces To LM35 And 16x2 Character LCD
Schematic Diagram

C source code of this example lists below, click here to download a zip archive.

Note: Without configuring the sprintf() function in the project property, it can't convert the float data type to string value. We must the project properties as follow.
  1. Open the project properties by pressing ALT+F7. 
Tick the required tick box as shown in the picture

  1. Fill the required link flag
Fill the text box with this string "-lprintf_flt"

Then we can use the sprintf() function with float data type properly. If you still have the problem with sprintf(), see this link because I got it work from there.

Sunday, June 7, 2020

Temperature reading from LM35 with ADC module of ATMega32

In this ADC module, we have an easy example of interfacing with device ADC. LM35 is analog temperature sensor capable of measuring the temperature between -55 to 150 degree Celsius. It outputs an analog voltage value that is 10 mV per degree Celsius proportionally. For more information please see this post.

Reading an analog temperature data with ADC module of ATMega32
A sample picture and pin diagram of LM35

In this example the system could only read the positive temperature value. Because I have some difficulty of working with negative voltage reading at this time.

Reading an analog temperature data with ADC module of ATMega32
LM35 connects to ADC0. There is no negative temperature reading here.

Source code:

#include <avr/io.h>
#define  F_CPU 4000000UL
#include <util/delay.h>
int main(void)
{ float voltage;
 float temperature;
 int _temperature;
 char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
 0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
 //PortC as output
 DDRC=0xFF;
 //PortD as output
 DDRD=0xFF;
 //PA0 or ADC0 as an analog input
 DDRA=0;
 //Turn on the ADC module
 ADCSRA|=(1<<ADEN);
 while (1)
 {
  //Start the conversion
  ADCSRA|=(1<<ADSC);
  //Wait for the completion
  while((ADCSRA&(1<<ADSC))==1);
  /*Convert ADC to voltage*/
  voltage=(ADCL+(ADCH<<8))*5.0/1024;
  /*Convert to temperature*/
  temperature=voltage*100;
  /*make dot point*/
  temperature*=10;
  _temperature=(int)temperature;
  /*Displaying the Data*/
  PORTD=0x00;
  PORTC=ssd[_temperature/1000];
  if(_temperature>=1000) PORTD=0x01;
  _delay_ms(10);
  PORTD=0x00;
  PORTC=ssd[(_temperature%1000)/100];
  PORTD=0x02;
  _delay_ms(10);
  PORTD=0x00;
  PORTC=ssd[(_temperature%100)/10]|0x80;
  PORTD=0x04;
  _delay_ms(10);
  PORTD=0x00;
  PORTC=ssd[_temperature%10];
  PORTD=0x08;
  _delay_ms(10);
   }
}

Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.

Reading an analog temperature data from LM35 with ADC module of ATMega32



Click here to download its source file.


336x280

Search This Blog

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (13) AT89C52 (7) ATMega32 (58) ATMega644P (27) AVR (87) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (133) Distance Sensor (3) ds1307 (10) DS18B20 (5) ds3231 (1) ds3232 (2) dsPIC (5) dsPIC30F1010 (3) dsPIC30F2010 (5) EEPROM (5) Environment Sensor (5) ESP32 (1) esp8266 (1) Graphical LCD (2) I2C (38) ILI9341 (1) Input/Output (76) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (2) LCD (75) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Module PCB (1) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (10) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (10) Software TWI (2) SPI (39) ST7735 (1) STM32 (11) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (5) STM32CubeIDE (13) STM32F103C8T6 (9) STM32F401CCU6 (1) SysTick (4) temperature sensor (13) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (8) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)