My arduino fridge control build.

Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum

Help Support Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

simpleton

Active Member
Joined
Dec 10, 2008
Messages
28
Reaction score
0
Location
Texas
Howdy folk, I've been working on setting up an arduino to manage the temp in my fridge and I thought I would chronicle my adventure. What I have is rough but I think it will work. My setup is as follows:


1 x Arduino clone(freeduino, works the same)
1 x LM35 temp sensor
1 x P2N2222A transistor
1 x 1N4004 diode
1 x 1K resistor
1 x PB134005 relay

I'll post the schematic if someone needs it.

The LM35 output goes to analog 0
The relay for the fridge is on pin 13


Here is my arduino code.

float tempC;
float tempF;
int tempPin = 0;
int ledPin = 13;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps

}

void loop()
{
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature
tempF = (tempC * 9)/ 5 + 32; // converts to fahrenheit
Serial.print ((int) tempC ); //send the data to the computer

Serial.print(" Celsius, ");
Serial.print ((int) tempF ); //send the data to the computer
Serial.print(" Fahrenheit, -> ");
delay(1000); //wait one second before sending new data
digitalWrite(ledPin, HIGH); // sets the LED on

if (tempF < 71) digitalWrite(ledPin, HIGH);
if (tempF < 71) Serial.println ("fridge on");
if (tempF > 69) digitalWrite(ledPin, LOW);
if (tempF > 69) Serial.println ("fridge off");


This code was stolen from a number of arduino forums, mixed and matched, and then further mangled by me. I am not a programmer. Eventually I would like to have a computer log temprature, compresser state, and be able to adjust the min and max temp, and make all of this via a web page. For now I just need it to hold my fridge at 70F.

Next will be building temp probes like in Yuri's howto then wiring the relay into my fridge.


Simpleton
 
Nice. Do you have this working? I'm doing something similar with an Arduino to control my brewery. I found that the LM34's needed to have a resistor-capacitor for remote sensing. You may discover the same with the LM35.
 
oops. This line of code here:

Code:
digitalWrite(ledPin, HIGH); // sets the LED on

Is going to turn your fridge on as well since you're using pin13 for the fridge. Also, since you're controlling a fridge compressor you should add a short-cycle delay to prevent slugging the compressor.
 
Yeah, I noticed that this morning. There are a couple of other things I need to fix. After looking at it for hours last night I stared missing obvious things. It also looks like I have the fridge on/off backwards.... Doh!
 
For some reason I couldn't let go of this. I started thinking about how I would do it and thought well this is so simple I'll just bang out some code. Turns out there was a little more too it than I thought by adding the short-cycle delay and also reducing the output to the serial. While the computer will have no problem accepting a stream of data every second, it's overkill and a gets hard to read, so I increased it to 10 seconds.

That brought up another problem though of accepting a setpoint if the program was delaying 10 seconds every cycle. So I thought interrupts might be an ideal way to handle this. If you wire two momentary switches on pins 2 and 3 (must be 2 and 3 as these are interrupts 0 and 1) to ground, you can depress either button at anytime to increment or decrement the setpoint. At least I think so. I compiled and uploaded this code and tested it, but my interrupts are hooked up to a rotary encoder and one is always low so I couldn't test that portion. It's commented out.

The short cycle looks like it works just fine. My LM34 temp probes are not near me so I just let the input float to test it. I adapted this from my contoller, so there might be extra variables or code that aren't needed....
Code:
#define relayPin 12
#define tempUpPin 2
#define tempDownPin 3
#define ledPin 12
#define tempPin 0


float tempC;
float tempF;
int setPoint = 70;                            // Initializes set point at 70*
long time = 0;
long offTime = 0;
boolean relayOn = false;

void setup()
{
Serial.begin(9600);                           //opens serial port, sets data rate to 9600 bps
pinMode (relayPin, OUTPUT);
pinMode (ledPin, OUTPUT);
pinMode (tempUpPin, INPUT);
pinMode (tempDownPin, INPUT); 
digitalWrite (tempUpPin, HIGH);              // turn on internal pull-up resistor
digitalWrite (tempDownPin, HIGH);            // turn on internal pull-up resistor
//attachInterrupt (0, doTempUp, LOW);          // attach interrupt 0 on pin2 to doTempUp 
//attachInterrupt (1, doTempDown, LOW);        // attach interrupt 1 on pin3 to do temp down

}// end setup

void loop()
{
tempC = analogRead(tempPin);                   //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;          //convert the analog data to temperature
tempF = (tempC * 9)/ 5 + 32;                   // converts to fahrenheit
Serial.print ((int) tempC );                   //send the data to the computer

Serial.print(" Celsius, ");
Serial.print ((int) tempF );                   //send the data to the computer
Serial.print(" Fahrenheit, -> ");
Serial.print (" Current Setpoint -> ");
Serial.print (setPoint);
time = millis() / 1000;                                 // Sets time to the current total program runtime in seconds.  
if (tempF > setPoint ) {                                // Compare temp: Temperature is above setpoint
  if ( time - offTime > 300)  {                         // Check to confirm compressor has been off at least 5 minutes set to # of seconds
  digitalWrite( relayPin, HIGH);                        // Turn fridge on
  Serial.print (" fridge on ");
  relayOn = true;
  }
  else {                                                // 5 minute delay is not over
    Serial.print (" Short-cycle delay enabled ");
    }
  }    
else  {                                                 // Temperature is below setpoint
  if (relayOn)  {                                       // Confirm relay is currently on
    digitalWrite( relayPin, LOW);                       // Turn fridge Burner Off.
    Serial.println (" fridge off ");
    relayOn = false;                                    // Set HLT Burner Status to off
    offTime = time;                                     // Time stamps the off time for Burner.
  }
}
Serial.println();  
delay(10000);                                   //wait ten seconds before sending new data

}// end loop
/*
void doTempUp() {
  setPoint ++;                                          // increment setpoint by 1
  Serial.print ("New Setpoint -> ");                    // display to serial  
  Serial.println (setPoint);                                    
  delay (2000);                                          // allow 2 seconds to read message
}// end doTempUp

void doTempDown() {
  setPoint --;                                          // deincrement setpoint by 1
  Serial.print ("New Setpoint -> ");                    // display to serial  
  Serial.println (setPoint);                                    
  delay (2000);                                          // allow 2 seconds to read message
}// end doTempDown
*/
 
Back
Top