How Arduino Uno setup for reading a TMP36 temperature sensor?

ampheoampheo
2 min read

here's a complete Arduino Uno setup for reading a TMP36 temperature sensor using both hardware and software filtering.


Hardware Connections (with RC filter)

TMP36 PinConnection
VCC+5V from Arduino
GNDGND
VOUT→ 1kΩ → A0
0.1µF
GND

This creates a 1kΩ + 0.1µF RC low-pass filter on the analog line.


Arduino Code (with EMA software filter)

cpp

const int sensorPin = A0;
float alpha = 0.1;            // Smoothing factor
float filteredTemp = 0;

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

void loop() {
  int raw = analogRead(sensorPin);

  // Convert ADC value (0–1023) to voltage (0–5V)
  float voltage = raw * (5.0 / 1023.0);

  // Convert voltage to temperature in Celsius (TMP36 output: 0.5V at 25°C)
  float tempC = (voltage - 0.5) * 100.0;

  // Apply EMA filter
  filteredTemp = alpha * tempC + (1 - alpha) * filteredTemp;

  // Print raw and filtered values
  Serial.print("Raw Temp: ");
  Serial.print(tempC);
  Serial.print(" °C | Filtered Temp: ");
  Serial.print(filteredTemp);
  Serial.println(" °C");

  delay(200);  // Sample every 200ms (5Hz)
}

Explanation

  • analogRead() reads raw ADC value from the TMP36.

  • TMP36 output = 0.5V at 25°C, scale is 10mV/°C.

  • The Exponential Moving Average (EMA) smooths short-term fluctuations.

  • Hardware RC filter attenuates high-frequency noise before ADC.


Optional: Moving Average Instead

If you prefer moving average:

cpp

#define N 10
float readings[N];
int index = 0;

float moving_average(float new_value) {
  readings[index] = new_value;
  index = (index + 1) % N;
  float sum = 0;
  for (int i = 0; i < N; i++) sum += readings[i];
  return sum / N;
}

Replace filteredTemp = alpha * tempC + ... with:

cpp

filteredTemp = moving_average(tempC);
0
Subscribe to my newsletter

Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ampheo
ampheo