Hello,
I've been using my Arduino Uno copy to control my beer brewing thermostat for some time. To facilitate temperature change, I lately installed a potentiometer connected to an LCD display to control the temperature setpoint. This has been working well, except for the fact that sometimes when I change the temperature setpoint using the potentiometer the relay doesn't follow, i.e. if I put the setpoint temperature below the actual temperature the relay doesn't stop, and if I put the setpoint temperature above the actual temperature the relay doesn't start. If I press the reset button, the problem disappears, but it is annoying to have to press the reset button every time a setpoint change has been made.
Moreover, sometimes the board and LCD display freeze, causing the relay to be stuck in either on or off position but the displayed temperature to remain the same. I did not have this problem before, when I just manually changed the temperature setpoint in the code instead. Could it be that the code is too extensive or complicated, so the board is overloaded? I'm only using around 40% of the board memory.
To more easily notice the freezing problem, I added a blinking LED to give me an indication of if it decides to freeze.
Below is the code, the first part of which is only to identify the temperature sensor. I apologise if you find it messy, but since I'm not much of a programmer I've only used other people's code and modified it to my purposes. I'm sure there's a quicker way to do it
Equipment:
Arduino Uno copy from FastTech
Arduino Compatible IIC / I2C Serial 2.5" LCD 1602 Display Module, FastTech (Brand=QAPASS I think)
Keyes 5V Relay Module for Arduino, FastTech
10k potentiometer
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
#define RELAY1 7
const int LED = 9; // the pin for the LED
float old_tempC = 20; //Value used to store temperature and start heating from the beginning
int Buzzer = 12; //the pin for the buzzer is 12
int Potpin = 2;
int Setpoint;
int val;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup(void)
{
pinMode(LED, OUTPUT); // LED is as an OUTPUT
pinMode(RELAY1, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // lcd backlight on
// locate devices on the bus
Serial.print("Locating devices...");
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
digitalWrite(RELAY1, 1); //To turn relay on in the beginning
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
// set the resolution to 12 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 10);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
val = analogRead(Potpin);
Setpoint = map(val, 0, 1023, 20, 100);
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("Setpoint:");
lcd.print(Setpoint);
lcd.setCursor(0, 1);
lcd.print("Actual temp:");
lcd.print(tempC);
if ((tempC < Setpoint) && (old_tempC >= Setpoint)) {
digitalWrite(RELAY1, 1); // Turns ON Relays 1
tone(Buzzer, 261); //261 is A //Turn off to remove unnecessary noise
// Serial.println("Temperature low, start heating");
lcd.clear();
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("Temp low,");
lcd.setCursor(0, 1);
lcd.print("start heating");
digitalWrite(LED, LOW);
delay(1000);
noTone(Buzzer);
}
else
{
digitalWrite(LED, HIGH);
noTone(Buzzer);
lcd.clear();
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("Setpoint:");
lcd.print(Setpoint);
lcd.setCursor(0, 1);
lcd.print("Actual temp:");
lcd.print(tempC);
}
if ((tempC >= Setpoint) && (old_tempC <= Setpoint)) {
// Serial.println("Temperature high, stop heating");
digitalWrite(RELAY1, 0); // Turns Relay Off
lcd.clear();
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("Temp high,");
lcd.setCursor(0, 1);
lcd.print("stop heating");
digitalWrite(LED, LOW);
delay(3000);
}
else
{
digitalWrite(LED, HIGH);
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("Setpoint:");
lcd.print(Setpoint);
lcd.setCursor(0, 1);
lcd.print("Actual temp:");
lcd.print(tempC);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
}
// FOR EMERGENCY TONE:
if (tempC >= 85)
{
tone(Buzzer, 1000);
}
old_tempC = tempC;
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
// Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println("DONE");
delay(1000);
printTemperature(insideThermometer); // Use a simple function to print out the data
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
Thank you for taking your time to read this. If you have any suggestions on how to get the commands to start acting on a change in potentiometer reading immediately, every time, or on how to get rid of the complete relay freeze, please let me know.
Best regards,
Tomas Niemeyer
EDIT: Removed the unnecessary commented out commands, used the Auto Format function. Haven't looked into any other command instead of PrintTemperature yet.