Pages: [1]   Go Down
Author Topic: Help declaring a variable for temperature in thermistor--relay circuit  (Read 1497 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!

Quote
#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

}

« Last Edit: April 12, 2012, 02:53:35 pm by dkotes » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34532
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Take this line:-
Code:
double Temp;
and place it just after this line:-
Code:
#include <math.h>

This will make Temp a global variable accessible to all functions.
 
When posting code use the # icon not the quote one.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

fixed?
Code:
#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?

}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 12, 2012, 03:27:30 pm by dkotes » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

==>

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: April 12, 2012, 03:49:21 pm by dkotes » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: