Header Ads

LM35 temperature sensor | Arduino Code | Prefect stable reading | Random reading troubleshooting

LM35 temperature sensor | Arduino Code | Prefect stable reading | Random reading troubleshooting




Temp sensing and toggling the output on threshold
This logic is prefect for overdrive the jitter output






After I connected the three jumper wires from the LM35 on the breadboard to the 5V, A5 and GND pins on the Arduino, and uploaded the code to display a temperature every second to the serial console, the numbers alternated between zeroes and random numbers. The number I expected to see was 28°C degrees, the ambient temperature for my home office.

After reading numerous comments that the circuit doesn’t work and plenty of bad advice on how to fix it, I came across a comment that pinpointed the real problem: all those how-to articles were nothing more than copy-and-paste click bait for the websites.

The LM35DZ comes in a TO-92 package like a transistor. A plastic cylinder body with one side flat and three pins poking out from the bottom. It’s quite easy to connect power and ground backwards. The best way to remember pinout is to look at the flat side of the LM35: left pin is power, center pin is output, and right pin is ground.


The LM35 supposed to be a remote sensor at the end of a coaxial cable (one wire with grounded shielding). One way to stop the cable from acting like an antenna that interferes with the sensor is to add a resistor-capacitor (R-C) damper between out and ground. A bypass capacitor across power and ground eliminates unwanted electromagnetic interference (EMI).

Adding those three components fixed the LM35 as a temperature sensor for the Arduino.

After looking at so many how-to articles, I settled on “Reading the Room Temperature with an LM35 Sensor” by restating as the basis for my own project. It clearly explains how the LM35 works, uses the Arduino's internal voltage reference for better accuracy, and the copy-and-paste code works without modification with the revised circuit.

The LM35 in the revised circuit takes several minutes for the numbers to settle down and then consistently report the room temperature in the serial console.



Code:


/*
   Temp sensing and toggling the output on threshold

   This logic is prefect for overdrive the jitter output


   Author: arshad.pathan@gmail.com

*/


#define TEMP_SNS_PIN 0
#define TEMP_THRESHOLD 40
#define TEMP_COOL_THRESHOLD 38

#define TIMER_LED 9
#define TEMP_LED 10

volatile float tempC = 0;
volatile int reading = 0;
String mode = "";

volatile boolean temp_overshoot = false;



void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  //for temp sensing
  analogReference(INTERNAL);


  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TIMER_LED, OUTPUT);
  pinMode(TEMP_LED, OUTPUT);

  for (int i = 0; i < 10; i++) {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(TIMER_LED, HIGH);
    digitalWrite(TEMP_LED, HIGH);
    delay(100);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(TIMER_LED, LOW);
    digitalWrite(TEMP_LED, LOW);
    delay(100);
  }

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(TIMER_LED, HIGH);
  digitalWrite(TEMP_LED, HIGH);
}


void loop() {
  read_temp();

  if (temp_overshoot == false) {
    if (tempC > TEMP_THRESHOLD) {
      temp_overshoot = true;
    }
  }

  if (temp_overshoot) {
    if (tempC < TEMP_COOL_THRESHOLD) {
      temp_overshoot = false;
    } else {
      mode = "RED";
    }
  } else {
    mode = "GREEN";
  }

  Serial.print("sensor = ");
  Serial.print(tempC);
  Serial.print("\t mode = ");
  Serial.print(mode);

  Serial.println();
  delay(20);
}


/*s************************************************
   Read Temp
 *************************************************/
void read_temp() {
  reading = analogRead(TEMP_SNS_PIN);
  tempC = reading / 9.31;
}

No comments:

(C) Arshad Pathan. [Do not copy any contain without permission]. Powered by Blogger.