Arduino, Max6675, LiquidCrystal_I2C temperatore control

Hello, please help, I just started with Arduino.
I seceded to compile attached program but is difficult to connect to computer every time when I want to change temperature. I try to modify with 2 switch or potentiometer without success. I need to modify between 100 to 200 °C .Thanks to Ahmad Shamshiri for robojax.com for initial program & anticipate Thanks for help
This is my code:

#include <max6675.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);  // set the LCD address to 0x3F for a 16 chars and 2 line display

int GNDpin = 2;
int VCCpin =3;
int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

const int relayPin =8;
const int relayON = LOW;// do not change
const int relayOFF = HIGH; //do not chage 
int relayState = relayOFF;//initial state of relay

const int TEMPERATURE_UNIT =1;//1=Celsius, 2=Fahrenheit, 3=Keliven
const float START_TEMPERATURE = 175.0;//unit above
const float STOP_TEMPERATURE =180.0;//unit above
const int CONTROL_TYPE = 1;// 1= heater, 2=cooler
float temperature;

void setup() {

  lcd.init();
  lcd.clear();         
  lcd.backlight();      // Make sure backlight is on
 
  Serial.begin(9600);
  Serial.println("MAX6675 test with relay");
  // use Arduino pins 
  pinMode(relayPin, OUTPUT);//pin for relay
  digitalWrite(relayPin, relayState);
  
  pinMode(VCCpin, OUTPUT);
  digitalWrite(VCCpin, HIGH);

  pinMode(GNDpin, OUTPUT);
  digitalWrite(GNDpin, LOW);

  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
 
   readTemperature();
   printTemperature();
   loadControl(); 
   if(temperature >=165.5)
   {
    ///
   }
   delay(1000);
   
}//loop



/*
 * loadControl()
 * @brief controls the load based 
 * @param state can be either : relayON or relayOFF

 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
 */
void loadControl()
{
//Robojax.com heater/cooler with MAX6675 Thermocoupler  
     // Serial.print("Start: ");
     // Serial.print(START_TEMPERATURE);
     // Serial.print(" Stop: ");
      //Serial.println(STOP_TEMPERATURE);      
  if(CONTROL_TYPE ==1)
  {
    if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
    {
      relayControl(relayON);
    }
    if(STOP_TEMPERATURE <=temperature)
    {
      relayControl(relayOFF);
    }
  }else{
    if(START_TEMPERATURE >= temperature && STOP_TEMPERATURE >=temperature)
    {
      relayControl(relayOFF);
    }
    if(STOP_TEMPERATURE <=temperature)
    {
      relayControl(relayON);
    }
  }
    
}//loadControl()


/*
 * relayControl(int state))
 * @brief turns the relay ON or OFF
 * @param state is "relayON" or "relayOFF" defined at the top of code
 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
 */
void relayControl(int state)
{
  //Robojax.com heater/cooler with MAX6675 Thermocoupler
  if(state ==relayON)
  {
    digitalWrite(relayPin, relayON);
    Serial.println("Relay ON"); 
    lcd.setCursor(2,0);   //Set cursor to character 2 on line 0
    lcd.print("Relay ON");  
  }else{
   digitalWrite(relayPin, relayOFF);    
    Serial.println("Relay OFF"); 
    lcd.setCursor(2,0);   //Set cursor to character 2 on line 0
    lcd.print("Relay OFF");   
  }
 
}//relayControl()


/*
 *  readTemperature()
 * @brief reads the temperature based on the TEMPERATURE_UNIT
 * @param average temperature

 * @return returns one of the values above
 * Written by Ahmad Shamshiri for robojax.com
 * on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
 */
void readTemperature()
{
 //Robojax.com heater/cooler with MAX6675 Thermocoupler 

  if(TEMPERATURE_UNIT ==2)
   {
   temperature = thermocouple.readFahrenheit();//convert to Fahrenheit 
   }else if(TEMPERATURE_UNIT ==3)
   {
    temperature = thermocouple.readCelsius() + 273.15;//convert to Kelvin
   }else{
    temperature = thermocouple.readCelsius();// return Celsius
   }    
 //Robojax.com heater/cooler with MAX6675 Thermocoupler 
}// readTemperature()

/*
 * printTemperature()
 * @brief prints  temperature on serial monitor
 * @param charact type
 * @param "type" is character
 *     C = Celsius
 *     K = Keliven
 *     F = Fahrenheit
 * @return none
 * Written by Ahmad Shamshiri for robojax.com
 * on May 08, 2020 at 02:45 in Ajax, Ontario, Canada
 */
void printTemperature()
{
//Robojax.com heater/cooler with MAX6675 Thermocoupler
   Serial.print (temperature);
    printDegree();    
  if(TEMPERATURE_UNIT ==2)
   {
     Serial.print("F");
    }else if(TEMPERATURE_UNIT ==3)
   {
     Serial.print("K");
   }else{
     Serial.print("C");
   }    
  Serial.println();
//Robojax.com heater/cooler with MAX6675 Thermocoupler  
}//printTemperature()

/*
 * @brief prints degree symbol on serial monitor
 * @param none
 * @return returns nothing
 * Written by Ahmad Shamshiri on July 13, 2019
 * for Robojax Tutorial Robojax.com
 */
void printDegree()
{
    Serial.print("\\xC2"); 
    lcd.setCursor(2,1);   //Move cursor to character 2 on line 1
    lcd.print("Temp=");
    lcd.print ( thermocouple.readCelsius());
   // lcd.print (analogRead(pin));    
    Serial.print("\\xB0");  
}

Hi,
hope this code helps.
Connect the center pin of a potentiometer to pin A0.
Connect one side to +5V and the other to GND.

void loop() {
 
   readTemperature();
   printTemperature();
   loadControl(); 
   int adjuste = analogRead(A0);
   float limit = map(adjuste, 0, 1023, 10000, 20000);
   limit = limit/100;
   Serial.print("limit ");Serial.println(limit);
   if(temperature >=limit)
   {
    ///
   }
   delay(1000);
   
}//loop

Thank you very much

I done modification and work fine.

If is not too much I have another issue, I found on net Arduino Thermostat and I try to replace DHT11 with my sensor MAX6675 and no success after few days. I need indication only in Celsius to simplify sketch, with increment by 10°C. I don’t attach sketch, it is more easy with link.

Once again THANK YOU

Hi,

Have you written JUST a simple code or used a Library example to prove you are connected and reading values from the DHT11 via the IDE monitor?

Forget your main code, JUST establish you are able to read the DHT11.

Please post your simple code and circuit diagram.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
try this code:

#include <max6675.h>
//#include "max6675.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

// Button configuration
const int
  DECREMENT_BTN_PIN = 7,
  INCREMENT_BTN_PIN = 8;

// "Heater" and "cooler" configuration
const int
//  HEATER_PIN = 13,
//  COOLER_PIN = 10;
  HEATER_PIN = 4,                     // Pin changed
  COOLER_PIN = 5;                     // Pin changed

// Controls
int targetC = 22;
//------------------------------------------------------------------------------------------------
void setup() {
  // Serial (for logging)
  Serial.begin (9600);
  //Serial.print("setup()\n");

  // LCD
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.print("\tLCD initialized\n");

  // Buttons
  pinMode(INCREMENT_BTN_PIN, INPUT_PULLUP);
  pinMode(DECREMENT_BTN_PIN, INPUT_PULLUP);

  // Temperature Controllers
  pinMode(HEATER_PIN, OUTPUT);
  pinMode(COOLER_PIN, OUTPUT);
}
//------------------------------------------------------------------------------------------------
void loop() {
   
  // Read Button State
  bool incrementTarget = digitalRead(INCREMENT_BTN_PIN);
  bool decrementTarget = digitalRead(DECREMENT_BTN_PIN);
  bool wait = incrementTarget || decrementTarget;

  // Configure
  if (incrementTarget) { targetC+=10; }
  if (decrementTarget) { targetC-=10; }
  const char symbol = 'C';
  const int target = targetC;

  // Read Sensor Data
  float temperature = thermocouple.readCelsius();// return Celsius
  //Serial.print("\t" + String(temperature) + "°" + symbol + "% \n");
  
  // Write out to LCD
  lcdWrite(0, "Temp:   " + String(temperature) + symbol);
  lcdWrite(1, "Target: " + String(target) + ".00" + symbol);

  // Activate Temperature Control
  if(round(target) > round(temperature)) digitalWrite(HEATER_PIN, HIGH);
  else digitalWrite(HEATER_PIN, LOW);

  if(round(target) < round(temperature)) digitalWrite(COOLER_PIN, HIGH);
  else digitalWrite(COOLER_PIN, LOW);

  // Pause to allow finger to lift from button(s)
  if(wait) delay(500);
}

// Utility to simplify writing to LCD
void lcdWrite(const bool line, const String& text){
  lcd.setCursor(0, line);
  lcd.print(text);
}

Hello again, sorry but take time until I will do it myself.
At first sketch is possible to add value in place off “xxx” to know what value I set.

void printDegree()
{
    Serial.print("\\xC2"); 
    lcd.setCursor(0,0);   //Move cursor to character 2 on line 1
    lcd.print("Temp=");
    lcd.print (round (thermocouple.readCelsius()));
    lcd.print(" off ");
    lcd.print ("xxx");
    Serial.print("\\xB0");  
}

For second sketch, I will test physical after finish with first one.
Thank you very much for everything.
Stelian

Hello again
I succeeded to replace “xxx” with limit value, but during test the relay don’t follow limit value.
I will try to see what the issue but some help is wonderful to not lose another few days.
Thank you very much
Stelian

Hello
Thank you for your help, in final I succeed to solve my problem and tested and run ok.
This is the final entry how solve my problem

void loop() {
 
   readTemperature();
   printTemperature();
   loadControl(); 
   int adjuste = analogRead(A0);
   float limit = map(adjuste, 0, 1023, 10000, 20000);
   limit = limit/100;
   Serial.print("limit ");Serial.println(limit);
   lcd.setCursor(10,0);lcd.print (round(limit));
   if(temperature >=limit) relayControl(relayOFF);
   else
   if(temperature <=limit) relayControl(relayON);
   
   {

Thank you
Stelian

Hi,
Do you have in your project an indicator, LED, to visually show if your relay is ON or OFF?

A problem you may come across is the chatter or fast operating of the relay as the temperature varies around the set point.
You may need to add some hysteresis in your control to stop this.

That is you get the relay to turn OFF at limit, but turn ON at limit - 1 or 2 degrees.

Tom... :smiley: :+1: :coffee: :australia:

Hello

Yes, first my LCD display on line tow show status of relay and my module relay is have led indication.

Stelian

Hello again
I try and succeed at second sketch to use only one relay. I tested and work in a better range off temperatures. I will attach my final sketch.
Thank you again for your help
Best regards Stelian

#include <max6675.h>
//#include "max6675.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

// Button configuration
const int
  DECREMENT_BTN_PIN = 7,
  INCREMENT_BTN_PIN = 8;

// "Heater" and "cooler" configuration
const int
  RELAY_PIN = 4,    
  COOLER_PIN = 4;  

// Controls
int targetC = 100;
//------------------------------------------------------------------------------------------------
void setup() {
  // Serial (for logging)
  Serial.begin (9600);
  Serial.print("setup()\n");

  // LCD
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.print("\tLCD initialized\n");

  // Buttons
  pinMode(INCREMENT_BTN_PIN, INPUT_PULLUP);
  pinMode(DECREMENT_BTN_PIN, INPUT_PULLUP);

  // Temperature Controllers
  pinMode(RELAY_PIN, OUTPUT);

}
//------------------------------------------------------------------------------------------------
void loop() {
   
  // Read Button State
  bool incrementTarget = digitalRead(INCREMENT_BTN_PIN);
  bool decrementTarget = digitalRead(DECREMENT_BTN_PIN);
  bool wait = incrementTarget || decrementTarget;

  // Configure
  if (incrementTarget) { targetC+=10; }
  if (decrementTarget) { targetC-=10; }
  const char symbol = 'C';
  const int target = targetC;

  // Read Sensor Data
  float temperature = thermocouple.readCelsius();// return Celsius
  //Serial.print("\t" + String(temperature) + "°" + symbol + "% \n");
  
  // Write out to LCD
  lcdWrite(0, "Temp:   " + String(temperature) + symbol);
  lcdWrite(0, "Temp:   " + String(temperature) + symbol);
  lcdWrite(1, "Target: " + String(target) + ".00" + symbol);

  // Activate Temperature Control
  if(round(target) > round(temperature)) digitalWrite(RELAY_PIN, LOW);
   else //digitalWrite(HEATER_PIN, LOW);
  if(round(target) < round(temperature)) digitalWrite(RELAY_PIN, HIGH);
  
  // Pause to allow finger to lift from button(s)
  if(wait) delay(500);
}

// Utility to simplify writing to LCD
void lcdWrite(const bool line, const String& text){
  lcd.setCursor(0, line);
  lcd.print(text);

}

Hello
During test off sketch with push buttons I observed that not have limits for target temperature, is going to “-“. I try myself but for moment no success. Pls help.
Thank you
Stelian

Hi,
and what would be the upper and lower limits?

Hello
Limits 10 to 250
Stelian

Limits (target100 min.10 max.250

try this way:

 // Configure
  if (incrementTarget) {targetC += 10;}
  if (targetC > 250 ){targetC = 250;}
  if (decrementTarget) {targetC -= 10;}
  if (targetC < 10 ) {targetC = 10;}

Hello
Thank you very much, everything is perfect now. Your help is very much appreciated.
Stelian

Hello
I modify sketch and is working fine with a small issue. When I decrees value under 100 is showing in stand of 90 show 900 on LCD but in serial show correctly
Attached is sketch:

#include <max6675.h>
//#include "max6675.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display

int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

// Button configuration
const int
DECREMENT_BTN_PIN = 7,
INCREMENT_BTN_PIN = 8;

// "Heater" and "cooler" configuration
const int
RELAY_PIN = 4,
COOLER_PIN = 4;

int relayState = digitalRead(RELAY_PIN);

// Controls
int targetC =100;
//------------------------------------------------------------------------------------------------
void setup() {
  // Serial (for logging)
  Serial.begin (9600);
  Serial.print("setup()\n");

  // LCD
  lcd.init();
  lcd.clear();
  lcd.backlight();      // Make sure backlight is on
  Serial.print("\tLCD initialized\n");

  // Buttons
  pinMode(INCREMENT_BTN_PIN, INPUT_PULLUP);
  pinMode(DECREMENT_BTN_PIN, INPUT_PULLUP);

  // Temperature Controllers
  pinMode(RELAY_PIN, OUTPUT);

}
//------------------------------------------------------------------------------------------------
void loop() {

  // Read Button State
  bool incrementTarget = digitalRead(INCREMENT_BTN_PIN);
  bool decrementTarget = digitalRead(DECREMENT_BTN_PIN);
  bool wait = incrementTarget || decrementTarget;

  // Configure
  if (incrementTarget) {
    targetC += 10;
  }
  //  if (targetC > 250 ){targetC = 250;}
  if (decrementTarget) {
    targetC -= 10;
  }
  //  if (targetC < 10 ) {targetC = 10;}
  const char symbol = 'C';
  const int target = targetC;

  // Read Sensor Data
  float temperature = thermocouple.readCelsius();// return Celsius
  Serial.print("\t" + String(temperature) + "°" + symbol + "% \n");

  // Write out to LCD
  lcd.setCursor(0, 0);
  Serial.print ("Temp: " + String(temperature));
  lcd.print ("Temp:  ");
  lcd.print(round(temperature));
  lcd.print(" / ");
  Serial.print ("Target: " + String(target) + ".00" + symbol);
  lcd.print (target);

  // Activate Temperature Control
  if (round(target) > round(temperature)) digitalWrite(RELAY_PIN, LOW);
  else //digitalWrite(HEATER_PIN, LOW);
    if (round(target) < round(temperature)) digitalWrite(RELAY_PIN, HIGH);

int relayState = digitalRead(4);
if (relayState == HIGH)
{
  Serial.println("LOW");
} else {
  Serial.println("HIGH");
}
lcd.setCursor(0, 1);
if (relayState== HIGH)
{
  lcd.print ("Relay OFF");
} else {
  lcd.print ("Relay  ON");
  
}
  // Pause to allow finger to lift from button(s)
  if (wait) delay(500);
}

Please help, thank you in advance
Stelian

Hi,
When your write the temperature value to the LCD, do you clear the old value with spaces before putting the new value in.
When you go from 100 to 90, the LCD command is left justified, so 90 writes over the 1 and 0 figures in 100 and leaves the other 0 on display, hence 900.

You need to check if the value has changed, if so, then send 3 spaces to the LCD before displaying the new value.

Don't use lcd.clear each time to clear your values as that will make the LCD screen flicker.

I hope that makes sense.

Tom... :smiley: :+1: :coffee: :australia:

Hello
Thank you very much, I do the entry and now is working fine
BRGDS Stelian