LCD errors/clears when relay switches.

I have 2 relay setup and an LCD screen to display the pin status of the relay which is controlled by a temperature/humidity sensor.

When the relay switches (i believe when it switches off) the LCD screen some times displays partial gobbledygook & some times goes completely blank (back light stays on)

Does any one have any idea why this might happen?

#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 6  
#define DHTTYPE DHT22   
#define heaterpin 8
#define fanpin 9
#define humpin 10
#define arrsize 40
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  float h;
  float t;
  int avgTemp;
  int avgHum;
  int timespass = 360;
  int tempArr[arrsize];
  int humArr[arrsize];
  int arrFill;
  int hum;
  int temp;

void setup() {

  Serial.begin(9600); 
  dht.begin(); /// start temp sensor etc
  pinMode(heaterpin, OUTPUT);   
  pinMode(fanpin, OUTPUT);   
  pinMode(humpin, OUTPUT);
  lcd.begin(16, 2); 

}


void loop() {  
  
  h = dht.readHumidity();
  t = dht.readTemperature();
  hum = h;
  temp = t;
  
  checkHumidity(hum);
  checkTemp(temp);

  writeToArray();
  doAverages();
  
  writeToLCD();
  delay(5000);
  
}

void writeToArray(){
  
  timespass = timespass + 1;
  
  if(timespass > 360){   //720
    arrFill = arrFill + 1;
     if(arrFill == arrsize){
       arrFill = 1;
     }   
     

     humArr[arrFill] = hum;
     tempArr[arrFill] = temp;
     timespass = 0;
  } 
  
  
  
}

void doAverages(){

  avgTemp = 0;
  avgHum = 0;
  for(int x = 0; x <= arrFill; x = x + 1){
    
    avgTemp = avgTemp + tempArr[x];
    avgHum = avgHum + humArr[x];
    
  }
  
  avgTemp = avgTemp / arrFill;
  avgHum = avgHum / arrFill;
  
}

void writeToLCD(){
    
    lcd.setCursor(0, 1); 

    if(digitalRead(heaterpin) == LOW){
      lcd.print("T:On");
    }else{
      lcd.print("T:Off");
    }
    
    if(digitalRead(humpin) == LOW){
      lcd.print(" H:On");
    }else{
      lcd.print(" H:Off");
    }
      
    if(digitalRead(fanpin) ==LOW){
      lcd.print(" F:Off");
    }else{
      lcd.print(" F:On");
    }
  
  lcd.setCursor(0, 0);  
  lcd.print(temp);
  lcd.print("c");
  lcd.print(" ");
  lcd.print(hum);
  lcd.print("%  "); 
  lcd.print(avgTemp);
  lcd.print("c");
  lcd.print(" ");
  lcd.print(avgHum);
  lcd.print("%");
  
}

void checkHumidity(int hum1){
  
  if(hum1 < 90){
    digitalWrite(humpin,LOW); 
    //digitalWrite(fanpin,LOW);  
  }else if(hum1 > 90){
    digitalWrite(humpin,HIGH);
    //digitalWrite(fanpin,HIGH); 
  }
   
}

void checkTemp(int temp1){
  
  if(temp1 < 24){
    digitalWrite(heaterpin,LOW);  
  }else if(temp1 > 27){
    digitalWrite(heaterpin,HIGH);    
  }
  
}

Does any one have any idea why this might happen?

Electromagnetic interference. It is a common problem with a really simple solution. A 5 cent diode.

PaulS:
Electromagnetic interference. It is a common problem with a really simple solution. A 5 cent diode.

Please expand on this, I would love you to solve my problem. It has been playing on my mind for weeks why I cannot seem to eradicate the issue.

Where would I put the diode? Do I need a certain type?

Where would I put the diode?

Grumpy Mike has a web page with pictures. I'm sure you could search for it. The diode goes between the Arduino and the relay, so voltage spikes when the relay fires don't get back to the Arduino.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

How are you powering the relays?

Tom..... :slight_smile: