STM32 LM35 Temperature Sensor Example – LM35 With STM32 ADC

Previous Tutorial Previous Tutorial Tutorial 26 Next Tutorial Next Tutorial
STM32 LM35 Temperature Sensor Interfacing
STM32 Course Home Page ????

 

STM32 LM35 Temperature Sensor Example Code Project Blue Pill STM32F103C8

 

In this article, we’ll create a simple project for STM32 LM35 temperature sensor interfacing with ADC. It’ll also include an LCD screen to display the temperature. You’ll also learn how to add and configure the ECUAL drivers, it was described before in a previous tutorial, but today will be another practice example to see how to build portable high-level application firmware.

[toc]


   Required Components For LABs   

 

All the example code/LABs/projects in the course are going to be done using those boards below.

QTY Component Name ???? Amazon.com ???? eBay.com
2 BreadBoard Amazon eBay
1 LEDs Kit Amazon Amazon eBay
1 Resistors Kit Amazon Amazon eBay
1 Capacitors Kit Amazon Amazon eBay & eBay
2 Jumper Wires Pack Amazon Amazon eBay & eBay
1 9v Battery or DC Power Supply Amazon Amazon Amazon eBay
1 Micro USB Cable Amazon eBay
1 Push Buttons Amazon Amazon eBay
1 LM35 Temperature Sensor  Amazon eBay
1 Alphanumeric LCD Module Amazon eBay
1 USB-TTL Converter or FTDI Chip Amazon Amazon eBay  eBay

★ Check The Full Course Complete Kit List

Some Extremely Useful Test Equipment For Troubleshooting:

Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.


   STM32 LM35 Temperature Sensor Interfacing   

 

The LM35 series are precision integrated-circuit temperature devices with an output voltage linearly proportional to the Centigrade temperature. It has a linear + 10-mV/°C Scale Factor. Which means you’ll have to measure its output voltage and divide it by 0.01 to get the temperature reading in °C, that’s all.

The LM35 temperature sensor can be used in a couple of configurations. The basic one is the full positive temperature range (from 2°C up to +150°C). And the full range that can go below zero degrees (from -55°C up to +150°C). The basic configuration requires no external components besides the LM35 itself, and the full range configuration requires an additional resistor which can be calculated using the formula in the diagram below.

It’s a matter of configuring the ADC and triggering it to convert the corresponding channel and read the result and use the linear equation above to find out the temperature. However, we need to make our application code portable and configurable so it doesn’t call a specific channel or ADC instance, and that’s what I’ve done in the ECUAL/LM35 driver. Let’s see what’s inside of it, how it works, and how to configure and use it. And before that, if you need a solid and basic introduction to the LM35 sensor interfacing, you can check out This LM35 Interfacing Tutorial.

LM35 Temperature Sensor Interfacing With PIC Tutorial


   STM32 LM35 ECUAL Driver   

 

In this section, I’ll describe the LM35 driver components and how to configure and add it to your projects.

The ECUAL / LM35 Driver Files

The LM35 driver consists of the following files:

  • LM35.h
  • LM35.c
  • LM35_cfg.h
  • LM35_cfg.c
STM32 LM35 Temperature Sensor Example Code Project

It can be found in the course’s repo on GitHub.

You’ll need only to modify the configuration files. The source code for this driver is found in (LM35.c) and to use it you’ll include the header (LM35.h). Therefore, I’ll write here the code listing for the LM35_cfg.c file.

LM_cfg.c File

The SENSORS_NUM is defined to 1, so the configuration initialization above creates an instance of the LM35_cfg that is connected to an analog input pin on GPIOA, associated with ADC1 peripheral, and the channel number is 7. That’s the instantiation statement in order to configure your sensor driver code.

The ECUAL / LM35 Driver Configurations

You can configure the following parameters with this driver version:

SENSORS_NUM can be defined as 1, 2, 3, or whatever number you need. This represents how many LM35 sensors are there in your system, in case you’re using multiple sensors. The default value is 1.

The GPIOx port represents the GPIO port that the analog input pin belongs to. You have the freedom to hook the sensor to any analog pin on your microcontroller.

ADC_Instance you can assign ADC1, ADC2, or whatever ADCx peripheral is dedicated to the channel you’ve chosen to hook your sensor to.

Lastly, the ADC_Channel_Num is the ADC analog input pin channel number.

The ECUAL / LM35 Driver APIs For Application

The application layer will include the “LM35.h” header file of the driver. And therefore, it can access the available APIs. There are basically 2 functions in the source code of this driver as mentioned down below.

Obviously, the init function initialized the LM35 sensor line. It takes the number of the sensor as a parameter. If you’re using only one LM35 sensor, then pass 0 to this function. Otherwise, you’ll need to pass the index of the addressed sensor instance.

The LM35_Read function does a lot of things actually. It selects the analog sensor line from the ADC configurations and triggers an A/D conversion and takes the ADC result, converts it to temperature as a floating-point number, and returns the result at the end. The input parameter is the number of the LM35 sensor that you want to read, just in case you’re using multiple sensors in the system.

The ECUAL / LM35 Driver Instantiation

In order to instantiate an LM35 sensor line, you’ll have to open the LM35_cfg.c file and add your configurations. Let’s assume I’ve got 1 LM35 sensor and I want to hook it to the pin A5 which is associated with ADC1 peripheral. Here is what should be written in the cfg.c file.

Another example: let’s assume my system does have 2 LM35 sensors, the first of which is going to be connected to analog channel A2, and the other one to channel A6, both are associated with ADC1. Here is what should be written in the cfg.c file.

How To Add The ECUAL / LM35 Driver To Your Project

Please, refer to This Tutorial (How To Add ECUAL Drivers To Your STM32 Projects) in which I’ve shown the exact step-by-step procedure in order to add any ECUAL driver to your project. The exact same steps are still the same, however, that tutorial shows how to add the LCD driver but it’s the same steps anyway. And in today’s LAB, we’ll need to add that LCD driver as well, so it’s highly recommended.

Adding ECUAL Driver To STM32 Project


   STM32 Light Sensor Project – LAB21   

 

LAB Number 21
LAB Title STM32 LM35 Temperature Sensor Interfacing + LCD
  • Set up a new project as usual with system clock @ 72MHz
  • Set up An Analog Input Pin (Channel 7) In single Conversion Mode (The LDR Pin)
  • Set up timer2 in PWM mode with output on channel 1 (The LED Pin) 

Here is The Connection Diagram For This LAB

STM32 LM35 Temperature Sensor Example Project Code Embedded C

It Can Also Go This Way & Implement It Without An LCD But Use Serial Port To Print The Result

STM32 LM35 Temperature Sensor Example Project Code

 


   STM32 LM35 Example   

 

In this LAB project, we’ll initialize the LM35 sensor, the LCD display, and continuously read the sensor and update the display with the temperature value. The application firmware itself is really easy once you’ve included the necessary drivers and configured everything as it should be.

 

And now, let’s build this system step-by-step

 

Step1: Open CubeMX & Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

Step3: Set The RCC External Clock Source

Step4: Go To The Clock Configuration

Step5: Set The System Clock To Be 72MHz

The ADC peripherals will be assigned a default clock of 12MHz that you can optionally increase to a maximum of 14MHz. But we won’t do that in this example.

Step6: Enable The Analog Channel To Be Used (AN7)

This step seems to be redundant as the LM35 driver actually configures the ADC module and enables that channel which overrides any configurations done here in CubeMX. However, this step is mandatory as it makes CubeMX generate the ADC_HAL files which are being used by the driver itself. So you need at least to enable one channel just to have those files generated and included in the project directory.

STM32 LM35 Temperature Sensor Example Code Project Blue Pill STM32F103

Step7: Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using

Step8: Add The ECUAL LCD, LM35 Drivers As Well As The util files

The exact steps to add the drivers are illustrated in This Tutorial as mentioned before.

The util folder includes some utility files for software delay functions and stuff like that.

 

Here is The Application Code For This LAB (main.c)

Download The STM32 LM35 Temperature Sensor Project LAB21

 

Note That: the floating-point numbers formatting may not be enabled in the IDE tools by default. Therefore, you’ll need to enable that manually in order to be able to use the sprintf() function to print formatted float variables. Just do the following

Open project properties by right-clicking on its name in the IDE project files navigator > C/C++ Build > Settings > Tool Settings > check the option “use float with printf”. That’s all!

 

The LAB Connections

LM35 Temperature Sensor Connection

STM32 LM35 Temperature Sensor Example Project C Code

LCD Connection

STM32 LCD Library Example LCD 16x2 Code With STM32 LCD Tutorial

 

The Result For LAB Testing (video)

 


   STM32 LM35 Driver Multi-Sensor Support   

 

As I’ve mentioned earlier that this ECUAL LM35 driver can be used to instantiate multiple LM35 sensors with different channels using the configurations files. For example, here is a typical configuration structure for a project that will have 2 LM35 sensors working independently.

LM35_cfg.c

And here is a typical application example for initializing and reading both sensors.

main.c

 


   STM32 LM35 Driver Sampling Time   

 

One final test that I’ve also done was to configure the LM35 sensor in normal settings and set the system clock of the Blue Pill (STM32F103C8) @ 72MHz, the ADC @ 12MHz, and using a free GPIO pin. I set the pin HIGH before calling the LM35_Read function and driver the pin LOW after the function return. So as to measure the sampling time for the LM35_Read function. And it was found to be 16µs as you can see on the DSO screen. I can use it safely in any OS-based applications hereafter, so it’s ok any may not seek to optimize it more at least in the near future.

 


 

Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!

 

Previous Tutorial Previous Tutorial Tutorial 26 Next Tutorial Next Tutorial

 

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment