Beer cellar thermostat

So I need a lot of help with my programming. I’m using a mini fridge to cellar some beers and the onboard thermostat is swinging the temperature by 5-10 degrees. That sparked the idea that I could create something more stable.
I want to accomplish a really simple thermostat for now. Ill set the temperature in the code and the fridge will just stay at that temperature.

Ill be using a 10A relay to turn the fridge on and off. For the temperature sensor I’m using DS18B20.
So far I’m able to get a temperature reading to print to the serial monitor but have no idea how to program the rest.

Eventually Ill be getting an LCD with user controls but I first what to get it working with the basics.

If someone could give me any help or point me to an example that will relate to this it would help me a lot.

Cellar_Fridge_Controller.ino (385 Bytes)

and the onboard thermostat is swinging the temperature by 5-10 degrees.

Perhaps the thermostat isn't the issue. Why are you assuming that it is?

So far I'm able to get a temperature reading to print to the serial monitor but have no idea how to program the rest.

Explain, in words, not code, what you want to do. 90% of the time, failure to create code to implement requirements is a problem with the requirements, not the code.

PaulS: Perhaps the thermostat isn't the issue. Why are you assuming that it is? Explain, in words, not code, what you want to do. 90% of the time, failure to create code to implement requirements is a problem with the requirements, not the code.

Reason I say its the thermostat is because I was logging the temp with my aquarium controller and I noticed the large swings when using the onboard thermostat. I then used the aquarium controller to control the temperature I only had swings of 0.5-1 degree. I don't want use the aquarium controller for this because normally use it for my reef tank.

What I would like to accomplish is this: When the fridge reaches the high temperature (8 celsius) I want to fridge to turn on. When the fridge reached the low temperature (7 celsius) I want the fridge to turn off.

What I would like to accomplish is this: When the fridge reaches the high temperature (8 celsius) I want to fridge to turn on. When the fridge reached the low temperature (7 celsius) I want the fridge to turn off.

So, if you know the temperature, and you know how to turn the relay on and off, what is the problem?

Reason I say its the thermostat is because I was logging the temp with my aquarium controller and I noticed the large swings when using the onboard thermostat. I then used the aquarium controller to control the temperature I only had swings of 0.5-1 degree. I don't want use the aquarium controller for this because normally use it for my reef tank.

I wasn't arguing that the thermostat wasn't the problem. So, thank you for the explanation. It seems that you have logically considered the factors, and come to the correct conclusion.

PaulS: So, if you know the temperature, and you know how to turn the relay on and off, what is the problem?

That is the problem :) Don't know how to tell the relay to turn on/off based on the temperature.

Starter question : Do you know how to read the temperature ?

UKHeliBob: Starter question : Do you know how to read the temperature ?

I know how to print it to serial but I don't think that's the same as reading so ill have to say no I don't.

If you can print it then you are reading it so you are half way there.

Let’s suppose that you read it into a variable named temperature and print it.
Now you can do stuff like

if (temperature >= 8)
{
  Serial.println("Temperature too high");
}
else if (temperature <= 7)
{
  Serial.println("Temperature too low");
}

Do that then you can move on to turning the relay on or off instead of printing messages.

Next question : do you know how to turn the relay on and off ?

UKHeliBob:
Next question : do you know how to turn the relay on and off ?

this is my guess

set me pin
int FridgeOn = 8;

turn on relay… dont know where this would go in the code
digitalWrite(FridgeOn, HIGH)

Close, but no cigar !

Naming the pin is a good idea but I don't like the name you chose as it is not descriptive as to what the pin does, but it would work. Better might be

const byte relayPin

const because its value will never change, byte because it uses less memory than an int and relayPin because that's what it is.

Then in setup() you need

pinMode(relayPin, OUTPUT);

This allows the pin to be set to HIGH or LOW, 5V or 0V respectively.

To change the state of the relayPin you use digitalWrite() as you guessed and it would go where the Serial.print()s are in my code as actions to be taken depending on the temperature. Leave the print commands there though as they could be useful for debugging later.

Next question : what sort of relay do you have and how is it connected to the Arduino ? Surprisingly you may need to set relayPin LOW to turn on the relay and vice versa.

const because its value will never change, byte because it uses less memory than an int and relayPin because that's what it is.

I'm no expert by any means, but out of curiosity i'm going to ask, why not just use a boolean?

UKHeliBob:
Close, but no cigar !

Naming the pin is a good idea but I don’t like the name you chose as it is not descriptive as to what the pin does, but it would work. Better might be

const byte relayPin

const because its value will never change, byte because it uses less memory than an int and relayPin because that’s what it is.

Then in setup() you need

pinMode(relayPin, OUTPUT);

This allows the pin to be set to HIGH or LOW, 5V or 0V respectively.

To change the state of the relayPin you use digitalWrite() as you guessed and it would go where the Serial.print()s are in my code as actions to be taken depending on the temperature. Leave the print commands there though as they could be useful for debugging later.

Next question : what sort of relay do you have and how is it connected to the Arduino ? Surprisingly you may need to set relayPin LOW to turn on the relay and vice versa.

Not using the one I original relay I had planned to use because I cant find it. So now ill be using a Sainsmart solid state relay.
this one → Sainsmart Solid State Relay

Here is my code so far. I feel I’m getting closer and its making a lot more sense. Thanks again UKHeliBob for all the help.

#include <OneWire.h>

#include <DallasTemperature.h>

OneWire oneWire(13);

const byte relayPin(8);

DallasTemperature sensors(&oneWire);



void setup()
{
  sensors.begin();
  Serial.begin(57600);
  pinMode(relayPin, OUTPUT);
}
void loop()

{
  sensors.requestTemperatures();
  float temperature;
  temperature = sensors.getTempCByIndex(0);
  
  
if (temperature >= 8)
{
  Serial.println("Temperature too high");
}
else if (temperature <= 7)
{
  Serial.println("Temperature too low");
}

  Serial.print("Temp =");
  Serial.print("\t");
  Serial.println(temperature);

  delay(2000);
}

oops replied with the wrong code. let me try again…

#include <OneWire.h>

#include <DallasTemperature.h>

OneWire oneWire(13);

const byte relayPin(8);

DallasTemperature sensors(&oneWire);



void setup()
{
  sensors.begin();
  Serial.begin(57600);
  pinMode(relayPin, OUTPUT);
}
void loop()

{
  sensors.requestTemperatures();
  float temperature;
  temperature = sensors.getTempCByIndex(0);
  
  
if (temperature >= 8)
{
  Serial.println("Temperature too high");
  digitalWrite(relayPin, HIGH);
}
else if (temperature <= 7)
{
  Serial.println("Temperature too low");
  digitalWrite(relayPin, LOW);
}

  Serial.print("Temp =");
  Serial.print("\t");
  Serial.println(temperature);
  

  delay(2000);
}

but out of curiosity i'm going to ask, why not just use a boolean?

A boolean and a byte are the same size. And, how can a pin number be true or false?

Darn just realized that Sainsmart solid state relay I mentioned earlier wont work. Its rated for 2.0 amps and the fridge draws about 2.5 amps running.

OneWire oneWire(13);

const byte relayPin(8);

You might want to review those lines of code. That is not how you give a pin a name.

UKHeliBob: You might want to review those lines of code. That is not how you give a pin a name.

Is this better?

OneWire oneWire = 13;
const byte relayPin = 8;

Is this better?

No. The OneWire declaration was correct initially. The relayPin declaration, though unconventional, was not wrong. either. The second one is now conventional.

oh ok. got it