How to Interface LM35 Temperature Sensor in Embedded C
To interface the
LM35 temperature sensor in Embedded C, connect its output to an ADC pin of your microcontroller and read the analog voltage. Convert the ADC value to temperature by using the sensor's scale factor (10mV/°C) and the ADC resolution in your code.Syntax
To read temperature from LM35, you typically use the ADC (Analog to Digital Converter) of your microcontroller. The basic steps are:
- Initialize the ADC module.
- Start ADC conversion on the pin connected to LM35 output.
- Read the ADC digital value.
- Convert the ADC value to voltage.
- Calculate temperature using the formula:
Temperature (°C) = Voltage (mV) / 10.
Here is the general syntax pattern:
embedded_c
void ADC_Init(void); unsigned int ADC_Read(unsigned char channel); int main() { unsigned int adc_value; float voltage, temperature; const float Vref = 5.0; // Reference voltage in volts const unsigned int ADC_Max_Value = 1023; // For 10-bit ADC ADC_Init(); adc_value = ADC_Read(0); // Read from ADC channel 0 voltage = (adc_value * Vref) / ADC_Max_Value; // Convert ADC value to voltage temperature = voltage * 100.0; // LM35 outputs 10mV per degree Celsius while(1) { // Your code here } return 0; }
Example
This example demonstrates reading the LM35 sensor connected to ADC channel 0 on an 8-bit microcontroller with 10-bit ADC and 5V reference voltage. It calculates and prints the temperature in Celsius.
embedded_c
#include <avr/io.h> #include <util/delay.h> #include <stdio.h> #define VREF 5.0 #define ADC_MAX 1023.0 void ADC_Init() { ADMUX = (1<<REFS0); // AVcc with external capacitor at AREF pin ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC, prescaler 128 } unsigned int ADC_Read(unsigned char channel) { ADMUX = (ADMUX & 0xF8) | (channel & 0x07); // Select ADC channel ADCSRA |= (1<<ADSC); // Start conversion while(ADCSRA & (1<<ADSC)); // Wait for conversion to finish return ADC; } int main(void) { unsigned int adc_value; float voltage, temperature; ADC_Init(); while(1) { adc_value = ADC_Read(0); // Read ADC channel 0 voltage = (adc_value * VREF) / ADC_MAX; // Convert to voltage temperature = voltage * 100.0; // LM35: 10mV per degree, so 100 * voltage // For demonstration, assume a function to print float exists // printf("Temperature: %.2f C\n", temperature); _delay_ms(1000); } return 0; }
Output
Temperature: 25.00 C (example output varies with sensor reading)
Common Pitfalls
- Incorrect ADC reference voltage: Using a wrong reference voltage causes wrong temperature readings.
- Not initializing ADC properly: ADC must be enabled and configured before reading.
- Wrong ADC channel selection: Make sure the LM35 output is connected to the ADC channel you read.
- Ignoring sensor output scale: LM35 outputs 10mV per °C, so conversion must reflect this.
- Not averaging ADC readings: ADC noise can cause fluctuating values; averaging multiple readings improves stability.
embedded_c
/* Wrong way: Not enabling ADC or wrong channel */ unsigned int ADC_Read_Wrong() { ADCSRA = 0; // ADC disabled ADMUX = 0xFF; // Invalid channel ADCSRA |= (1<<ADSC); while(ADCSRA & (1<<ADSC)); return ADC; } /* Right way: Enable ADC and select correct channel */ void ADC_Init_Right() { ADMUX = (1<<REFS0); // AVcc reference ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); } unsigned int ADC_Read_Right(unsigned char channel) { ADMUX = (ADMUX & 0xF8) | (channel & 0x07); ADCSRA |= (1<<ADSC); while(ADCSRA & (1<<ADSC)); return ADC; }
Quick Reference
- LM35 output: 10mV per °C
- ADC resolution: Depends on microcontroller (e.g., 10-bit = 1024 steps)
- Voltage calculation:
Voltage = (ADC_value * Vref) / ADC_Max - Temperature calculation:
Temperature = Voltage * 100(for LM35) - ADC initialization: Enable ADC, set reference voltage, set prescaler
Key Takeaways
Connect LM35 output to an ADC pin and initialize ADC before reading.
Convert ADC readings to voltage using your microcontroller's reference voltage and ADC resolution.
Calculate temperature by dividing voltage (in mV) by 10 for LM35 sensor.
Always verify ADC reference voltage and channel selection to avoid wrong readings.
Average multiple ADC readings to reduce noise and get stable temperature values.