Help declaring a variable for temperature in thermistor--relay circuit

Hello!

I have spent the past few weeks reading a lot about relay control with arduino. I'm not an electrician by study so learning basic coding and circuit building has been a fun adventure.

My first "eureka!" moment was when I was able to take temperature measurements with a thermistor.

My second "eureka!" moment was when I was able to modify the "blink" example code to turn my relay on and off using a transistor, and just today after my diode arrived, turned an LED on when the relay was closed.

My ultimate goal is to replace the LED with a 120V 60W light bulb, which is going to be in close proximity to the thermistor, which I want to use to turn the lightbulb on and off.

This is my VERY SIMPLE code that I borrowed from the Arduino Thermistor page, and I just threw in some "if else" statements hoping it might make sense.

Basically, it's saying "Temp" is not declared in the if/else statements, and I'm not sure wether to replace Temp with AnalogRead0 or what...

I'd like the Analog0 input (after it has been converted to Celsius) to control the relay ON/OFF switch so to speak. I've also put a fritzing schematic at the bottom if that helps, thank you for any help!!

#include <math.h>
int RelayHOT = 4;
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}

void setup() {
Serial.begin(115200); // begin the serial monitor
pinMode(RelayHOT, OUTPUT); //set pin 4 (known as relayHOT - to an output)
}

void loop() {
Serial.println(int(Thermister(analogRead(0)))); // print Celcius temp reading in serial monitor
delay(5000); // wait 5 seconds before sampling temperature again

if (Temp < 28) digitalWrite(RelayHOT, HIGH); //if the temperature is less than 28C, turn on the relay which will turn on the light and increase temperature
else if (Temp > 28) digitalWrite(RelayHOT, LOW); //if the temperature is greater than 28C, turn the bulb off because it is hot enough

}

Take this line:-

double Temp;

and place it just after this line:-

#include <math.h>

This will make Temp a global variable accessible to all functions.

When posting code use the # icon not the quote one.

fixed?

#include <math.h>

int RelayHOT = 4;

double Thermister(int RawADC)
{
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
 return Temp;
}

void setup() 
{
 Serial.begin(115200);  
 pinMode(RelayHOT, OUTPUT);  
}

void loop() 
{
  float Temp = Thermister(analogRead(0));
  Serial.println(Temp,2);                      // print Celcius temp  with 2 decimals reading in serial monitor
 
  if (Temp < 27.5) digitalWrite(RelayHOT, HIGH);       //if the temperature is less than 27.5 C, turn on the relay which will turn on the light and increase temperature
  else if (Temp > 28.5) digitalWrite(RelayHOT, LOW);  //if the temperature is greater than 28.5 C, turn the bulb off because it is hot enough

  delay(5000);  // wait 5 seconds before sampling temperature again  - WHY?

}

EDIT---

MY MISTAKE, I had a loose connection to Ground on the thermistor, giving it bad readings.

The code is error free now, and giving temperature readings accurately, but the Relay is turning on before the correct temperature is reached. I think i need to add a pulldown resistor to the base to keep the transistor from turning on falsely

Temp = log(((10240000/RawADC) - 10000));

==>

Temp = log(10240000.0/RawADC - 10000.0); // make all consts float

EDIT: Wow I must be high on paint fumes today...I want the relay on until a temperature is hit...

Just held my finger on the probe and watched it go up nice and slowly with 1/100th of a degree accuracy, once I hit the cut-off temperature the relay switched off and is staying off.

It just cooled back down and the relay turned back on.

I #$%@ING LOVE YOU GUYS TY. Month long project finally done!!!!!!!! I'm pumped.

In case anyone is interested in what the final project will be, I'm trying to reproduce this

http://www.russelldurrett.com/lightbulbpcr.html

Thanks again!

Can you explain a bit about PCR (is it the PCR=DNA multiplication?)

Yes PCR stands for Polymerase Chain Reaction, and it is a tool, or rather protocol, developed to make many many copies of a particular DNA sequence.

As you may know, DNA is double stranded, with one side "complementary to the other", and is held together by Hydrogen bonding, a rather "weak" bond in terms of the ways atoms can bond.

So, one way to break hydrogen bonding is by heating the DNA, which is where the light bulb comes in. It heats the small vials up to about 95C, which denatures the DNA and makes it break apart into it's two single strands.

Inside the mixture is also free nucleotides, the "bases" that make up DNA; A,T,G,C. A bonds with T, G bonds with C.

So, let's say we have the gene for Insulin production. We take that gene, mix in some free nucleotides, some special enzymes known as Polymerase enzymes (their discovery has a cool story, look up Taq enzyme) and some primers, and let it run about 30 cycles of heating up and cooling down.

Each time the DNA is "denatured", each single strand becomes a template, and is copied and becomes a double strand.

So, with a few million or hundred million copies of DNA in your initial sample, after 30 cycles you have a LOT of copies of one piece of DNA.

If it was the gene for Insulin, say for example, you can then clone it into a plasmid (like a blueprint) and put it into bacteria (little factory).

The E.Coli runs the blueprint, billions of times in a big fermented, and all you need to do is feed it some food.

Then, you harvest the drug you need (insulin) and bottle it and sell it.

That's an oversimplification but you'd be surprised how much molecular biology just makes sense. Repeat that with gene of interest...for penicilin...other drugs...ethanol production etc etc

"Real" thermocyclers are expensive, thousands of dollars, so trying to make one cheap.

The guy who invented PCR just used hot water baths and moved the tubes manually by hand hundreds of times, so, it's come a long way

what is the type of transistor used for this circuit?
if i want to use 12vdc source,do i need to change the value of resistor and the type of transistor?