0

So After trying several sketches for a LM35 and none working I finally found one that was giving me accurate results. The problem is the code is way over my head. I was able to modify it enough to get the serial and temperature to a LCD but I cant figure out why its not tripping the relay after set values. The sketch completely ignores the temperature value therefor will never set the relay HIGH. What am I missing?

/*
 LM35 thermometer, no floats, no delays

  http://www.ti.com/lit/gpn/lm35
*/
const int relay = 0;
const byte sampleBin = 8, // number of samples for smoothing
           aInPin = A5;
const int fudge = 170; // adjust for calibration
const int kAref = 1090, // analog ref voltage * 1000
          kSampleBin = sampleBin * 1000,
          tEnd = 5000; // update time in mS
int tempC,
    tempF;
uint32_t total,  // sum of samples
         tStart; // timer start
int sensorPin = 5;// The analog pin the LM35's Vout is connected to.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

/*
Initialize serial connection with the computer*/
void setup()
{
  pinMode(relay, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16,2);
  analogReference(INTERNAL); // use 1.1V internal ref
  analogRead(aInPin);
  for (int i = 0; i < sampleBin; i++) // for smoothing, fill total
    total += analogRead(aInPin);   // with sampleBin * current
                                   // reading
}

void loop()
{
  if (millis() - tStart > tEnd)
  {
    tStart = millis(); // reset timer
    total -= (total / sampleBin); // make room for new reading
    total += analogRead(aInPin); // add new reading
    tempC = total * kAref / (kSampleBin + fudge);
    tempF = (tempC * 18 + 3200) / 10;
    Serial.print("Temperature- ");
    prntTemp(tempF);
    Serial.println();
  }
}

void prntTemp(int temp)
{
  Serial.print(temp / 10); // whole degrees
  Serial.print(".");
  Serial.print(temp % 10); // tenths
  Serial.print("\t");

  // display Temperature on LCD
  lcd.setCursor(0,0);
  lcd.print("Temperature-");
  lcd.setCursor(0,1);
  lcd.print(tempF);
  lcd.println(" degrees F ");
  delay(10);// print data every 10milliseconds

  if (tempF >= 78.0)
  {
    digitalWrite(relay, HIGH);
  } 
}
  • Does the LCD show the expected temperature? -- Are you sure that your relay is connected to Pin 0? Did you check this with another simple sketch? -- tStart is initialized with 0, do you want this? – the busybee Jan 30 at 10:48
  • Not related to your problem: The delay(10); in prntTemp() is not necessary, because the latter is called only every 5 seconds. -- Not related to your problem that relay is not set HIGH, but will be a problem if that will work: You need an else branch that resets the relay to LOW. -- And, again not related to your problem: You don't need to access tempF in prntTemp() as it is provided as the argument in parameter temp. – the busybee Jan 30 at 10:48
  • Please show us the serial output – Sim Son Jan 30 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.