Introduction: STM32F103 Nucleo: LM35 Temprature Sensor (using Mbed.h)

In this tutorial, we will learn to will learn to interface Temperature Sensor with STM32 Nucleo Board. We will be using most commonly used temperature sensor i.e. LM35. We will be taking input from the sensor and displaying it on a serial port. We will be using mbed.h. And this time we will build project scratch!

Components Required:

  1. STM32F103 Nucleo Board and a cable to connect it with PC.
  2. LM 35 Temperature Sensor
  3. 3x Female to Male Connecting Wire.

This project is divided into three steps:

  1. Setting up Temperature Sensor.
  2. Programming using mbed online compiler.
  3. Visualizing the output.

If you test this first, binary file is attached. Just make connections as shown in next step and download (or copy) this .bin file onto your nucleo board. To see how to see the ouput, jump to visualizing the output step.

Step 1: Setting Up Temperature Sensor

Pin out of LM35 temperature sensor is shown in figure[1]. LM35 DataSheet.

Make connection as mentioned below and as shown in figure[2]:

  1. Nucleo 5V -> LM35 Vcc
  2. Nucleo A0 -> LM35 Output
  3. Nucleo GND -> LM35 GND

Step 2: Programming and Compiling

Open mbed Compiler. Sign In. Click on New in the top left corner. Make sure that NUCLEO-F103RB is selected in Platform. From template, select Empty Program. Enter a program name. and click Ok.

Now, right click on that program and select new file. Enter file name main.cpp. Click OK. Now, again Click on that program and select Import Library-> From Import Wizard. The search for 'mbed'. After some time, a list will appear. drag the first library into your program (drop onto your program folder i.e. program name)

Now, Copy and paste the following code into your main.cpp file.

#include "mbed.h"                   //including mbed.h
AnalogIn analog_value(PA_0);        //Setting pin PA0 (A0) as Anlog Input
Serial pc(SERIAL_TX, SERIAL_RX);    //Create an object of Serial Class
int main() {
    float meas;
    float temp;
    while(1) {
	// Converts and read the analog input value (value from 0.0 to 1.0)
	meas = analog_value.read();
	//converts the analog value to eq. temprature.
        temp = ((meas * 5000)/10);
	//^ if you are getting high values(i.e.50~60)try subtracting 40 from temp
	//^^if you are getting VERY high value, lets say greter than 150,
	//LM35 isn't connected corectly or your LM35 isn't working properly  
	//printing the temp. onto Serial
        pc.printf("temprature = %.0f ^C\r\n", temp);
        wait(1); // 1000 ms Delay
    }
}

Code is explained in comments. Click on Compile and Download the binary file onto Nucleo Board. (or copy)

Step 3: Visualizing the Output

In order to visualize the output, you will need to install the a Software that can read from Serial Port. We will be using Putty, most widely used software. I have attached the .exe file. You can download it from here, or you can from elsewhere. Once downloaded, now run putty.exe. Select 'Serial'. Enter the COM port onto which your STM32 Nucleo is attached. click 'OPEN" or press Enter. Now, you should be able to see the temperature being printed on your putty screen.

NOTE:

  1. If you are getting NULL/NILL's, make sure that your baudRate to 9600.