Author Topic: Need Help With Simple Temperature controller  (Read 6816 times)

NO2nice

  • Newbie
  • *
  • Posts: 10
Need Help With Simple Temperature controller
« on: April 24, 2018, 07:11:05 pm »
I have spent way too much time trying to figure this out and before throwing it out I hope one of you people will be able to tell me what I am doing wrong. Thanks in Advance.
I want to use the Digispark Attiny85 to read a TMP36 and based on the temperature, light up a corresponding LED and trigger a relay based on the code below and the schematic attached.  I tried using the else-if statement to see if i could clean up the code but nothing works.

Please Help!


int rawvoltage = 0; 


void setup()
{
 
  pinMode(2, INPUT); //Analog input 1 for TMP36
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  //Serial.begin(9600);
}

void loop()
{
  int rawvoltage = analogRead(1); //Read P2
   
float voltage = (rawvoltage*5.0/1024); //convert rawvoltage to voltage

  float celsius = (voltage-.500)*10;

   float Farenheit = (celsius*9/5)+32;
 
  if (Farenheit > 86)
  {
    digitalWrite(3,HIGH);
    digitalWrite(0,HIGH);
    delay(1000);
  }
/*
else
{   
    digitalWrite(3,LOW);
    digitalWrite(0,LOW);
    delay(1000);
}
*/

else if (Farenheit <= 85 && Farenheit >=68)
  {
    digitalWrite(4,HIGH);
    digitalWrite(0,LOW);
    delay(2000);
  }
/*
else
{
    digitalWrite(4,LOW);
    digitalWrite(0,HIGH);
    delay(1000);
}
*/

else if (Farenheit < 68)
   {
    digitalWrite(1,HIGH);
    digitalWrite(0,LOW);
    delay(1000);
  }
[/img][/img][/img]
}


apt403

  • Newbie
  • *
  • Posts: 4
Re: Need Help With Simple Temperature controller
« Reply #1 on: April 25, 2018, 12:10:44 pm »
I think your problem is here:

Code: [Select]
float celsius = (voltage-.500)*10;

Change it to

Code: [Select]
float celsius = (voltage-.500)*100;

Per the datasheet, the TMP36 outputs .10mV/°C with a 500mV offset - For example: at 25°C, the output is 750mV (.75 Volts). To turn that into °C, you need to subtract .5v, then multiple the result by 100:

.75 - .5 = .25

.25 * 100 = 25

And your °F variable is defined in relation to the °C variable, so that's going to be off by a factor of 10, as well.

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #2 on: April 25, 2018, 12:36:44 pm »
Thanks for the response, I did try that and put it back to 10 after running the code on my arduino board. 
My understanding (or lack of it) is that the rawvoltage is stored in mV so at 25°C it is 614mV which you have to multiply by 5 and divide by 1024 to get the real voltage of 3V which is then offset by 0.5V to get 2.5V and then multiplied by 10 to get 25°C.

does my circuit look OK? I cannot figure this out.
« Last Edit: April 25, 2018, 12:46:11 pm by NO2nice »

apt403

  • Newbie
  • *
  • Posts: 4
Re: Need Help With Simple Temperature controller
« Reply #3 on: April 25, 2018, 01:35:09 pm »
The circuit looks okay - the datasheet calls for a .1uF cap on Vin, but in practice I haven't had a problem ignoring that.

You've verified that the Farenheit and Celcius values are reasonable through the serial monitor or something similar?

How is the program behaving right now?

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #4 on: April 25, 2018, 06:30:20 pm »
I had the program print out the temperatures with the serial monitor on the arduino board but i didn't know that the digispark could do the same.  right now I check the voltage at the TMP36 output to ground and it seems to fluctuate correctly according to the temperature.

right now when i power it up the Green LED lights up for 5 seconds then it turns off and the red LED turns on along with the green LED on the digispark board. I'll have to figure out how to use the serial monitor on the digispark attiny85.

thanks for the help

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #5 on: April 27, 2018, 05:34:04 am »
I figured out how to get the serial monitor working and i changed the voltage variable into a float which helped but the "if..else" statements did not work.  The TMP36 output correctly and it was read by the board correctly and printed out correctly on the serial monitor but for some reason Pin 3 was always HIGH and then pin 4 was dim.  So after another 2 hours of beating a dead horse I am surrendering and throwing the Digispark out.  I created this circuit on my arduino uno in 15 minutes and it worked and now i think i spent 10 hours cumulative on this digispark with nothing to show for it.  very frustrating.


apt403

  • Newbie
  • *
  • Posts: 4
Re: Need Help With Simple Temperature controller
« Reply #6 on: April 27, 2018, 09:57:14 am »
Sorry this is causing you so much grief - Just had a thought - Are you running this circuit w/ it plugged into USB? Pins 3 and 4 are the USB+ and USB- data lines. Not sure that would cause the problem, but it is an intriguing coincidence that you're getting constant HIGH on the USB+ line, and constant LOW on the USB- line.

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #7 on: April 27, 2018, 08:27:35 pm »
thanks for trying to help out with this one. I wouldn't waste so much time on something so simple but because its so simple its aggravating!  I tried running it with USB or on separate powersupply, same thing.
i tried the .1uf capacitor on the TMP36, same result.

so i'm really at a loss with this one.

dl4ou

  • Newbie
  • *
  • Posts: 15
Re: Need Help With Simple Temperature controller
« Reply #8 on: April 28, 2018, 06:53:35 am »
The power supply looks not good.
GND seemed to be ok.
The Digispark ist powered from USB-Port.
The relay is powered from 5V external power supply.
The TMP36 is powered from Digispark-Vin. Thats not ok. You should connect TPM36-PIN 1 to Digispark 5V.
Please refer to the Digispark circuit.

dl4ou

  • Newbie
  • *
  • Posts: 15
Re: Need Help With Simple Temperature controller
« Reply #9 on: April 28, 2018, 06:57:26 am »
It would be also a good idea to have a flyback diode on the relay.

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #10 on: April 29, 2018, 06:03:15 am »
Hi  dl4ou - thanks for the comments but I'm not sure what you mean

The power supply looks not good. - I am using a regulated 5V DC supply for +5V.
GND seemed to be ok.

The Digispark ist powered from USB-Port. -The Digispark must be powered by the USB? I was powering it with GND and Vin.

The relay is powered from 5V external power supply. I am powering the relay with the same regulated power supply off of the breadboard.

The TMP36 is powered from Digispark-Vin. Thats not ok. You should connect TPM36-PIN 1 to Digispark 5V. -I connect the TMP36 pin 1 to the power supply +5V, Pin 2 is connected to P2 on Digispark and Pin3 is connected to the ground

Please refer to the Digispark circuit. -do you have a link to the circuit?

also the relay is an isolated 5V relay made to work with the arduino

thanks again

dl4ou

  • Newbie
  • *
  • Posts: 15
Re: Need Help With Simple Temperature controller
« Reply #11 on: April 29, 2018, 12:24:33 pm »
Your schematic shows only a connection between power supply 5V and Relay. I can't see the connection between power supply 5V and Vin (there is no connection point). Therefore I assumed that the Digispark ist powered via USB.

Between Vin and Digispark 5V is a voltage difference caused by the regulator of the digispark.
Therefore the TMP36 and the attiny have different operation voltage.
I prefer to power the Digispark at 5V and GND.

I use a schematic from google "digispark schematic".

A relay without flyback diode could damage the Digispark.

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #12 on: May 01, 2018, 05:48:58 am »
thank you very much for the help! its working and like I thought, it was a simple error that I could not see.

I am now using a 12V power supply to power the digispark and then using the 5V regulator to power the TMP36.  see attached photo. I will output the relay on P1.

thank you again!

MrCookie

  • Newbie
  • *
  • Posts: 21
Re: Need Help With Simple Temperature controller
« Reply #13 on: May 09, 2018, 11:31:35 am »
Why are you declaring rawvoltage every time the program runs? That doesn't make sense, you only need to declare the variable once, but you can assign it multiple times at the start of the loop. Also, I dont understand why you need two floats when all you want is the temperature in Fahrenheit.

NO2nice

  • Newbie
  • *
  • Posts: 10
Re: Need Help With Simple Temperature controller
« Reply #14 on: May 09, 2018, 01:30:25 pm »
honestly, I just copied the code from the arduino website and thats what they showed.  I dont need the Celsius float for what I am doing. I left it in there and just didnt delete it.

This is how i got it to work. My question is why to I have to assign the other pins to LOW when I only say to turn one HIGH?  example: temp >75 so I want Green LED on and all other LEDs and relay Off. If i dont assign a LOW value the other Leds stay on.



void setup()
{

  pinMode(2, INPUT); //Analog input 1 for TMP36
  pinMode(0, OUTPUT);//Red LED
  pinMode(1, OUTPUT);//Relay to turn on Heater
  pinMode(3, OUTPUT);//Green LED
  pinMode(4, OUTPUT);//Yellow LED

}
//int rawvoltage =0; //Take this out??
 
void loop()
{
int rawvoltage = analogRead(1); //Read P2
 
float voltage = (rawvoltage*5.0/1023); //convert rawvoltage to voltage

 float Farenheit = (((voltage-0.5)*100)*9/5)+32;
 
  if (Farenheit >= 75)
  {
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    delay(1000);
  }
  else if ((Farenheit) <75 && (Farenheit) > 68)
  {
    digitalWrite(4,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    delay(1000);
  }
  else if (Farenheit <= 68)
  {
    digitalWrite(0,HIGH);
    digitalWrite(1,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(3,LOW);
    delay(1000);
  }
}