Display Relay status on LCD depending on Temp threshold

Hello,

I have some issue in my coding to display the relay status for a given temperature on an LCD. Searched the forum and google but can't figure it out.

So the idea is:

If temp < 5 degrees the relay is on
LCD should display: T<5C Relay ON

If temp < 0 degrees the relay is off
LCD should display: T<0C Relay OFF

If temp > 5 degrees the relay is OFF
LCD should display: T>5C Relay OFF

Now the relay is working fine with the temp thresholds.
The LCD displays the relay state but not depending on the temp.
It always stays at: T > 5C Relay: OFF

I thinks something with an IF statement should be added to the code for the LCD.

This is the code:

/*
 * Arduino code voor Aurea Warmtepomp
 *
 * Let WP work at <5 degrees Celsius. 
 * At 5 degrees a fixed 24Kohm resistor is switched. 
 * At 0 degrees the sensor of the WP is switched again. 
 * To prevent frost damage.
 */

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

const int RELAY_PIN  = A3; // Arduino pin connected to the relay's pin
const int SENSOR_PIN = 2;  // Arduino pin connected to DS18B20 sensor's DATA pin
const float TEMPERATURE_THRESHOLD = 5; // °C
const float TEMPERATURE_THRESHOLD_COLD = 0; // °C

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensor(&oneWire);  // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address 0x27 or 0x3F, 16 column and 2 rows

float temperature;
int relay_state;

void setup() 
{
  Serial.begin(9600);            // initialize serial
  pinMode(RELAY_PIN, OUTPUT);
  sensor.begin();               // initialize the sensor
  lcd.init();                   // initialize the lcd
  lcd.backlight();              // open the backlight 
}

void loop() 
{
  sensor.requestTemperatures();             // send the command to get temperatures
  temperature = sensor.getTempCByIndex(0);  // read temperature in Celsius

  // print to LCD
  lcd.setCursor(0, 0);          // start to print at the first row
  lcd.print("Temp: ");          // print Temp
  lcd.print(temperature);       // print the temperature in Celsius
  lcd.print((char)223);         // print ° character
  lcd.print("C");

  // print to serial
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°C => relay's state: ");
  Serial.println(relay_state);

  // Check temperatures, set relay state, print relay state to LCD
  if (temperature < TEMPERATURE_THRESHOLD)
    relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
    lcd.setCursor(0, 1);
    lcd.print("T<5");
    lcd.print((char)223);
    lcd.print("C Relay: ON ");
  if (temperature < TEMPERATURE_THRESHOLD_COLD)
    relay_state = LOW;  // Sensor WP active frost protection
    lcd.setCursor(0, 1);
    lcd.print("T<0");
    lcd.print((char)223);
    lcd.print("C Relay: OFF");
  if (temperature > TEMPERATURE_THRESHOLD)
    relay_state = LOW;  // Sensor WP active
    lcd.setCursor(0, 1);
    lcd.print("T>5");
    lcd.print((char)223);
    lcd.print("C Relay: OFF");
    
  digitalWrite(RELAY_PIN, relay_state);  // control the relay
  
  //Wait 1 minute for new reading
  delay(60000);
}

I tried an IF statement on the LCD part.:

// Check temperatures and set relay state
  if (temperature < TEMPERATURE_THRESHOLD)
    relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
    {if (relay_state = HIGH) {lcd.setCursor(0, 1);
    lcd.print("T<5");
    lcd.print((char)223);
    lcd.print("C Relay: ON ");}}
  if (temperature < TEMPERATURE_THRESHOLD_COLD)
    relay_state = LOW;  // Sensor WP active frost protection
    {if (relay_state = LOW) {lcd.setCursor(0, 1);
    lcd.print("T<0");
    lcd.print((char)223);
    lcd.print("C Relay: OFF");}}
  if (temperature > TEMPERATURE_THRESHOLD)
    relay_state = LOW;  // Sensor WP active
    {if (relay_state = LOW) {lcd.setCursor(0, 1);
    lcd.print("T>5");
    lcd.print((char)223);
    lcd.print("C Relay: OFF");}}
    
  digitalWrite(RELAY_PIN, relay_state);  // control the relay
  
  //Wait 1 minute for new reading
  delay(60000);
}

But now the relay is not switching
The LCD now stays at: T < 5C Relay: ON

:face_with_raised_eyebrow:

Maybe it helps if you use { and } for the if blocks?

Start by using tools / auto format. A block like

  // Check temperatures, set relay state, print relay state to LCD
  if (temperature < TEMPERATURE_THRESHOLD)
    relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
    lcd.setCursor(0, 1);
    lcd.print("T<5");
    lcd.print((char)223);
    lcd.print("C Relay: ON ");

becomes

    // Check temperatures, set relay state, print relay state to LCD
    if (temperature < TEMPERATURE_THRESHOLD)
      relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
    lcd.setCursor(0, 1);
    lcd.print("T<5");
    lcd.print((char)223);
    lcd.print("C Relay: ON ");

Based on the indentation, only the statement relay_state = HIGH; depends on the if(condition).

Change that block to include all statements that depend on the if(condition).

    // Check temperatures, set relay state, print relay state to LCD
    if (temperature < TEMPERATURE_THRESHOLD)
    {
      relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
      lcd.setCursor(0, 1);
      lcd.print("T<5");
      lcd.print((char)223);
      lcd.print("C Relay: ON ");
    }

Repeat for your other blocks and see if it asolves the issue.

Hi, @marc_nl
Welcome to the forum.

Thanks for using code tags. :+1:

https://docs.arduino.cc/language-reference/en/structure/control-structure/if/

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

I see what you mean, thx!
So that may be the case.

I will try your suggestion. :+1:t2:
Finetune my code to be better blocks and add the } for the if statements. :face_with_monocle:

Thx, for the link.
I will dig further.

Let you guys know the outcome.

Have you some previous experience coding in Python?

In Python, indentation implies the start and end of a code block. So for example

In Python, all those lcd.xxx() functions would be executed only if the condition was true, because they all have the same indentation.

In C/C++, indentation has no effect. If you want to group some code lines together and execute all of them based on the if condition, you have to put the code lines inside { and }.

Never did coding in Python also not in C/C++. So this was a good tip that the indentation is an important part of the programming. Thx for your explanation.

Okay I changed the coding and added the indentation as mentioned by @sterretje
It solved the issue :grinning: :+1:t2:

I marked the reply as solution.

Thanks a lot for all your comments and support :smiley:

This is the coding including the solution, maybe it can be better regarding layout but happy with the results. Last night the relay was activated with the associated LCD message.

/*
 * Arduino code voor Aurea Warmtepomp
 *
 * Let WP work at <5 degrees Celsius. 
 * At 5 degrees a fixed 24Kohm resistor is switched. 
 * At 0 degrees the sensor of the WP is switched again. 
 * To prevent frost damage.
 */

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

const int RELAY_PIN  = A3; // Arduino pin connected to the relay's pin
const int SENSOR_PIN = 2;  // Arduino pin connected to DS18B20 sensor's DATA pin
const float TEMPERATURE_THRESHOLD = 5; // °C
const float TEMPERATURE_THRESHOLD_COLD = 0; // °C

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensor(&oneWire);  // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address 0x27 or 0x3F, 16 column and 2 rows

float temperature;
int relay_state;

void setup() 
{
  Serial.begin(9600);            // initialize serial
  pinMode(RELAY_PIN, OUTPUT);
  sensor.begin();               // initialize the sensor
  lcd.init();                   // initialize the lcd
  lcd.backlight();              // open the backlight 
}

void loop() 
{
  sensor.requestTemperatures();             // send the command to get temperatures
  temperature = sensor.getTempCByIndex(0);  // read temperature in Celsius

  // print to LCD
  
  lcd.setCursor(0, 0);          // start to print at the first row
  lcd.print("Temp: ");          // print Temp
  lcd.print(temperature);       // print the temperature in Celsius
  lcd.print((char)223);         // print ° character
  lcd.print("C");

  // print to serial
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°C => relay's state: ");
  Serial.println(relay_state);

  // Check temperatures, set relay state, print relay state to LCD
      if (temperature < TEMPERATURE_THRESHOLD)
      {
        relay_state = HIGH;  // 24Kohm active WP operates at fixed 5 degrees
        lcd.setCursor(0, 1);
        lcd.print("T<5");
        lcd.print((char)223);
        lcd.print("C Relay: ON ");
      }

      if (temperature < TEMPERATURE_THRESHOLD_COLD)
      {
        relay_state = LOW;  // Sensor WP active frost protection
        lcd.setCursor(0, 1);
        lcd.print("T<0");
        lcd.print((char)223);
        lcd.print("C Relay: OFF");
      }
      
      if (temperature > TEMPERATURE_THRESHOLD)
      {
        relay_state = LOW;  // Sensor WP active
        lcd.setCursor(0, 1);
        lcd.print("T>5");
        lcd.print((char)223);
        lcd.print("C Relay: OFF");
      }
    
  digitalWrite(RELAY_PIN, relay_state);  // control the relay
  
  //Wait 1 minute for new reading
  delay(60000);
}

Good to hear your code is working, but you have not understood something important.

Indentation has no effect on C/C++, as I mentioned. Indentation means the number of spaces or tabs to the left of each line of code.

It was adding the { and } that solved your issue, not the indentation.

Indentation has no effect on C/C++ but that does not mean it is not important. It greatly helps with the readability of code, which makes it easier for us humans to understand and spot errors in the code.

If you use the Auto Format option in the Tools menu in the IDE, that will correct any indentation errors in your code. It uses the { and } to know what the correct amount of indentation should be for each code line. If you go back to your original code from post #1 and Auto Format that, you will see the effect.

I see, good to know.
Added the {} in the original code.
After the autoformat it improves a lot the layout.

1 Like