Zed-King Institute

    Arduino LM35 Temperature Sensor with Alarm LED

    hardSensorsArduino (C++)

    The LM35 outputs 10 mV per °C; the Arduino reads that voltage and converts it into a temperature.

    The question

    An LM35 temperature sensor is on A2. Convert the reading to °C with temp = reading × 500.0 / 1024, print it at 9600 baud, and turn ON an LED on pin 7 when the temperature is above 30 °C (OFF otherwise).

    The sketch

    sketch.ino

    void setup() {
      Serial.begin(9600);
      pinMode(7, OUTPUT);
    }
    
    void loop() {
      int reading = analogRead(A2);
      float temp = reading * 500.0 / 1024;
      Serial.println(temp);
      if (temp > 30) {
        digitalWrite(7, HIGH);
      } else {
        digitalWrite(7, LOW);
      }
      delay(1000);
    }

    Wiring

    LM35: +Vs → 5V, GND → GND, Vout → A2. LED → 220Ω → pin 7.

    How it works

    1. 1analogRead() gives 0–1023 for 0–5 V.
    2. 2temp = reading × 500.0 / 1024 converts it to °C (5 V = 500 °C at 10 mV/°C).
    3. 3Print the temperature, then turn the LED on if it is above the limit.

    Common mistakes

    • Writing 500 / 1024 with whole numbers — the division gives 0. Use 500.0.
    • Connecting the LM35 back to front, which makes it heat up.

    Now solve a similar question yourself

    A new Arduino question every time, checked instantly.

    Practice questions

    More in Sensors