STM32 Blue Pill ADC tutorial | TMP36 Temperature Sensor Interfacing & Programming

This is electronics note on how to use the STM32 Blue Pill (STM32F103C8T6) ADC (Analog to Digital Converter) feature with example using TMP36 temperature sensor. It will be show how to interface and write program code for the STM32 Blue pill microcontroller board. Any other temperature sensors like LM35(see LM35 Temperature Sensor with Arduino and LM35 Temperature Sensor with Arduino: A Complete Guide & Project) can be used.

STM32 Blue Pill Temperature Sensor Interfacing

The circuit diagram below shows TMP36 temperature sensor connected to the STM32 Blue Pill microcontroller board. 

Blue Pill with TMP36 temperature sensor interfacing

The TMP36 sensor output pin is connected to the PA1 pin of the microcontroller. On the STM32F103C8T6 (Blue Pill), PA1 corresponds directly to ADC Channel 1 (ADC12_IN1).

Watch the following video demonstration of how the circuit works.

STM32 Blue Pill ADC Programming

The code below is written in Arduino C++ for the STM32F103C8T6 (Blue Pill). It is designed to run in the Arduino IDE using the official STMicroelectronics STM32 core and compiled via the ARM GNU Toolchain (arm-none-eabi-gcc).


const int tempPin = PA1;

void setup() {
  Serial.begin(9600);
  analogReadResolution(12);
}

void loop() {
  int rawADC = analogRead(tempPin);
  float voltage = (rawADC / 4095.0) * 5.0;

  // TMP36 formula: Temp (°C) = (Voltage - 0.5V) * 100
  float tempC = (voltage - 0.5) * 100.0;

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" °C");

  delay(100);
}

This Arduino code configures pin PA1 as an analog input connected to a TMP36 temperature sensor, setting up 9600-baud serial communication and explicitly enabling 12-bit ADC resolution ($0$ to $4095$) on an STM32 board inside setup(). In the main loop, it continuously reads the raw digital value from the sensor, scales it relative to a $5.0\text{V}$ reference voltage to calculate the actual input voltage, and then converts that voltage into Celsius using the standard TMP36 transfer function ($(V_{out} - 0.5\text{V}) \times 100$). Finally, it prints the calculated temperature to the Serial Monitor every 100 milliseconds.

Other SMT32 Blue Pill Tutorials:


Post a Comment

Previous Post Next Post