LM35 Temperature sensor with STM32

LM35 is an integrated-circuit temperature device with an output voltage linearly proportional to the centigrade temperature. As LM35 is not calibrated in Kelvin, user does not require to subtract large constant voltage from the output to obtain convenient centigrade scaling.

Features of LM35

  • Calibrate directly in Celsius
  • 0.5°C Ensured Accuracy ( at 25°C)
  • Rated for full -55°C to 150°C Range
  • Suitable for Remote Applications
  • Operates from 4V to 30V
  • Less than 60μA current drain
  • Low Self-Heating, 0.08°C in Still Air
  • Non-Linearity Only ±¼°C Typical
  • Low-Impedance Output, 0.1 Ω for 1-mA Load


  • Power Supplies
  • Battery Management
  • HVAC
  • Appliances

Click HERE to download LM35 datasheet.

Connections in LM35

We will use ADC to read values from LM35, And display it on the lcd. Lcd is connected via I2C using PCF8574. If you don’t know how to use DMA with ADC, check out this Tutorial. To know more about connecting LCD via I2C, go HERE.

Vcc is given to pin1, and pin3 is connected to the ground. Pin2 will be the input to the ADC pin of the microcontroller.

CubeMx Setup

Create new project in CubeMx and select desired ADC channel.

ADC1 channel 0 is selected and I2C1 is selected

Go to ADC1 config in configuration tab and make sure your settings are as follows

Scan Conversion- Enabled Continuous conv- Enabled DMA request – Enabled Sampling time – Highest(480 cycles)

Go to DMA setting and select the following

That’s it for CubeMx setup. Generate the project and open it

Some Insight into the Code

create variables

uint32_t adc_buf; 
int temp;

uint32_t adc_buf is the ADC buffer, where values from LM35 will be stored.

int temp is the variable to store the temperature.

Write a Callback Function

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
	temp = adc_buf*.322;   // convert value from adc buffer to temperature in Centigrade

Start ADC with DMA

HAL_ADC_Start_DMA (&hadc1, &adc_buf, 1);   // start ADC with DMA

Convert the temp (integer) to temperature (character)

char temperature[2];

sprintf (temperature, "%d", temp);

send the temperature to the LCD

lcd_send_string ("Temp= ");

lcd_send_string (temperature);
lcd_send_string ("C");		
lcd_send_cmd (0x86);


The following is the output in STMStudio. Variables observed are adc_buf and temp.

adc_buf value is 103 and temp is 32

As you can see below the LCD showing temperature of the surrounding.

LCD showing temp= 33C.

Notify of
Oldest Most Voted
Inline Feedbacks
View all comments