LM35 not activating switch on 5v Relay

Pulling my hair out a little bit on this, but I have a 5v relay switched by an LM35 voltage sensor, and it gives somewhat sketchy temp readings, but not too far off, but will only light the Relay on LED a bit, and not switch the switch on.

This is set to turn on an AC powered fan plugged into an outlet wired to the relay.

This seems super simple, and a simple on-off program runs the relay and fan perfectly.

The addition of the LM35 and appropriate code, just lights the LED a bit, not fully, it appears as if voltage is dropping.

Any assistance would be appreciated. I’m stumped, other than a few threads mentioning powering the relay itself, which this particular model, I’m not sure how that would be done.

Running this code:

//the output pin of the lm35 sensor
int lm35Pin = A1;

void setup() {
  // set up serial at 9600 baud
  Serial.begin(9600);
}

void loop() {
  int analogValue;
  float temperature;
  // read our temp sensor
  analogValue = analogRead(lm35Pin);

  // convert the 10bit analog value to celcius
  temperature = float(analogValue) / 1023;
  temperature = temperature * 500;

  // print the temp over serial
  Serial.print("Temp: ");
  Serial.print(temperature);
  Serial.println("C");
  Serial.print(analogValue);

  if(temperature > 31)
  digitalWrite(9,HIGH);

  else if(temperature < 31)
  digitalWrite(9,LOW);

  // wait 1s before reading the temperature again
  delay(2000);
  }

It might help if you set the relay pin to OUTPUT in setup().

pinMode(9, OUTPUT);

Leo..

I assume you use an Uno.
Using the internal 1.1volt Aref could make temp readout more stable.
Try this modified (untested) sketch.
Leo..

float tempC;

void setup() {
  analogReference(INTERNAL); // use internal 1.1volt Aref
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  tempC = analogRead(A0) * 0.1039; // calibrate by changing 0.1039

  if (tempC > 31) digitalWrite(9, HIGH);
  else digitalWrite(9, LOW);

  Serial.print("Temp: ");
  Serial.print(tempC, 1); // one decimal place
  Serial.println("C");
  delay(2000);
}

Wawa:
tempC = analogRead(A0) * 0.1039; // calibrate by changing 0.1039

I would like to write the above code as follows:

tempC = (float)100*(1.1/1024)*analogRead(A0);// tempC = (float)0.1074*analogRead(A0);

OR

tempC = (float)100*(1.1/1023)*analogRead(A0);// tempC = (float)0.1075*analogRead(A0);

Wouldd appreciate to know the source of the figure 0.1039?

GolamMostafa:
Wouldd appreciate to know the source of the figure 0.1039?

Exactly what you did, but compressed to a single number.
And calculated with the Aref voltage of my Arduino.

Every Arduino has a slightly different (but stable) 1.1volt Aref, that can be between 1.0 and 1.2 volt.
Mine was about 1.064volt.
Leo..

You can measure it with a voltmeter between Aref and Gnd when internal reference is activated.

Wawa:
Exactly what you did, but compressed to a single number.
And calculated with the Aref voltage of my Arduino.

Every Arduino has a slightly different (but stable) 1.1volt Aref, that can be between 1.0 and 1.2 volt.
Mine was about 1.064volt.
Leo..

So, tempC comes as:

= 100*(1.064/1024)*analogRead(A0);
//1.064 is the actual measured value at AREF-pin for the INTERNAL option of analogReference(arg)

= 0.1039*analogRead(A0);

Excellent!

To Leo,

Big miss in not setting that to output. It was what was wrong as I'm sure you knew.

Thanks, and I was already implementing the aref code you posted from another similar question. Thanks for that as well. I've been out of Arduino for a bit and needed to get this put together quickly as we have 6 week old Ducks that need some air flow in their coop during our, now record setting, heat wave. (109 in Austin today.)

Shawn

if (tempC > 31) digitalWrite(9, HIGH);
else digitalWrite(9, LOW);

Could add some hysteresis there, to stop the fan from turning on/off too fast.

if (tempC > 31) digitalWrite(9, HIGH);
if (tempC < 28) digitalWrite(9, LOW);

Wish I was there for a few days.
Winter, rain, the flu, and ~8C (46F) here.
Leo…