TMP36 controlling a Relay/Fan

Hello, my first post. I've been searching for answers and just cant figure it out. I think my questions may be just so simple no one bothers to write about it but I'm very new to this code thing.

I'm trying to get a TMP36 to turn on a relay at a certain temperature.

I'm Using a Arduino Leonardo with a standard 4 channel relay module powered separately. I have the following pins wired.

TMP36 PIN1 to Arduino PIN 3.3V with a jumper to AREF
TMP36 PIN2 to Arduino PIN A1
TMP36 PIN3 to Arduino PIN GND
Relay IN1 to Arduino PIN6

The first part of the sketch works fine and I can get he info from the serial monitor. I can also power the cycle the relay using a simple ON/OFF sketch so i'm pretty sure its wired up right I just cant figure out how t use the temp sensor info to control the relay. Any help would be greatly appreciated.

#define aref_voltage 3.3

int tempPin = 1;        
int tempReading;
int Relay1 = 6;

void setup(void) {
 Serial.begin(9600);   
 analogReference(EXTERNAL);
 pinMode(Relay1,OUTPUT);
}

void loop(void) {
 tempReading = analogRead(tempPin);  
 Serial.print("Temp reading = ");
 Serial.print(tempReading);  
 float voltage = tempReading * aref_voltage;
 voltage /= 1024.0; 
 Serial.print(" - ");
 Serial.print(voltage); Serial.println(" volts");
 float temperatureC = (voltage - 0.5) * 100 ;
 Serial.print(temperatureC); Serial.println(" degrees C");
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
 Serial.print(temperatureF); Serial.println(" degrees F");
 delay(1000);
}

// Part Below Not Working

void loop(){
 if (" degrees F" > 80)
 digitalWrite(Relay1, LOW);
 else
 digitalWrite(Relay1, HIGH);
 delay(500);            
}

if (" degrees F" > 80)Is not correct C language

You should read about the if syntax and about variables... starting with the basics is the best way to progress smoothly

I figured it wasn't correct I'm just not sure how to get what I want so I left the incorrect line to get my idea across. I can't figure out how to get condition to represent the temperature output in F.

I didn't realize this reference section existed, there is some good info there (I guess that why you linked me there), thanks! I've read if/else, if, loop() as well as a few others.

I tried to rewrite it to just read voltage from the pin but I still get a compiling error. I can't seam to see what I'm doing wrong. Probably really simple but I just don't know.

Here's what I tried (for the bottom part)

void loop()
{
  if (digitalRead(tempPin) > .5)
  digitalWrite(Relay1, LOW);
  else
  digitalWrite(Relay1, HIGH);
  delay(500);            
}

I know you probably don't need the error but here it is anyway :slight_smile:
Temp-Sensor.ino: In function 'void loop()':
Temp-Sensor.ino:31: error: redefinition of 'void loop()'
Temp-Sensor.ino:14: error: 'void loop()' previously defined here

Temp-Sensor.ino:31: error: redefinition of 'void loop()'

The problem is in the code you didn’t show. You have two loop functions. You’re only allowed one. Be aware that if you have multiple tabs open in the IDE it will combine them all together before it compiles, so the other loop may be on another tab if you have several open.

int tempPin = 1;
Serial.begin(9600);

Since the Serial interface uses pins 0 and 1, you need to stay off those pins if you are using Serial.

Sorry, it's analogRead in that code. So you're safe. But I would use A1 instead of just 1 to make it clear to others reading the code. The second code was digitalRead so I assumed wrongly that the first code did too.

Hi,
You cannot have two void loop().
Combine them then change.

 if (" degrees F" > 80)
 digitalWrite(Relay1, LOW);
 else
 digitalWrite(Relay1, HIGH);
 delay(500);

To

 if (temperatureF > 80)
{
 digitalWrite(Relay1, LOW);
}
 else
{
 digitalWrite(Relay1, HIGH);
}
 delay(500);

Tom... :slight_smile:

Delta_G:
You have two loop functions. You're only allowed one

Yup, didn't know that... I knew it was going to be something dumb I was doing.

Delta_G:
Sorry, it's analogRead in that code. So you're safe. But I would use A1 instead of just 1 to make it clear to others reading the code. The second code was digitalRead so I assumed wrongly that the first code did too.

That was my fault the second one should have been analogRead as well, I was still getting the error from having two loop function and hadn't even gotten that far. I'm still learning how to interpret the errors. Thanks for the help!

can I define int tempPin = A1; instead of int tempPin = 1; and have it work the same?

TomGeorge:
Hi,
You cannot have two void loop().
Combine them then change.
Tom... :slight_smile:

Yup, the two void loops thing was by biggest problem, it actually makes a lot of sence now that I think about it, still learning, thanks!!!

The if (temperatureF > 80) worked perfect I thought it was going to be either that or the (" degrees F") but I wasn't sure... slowly, very slowing its coming into focus for me :slight_smile: thanks for the help!

After another few minutes trouble shooting only to realize I forgot the ; at the end of delay(500) everything works great! Thanks everyone!!

Here the code (that is working for me) in case it helps someone else

#define aref_voltage 3.3

int tempPin = 1;        
int tempReading;
int Relay1 = 6;
 
void setup(void) {
  Serial.begin(9600);   
  analogReference(EXTERNAL);
  pinMode(Relay1,OUTPUT);
}
 
void loop() 
{
  tempReading = analogRead(tempPin);  
  Serial.print("Temp reading = ");
  Serial.print(tempReading);  
  float voltage = tempReading * aref_voltage;
  voltage /= 1024.0; 
  Serial.print(" - ");
  Serial.print(voltage); Serial.println(" volts");
  float temperatureC = (voltage - 0.5) * 100 ;
  Serial.print(temperatureC); Serial.println(" degrees C");
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  Serial.print(temperatureF); Serial.println(" degrees F");
  delay(1000);
  if (temperatureF > 80)
  {
    digitalWrite(Relay1, LOW);
  }
  else
  {
    digitalWrite(Relay1, HIGH);
  }
  delay(500);
}

Also open to suggestions and/or criticism on this code as I'm learning and will take whatever advice people want to offer.

Yes A1 would work the same as 1 in an analogRead but avoids any confusion about exactly which pin you mean. It's not an error to use the 1, just better practice to use A1 to distinguish between digital pin 1 and analog input 1.

Delta_G:
Yes A1 would work the same as 1 in an analogRead but avoids any confusion about exactly which pin you mean. It's not an error to use the 1, just better practice to use A1 to distinguish between digital pin 1 and analog input 1.

Done! I like it better, thank you.

Another question... if I wanted to also have the fan (relay1) come one for a bit every so often could I add the code below on the end or would I need to incorporate it into the if/else statement?

 digitalWrite(Relay1,LOW);
 delay(50000);
 digitalWrite(Relay1,HIGH);
 delay(1800000);

If you add that code to the end, then during that very long delay it won't be checking the temperature anymore. It will just be sitting there executing that delay call. If you want it to remain responsive to temperature then you'll have to get rid of the delay and go with the Blink Without Delay style.

Delta_G:
If you add that code to the end, then during that very long delay it won't be checking the temperature anymore. It will just be sitting there executing that delay call. If you want it to remain responsive to temperature then you'll have to get rid of the delay and go with the Blink Without Delay style.

Yup, I wouldn't have even though about that delay like that. That would make the temp sensor pretty much do nothing since it would run the fan for 50 seconds than wait for 30 minutes than quickly run the temp check and back to running the fan and delay again... interesting.

I've started looking into BlinkWithoutDelay function and it looks pretty interesting... a little overwhelming but I'll tinker with it for a bit and I'm sure it will start to make sense. I'll report back if/when I need hep :slight_smile: thanks!

Michael_Schofield:
I've started looking into BlinkWithDelay function

It's not a function. It's not even a recipe. It's a way of thinking. Once you embrace it, you'll find a thousand different ways to write it.

Delta_G:
It's not a function. It's not even a recipe. It's a way of thinking. Once you embrace it, you'll find a thousand different ways to write it.

Just wasn't sure what to refer to it as... found some info here Pretty awesome, I already ran one test sketch... it didn't go horribly wrong but not exactly right either, I'll keep working on it :smiley:

Here's a better one.

https://www.gammon.com.au/blink

Actually, check out h is whole site.