Hello,
I'm currently working on a project to control an old freezer.
to automate this process i am using:
Arduino NANO
- 1x waterproof DS18B20 temperature sensor
- I2C DS3231 real time clock module:
- I2C LCD 20X4
- 3x LED with a 270 ohm current limiting resistor to signal
- 2x pushbuttons to turn up or down the temperature
- a 4 channel 5V relay module (which will be replaced with a 5V OMRON G3MB-202P when i
make it to a pcb )//same thing as the 5V Songle relays
(the small relays will drive 230V high power relays) - external 5V power supply for the 5V relays (connected for complete opto isolation)(no common grounds)
so the code i wrote to use is this one:
#include <DS3231.h>////real time clock libary
#include <OneWire.h>//sensor communication libary
#include <LiquidCrystal_I2C.h>//i2c_lcd libary
#include <DallasTemperature.h>//DS18B20 temperature sensor libary
//----------------Libaries-setup-------------------------------------------------------------------------------
//----------------I2C-Real-Time-Clock-D3231-setup--------------------------------------------------------------
DS3231 rtc(SDA, SCL);
Time t;
// ---------------DS18B20-temperature-sensor-setup-------------------------------------------------------------
int SensorDataPin = 2;//datapin for the sensor this cannot be changed because of the DellasTemperature libary
OneWire oneWire(SensorDataPin);//onewire setup
DallasTemperature sensors(&oneWire); //onewire setup
float tempCelsius;// the value of the measured temperature in celcius
//arduino D2 is the sensor pin
//DellasTemperature.h is the libary needed for the DS18B20
int setTemp = -10; //default temperature = -10 degrees celcius this is adjustable to any temperature.
// Note that how higher the temperature is the more the relais have to switch
// temperature cannot be set lower than -30 this is because of the cooling plant limitations
// on the lcd screen it is possible to do but not relaiable
//----------------I2C-LCD-20X4-setup--------------------------------------------------------------------------
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x03F, 20, 4);
//arduino nano analog pin 4 = SDA
//arduino nano analog pin 5 = SCL
//LiquidCrystal_I2C.h & oneWire.h are needed for the i2C_lcd
//----------------led-sigalisation-control--------------------------------------------------------------------
int led_red = 12;// turns red if the temperature is not at the desired temperature
int led_orange = 11;// turns on if the heating elements are active
int led_green = 10;// turns on if the temperature is at the desired temperature
//----------------Knobs-temperature-control-------------------------------------------------------------------
int knop_up = 7;//turns the temperature up
int knop_down = 6;//turns the temperature down
int knopStatus;//converts digital input to an integer value
int knopStatus1;//converts digital input to an integer value
//-----------------Relay-setup-----------------------------------------------------------------------
int relay_klep = 5;//drives the high power relay to open the electro mechanical switch
int relay_heater = 4;//drives the high power relay to power the heating elements
// int relay_ventilator = 3;//drives the high power relay to turn on the ventilator
int relay_groep = 3;//drives the high power relay to turn on the high power cooling station
//-----------------Real-Time-Clock-DS3231----------------------------------------------------------------------
int onhour = 7;//turns on the heating elements at 7:30 am
int offhour = 8;//turns off the heating element at 8:00 am
int onhour1 = 19;//turns on the heating elements at 7:30 pm
int offhour1 = 20;//turns off the heating element at 8:00 pm
int offmin = 30;//turns on the heating elements at 7:30 am
int onmin = 0;//turns off the heating element at 8:00 am
int offmin1 = 30;//turns on the heating elements at 7:30 pm
int onmin1 = 0;;//turns off the heating elements at 8:00 Pm
//set the desired time here to turn the on the heater (7:30-8:00) && (19:30-20:00)
void setup()
{
//prevents the relays for turning on due to floating voltages
//----------------Main-setup-for-IO-ports--------------------------------------------------------------
Serial.begin(9600);//set the serial communication Baud rate to 9600
//which is fast enough for all the serial communication
//----------------Real-Time-Clock-DS3231----------------------------------------------------------------------
rtc.begin();//starts the I2C communication between the arduino and RTC
//----------------I2C-LCD-Setup-------------------------------------------------------------------------------
lcd.begin(20,4,LCD_5x8DOTS);//turns on the connection to the 20X4 i2c_lcd
lcd.init();//writes the lcd empty
lcd.backlight();//sets the lcd backlight on
lcd.setCursor (0,0);//sets the cursor
lcd.print ("Raphael Cochez");//display this text
lcd.setCursor (0,1);//sets the cursor
lcd.print ("Cooling V3");//display this text
lcd.setCursor (0,2);//sets the cursor
lcd.print ("Kast 1");//display this text
delay(10000);//adds a little delay for the startscreen
//also allows the DS18B20 to start measuring more accurate
lcd.init ();//writes the lcd empty
//----------------DS18B20-temperature-sensor-setup------------------------------------------------------------
sensors.begin();//turns on the DS18B20
//----------------Led-signalisation-setup---------------------------------------------------------------------
pinMode(led_red, OUTPUT);// set the digital pin to disable the pullup resistor
pinMode(led_orange, OUTPUT);// set the digital pin to disable the pullup resistor
pinMode(led_green, OUTPUT);// set the digital pin to disable the pullup resistor
//----------------Knobs-temperature-control-setup-------------------------------------------------------------
pinMode(knop_up, INPUT);// set the digital pin to enable the pullup resistor
pinMode(knop_down, INPUT); // set the digital pin to enable the pullup resistor
//----------------Relay-drivers-setup-------------------------------------------------------------------------
//pinMode(relay_ventilator,OUTPUT);// set the digital pin to disable the pullup resistor
pinMode(relay_klep, OUTPUT);// set the digital pin to disable the pullup resistor
pinMode(relay_heater, OUTPUT);// set the digital pin to disable the pullup resistor
pinMode(relay_groep, OUTPUT);// set the digital pin to disable the pullup resistor
digitalWrite(relay_klep,LOW);
digitalWrite(relay_heater, LOW);
digitalWrite(relay_groep,LOW);
//----------------serial-communication-setup------------------------------------------------------------------
Serial.setTimeout(10);//times out the serial communication for a 1/600 of a second
//enables smoother serial cummunication
}
void loop()
{
//---------------DS18B20-temperature-read-and-serial-monitor-displaying---------------------------------------
sensors.requestTemperatures();// send the command to get temperatures
tempCelsius = sensors.getTempCByIndex(0);// read temperature in Celsius
Serial.print("Temperature: ");//display this text in the serial monitor
Serial.print(tempCelsius);// print the temperature in Celsius
Serial.print("°C");//display this text in the serial monitor
//--------------Real-Time-Clock-D3231-time-request-and-serial-communication-----------------------------------
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (100);
//--------------Knobs-read-input-and-sets-the-desired-temperature---------------------------------------------
knopStatus = digitalRead (knop_up);
knopStatus1 = digitalRead (knop_down);
if (knopStatus == HIGH)//if this value is high the desired temperature is changed +1
{
setTemp ++;
Serial.println(" SetTemp ++");
}
if (knopStatus1 == HIGH)//if this value is high the desired temperature is changed -1
{
setTemp --;
Serial.println(" setTemp -- ");
}
//-------------Displaying-which-instalation-we-are-working-with-on-the-lcd------------------------------------
lcd.setCursor(0, 0);
lcd.print("Kast 1 ");
//------------Displaying-the-DS18B20-sensor-measured-value-on-the-lcd-----------------------------------------
lcd.setCursor(0, 2);
lcd.print("sensor: ");
lcd.print (tempCelsius);
lcd.print(" C");
//------------Displaying-the-desired-temperature-on-the-lcd---------------------------------------------------
lcd.setCursor(0, 3);
lcd.print("Set Temp: ");
lcd.print (setTemp);
lcd.print(" C ");
//------------Displaying-the-desired-temperature-on-the-lcd---------------------------------------------------
lcd.setCursor(15, 0);
lcd.print(t.hour);
lcd.print (":");
lcd.print (t.min);
delay (100);//delay gives code room to breath
if(t.hour == onhour && (t.min == onmin))
{
digitalWrite (relay_groep, LOW);//turns off the cooling plant
digitalWrite (relay_klep, LOW);//turns off the electro mechanical switch
digitalWrite (led_red, LOW);//turns off the fault signal because its in heating mode
digitalWrite (led_green, LOW);//turns on the green led to indicate that the temperature is ok
delay(60000); //gives the system time to reset
digitalWrite (relay_heater, HIGH);//turns on the heater
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
}
if(t.hour == offhour &&(t.min == offmin))
{
if (tempCelsius > setTemp )
//checks if there is no change in desired temperature
//compares the measured temperature with the desired temperature
{
//control of the relays
digitalWrite (relay_groep, LOW);//turns on the cooling plant
digitalWrite (relay_klep, LOW);//turns on the electro mechanical switch
digitalWrite (relay_heater, HIGH);//turns of the heater to make sure its off
digitalWrite (led_green, LOW);//indicates that the temperature is not ok
digitalWrite (led_orange, LOW);//indicates that the heater is off
//red led blinks 5 times to signal a to high measured temperature after that it stays high for 25 seconds
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red,HIGH);//turns on the led to give the indicator that the temperature is not ok
}
if (tempCelsius <= setTemp)
{
//control of the relays
digitalWrite (relay_groep, LOW);//turns off the cooling plant
digitalWrite (relay_klep, LOW);//turns off the electro mechanical switch
digitalWrite (relay_heater, LOW);////turns of the heater to make sure its off
digitalWrite (led_red, LOW);//turns of the heater to make sure its off
digitalWrite (led_green, HIGH);//turns on the green led to indicate that the temperature is ok
digitalWrite (led_orange, LOW); //indicates that the heater is off
delay (60000);
}
//------------------------second-part---------------------------------------------------------------------------
if(t.hour == onhour1 && (t.min == onmin1))
{
digitalWrite (relay_groep, LOW);//turns off the cooling plant
digitalWrite (relay_klep, LOW);//turns off the electro mechanical switch
digitalWrite (led_red, LOW);//turns of the heater to make sure its off
digitalWrite (led_green, LOW);//turns on the green led to indicate that the temperature is ok
delay(60000); //gives the system time to reset
digitalWrite (relay_heater, HIGH);//turns on the heater
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_orange, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_orange, HIGH);//turns on the led to give the indicator that the temperature is not ok
}
if(t.hour == offhour1 && (t.min == offmin1))
{
digitalWrite (relay_heater, LOW);//turns of the heater to make sure its off
delay(60000);//gives the system time to reset
if (tempCelsius > setTemp )
//checks if there is no change in desired temperature
//compares the measured temperature with the desired temperature
{
//control of the relays
digitalWrite (relay_groep, HIGH);//turns on the cooling plant
digitalWrite (relay_klep, HIGH);//turns on the electro mechanical switch
digitalWrite (relay_heater, LOW);//turns of the heater to make sure its off
digitalWrite (led_green, LOW);//indicates that the temperature is not ok
digitalWrite (led_orange, LOW);//indicates that the heater is off
//red led blinks 5 times to signal a to high measured temperature after that it stays high for 25 seconds
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red, HIGH);//turns on the led to give the indicator that the temperature is not ok
delay (1000);
digitalWrite (led_red, LOW);//turns off the led shortly
delay (1000);
digitalWrite (led_red,HIGH);//turns on the led to give the indicator that the temperature is not ok
}
if (tempCelsius <= setTemp)
{
//control of the relays
digitalWrite (relay_groep, LOW);//turns off the cooling plant
digitalWrite (relay_klep, LOW);//turns off the electro mechanical switch
digitalWrite (relay_heater, LOW);////turns of the heater to make sure its off
digitalWrite (led_red, LOW);//turns of the heater to make sure its off
digitalWrite (led_green, HIGH);//turns on the green led to indicate that the temperature is ok
digitalWrite (led_orange, LOW); //indicates that the heater is off
delay (60000);
}
}
}
}
The issues i encounter:
- The relays wont receive any signal using this code but i can't find out why.
- The code doesn't seems to compare the measured signal with the desired signal
- I encounter floating voltages on the relays which is why i tried to write the internal pullup
resistor to make sure its an input and a low output state
Things i tried to fix my problem:
- With a regular testcode* on pin 3, 4, 5 the relays work fine so there is no fault in the wiring
- The DS18B20 is working fine and displays its value on the serial monitor as well on the I2C LCD
- The integer to set the desired temperature is also working and displays its value on the serial
monitor as well on the I2C LCD to. - i tried to write my used pins 3, 4, 5 active LOW in the setup
Can i get some help with this issue? I am breaking my mind over this stuff.
I don't think there is a wiring problem because when testing al different parts separately they work fine.
my guess is there is something wrong with the DS3231 parts of the code or there is some minor mistake which i completely overlooked.
testcode*
int button1 = 6;
int button2 = 7;
int knopState1;
int knopState2;
int relay1 = 3;
int relay2= 4;
int relay3 = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(knop1, INPUT);
pinMode(knop2, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
knopState1 = digitalRead (button1);
knopState2 = digitalRead (button2);
if (knopstate1 == HIGH)
{
Serial.println ("button ok");
digitalWrite (relay1, HIGH);
delay (800);
digitalWrite (relay2, LOW);
delay (800);
digitalWrite (relay3, LOW);
delay (800);
Serial.println ("relay 1 works");
}
if (knopState2 == HIGH)
{
Serial.println ("button 2 ok");
digitalWrite (relay1, LOW);
delay (800);
digitalWrite (relay2, HIGH);
delay (800);
digitalWrite (relay3, HIGH);
delay (800);
Serial.println ("relay2 & relay3 works$");
}
}
