lcd problem..

Im building a system to control the humidity in a glass tank through a humidity device. Im trying to on/off a 220V humidity device and when the device passed a desired humidity to turn off the device through a relay. The system looks that working fine but sometimes when the relay switches off the device, the lcd starts to print random characters but the program run fine(it still control the device if the humidity exceed the desired value). I cant understand the problem cause the most of the time that relay switches off the device the lcd wotking fine. Also when the lcd starting to print random characters if i press reset after this the lcd works fine again… im posting also the code if anyone can help… (also the connection of the relay is same with this http://www.glacialwanderer.com/_blog/blog2008/04_April/hb_relay5.jpg but instead of the 1N4004 diode i use a 1N4007 and istead of the 2N2222 transistor i use a 2N3904 )

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int tempPin = 0; // analog 0 ,LM35 pin
const int humPin = 1; // analog 1 , HIH-4030 pin 
const int lightPin = 2; // analog 2 , light sensor 
const int potPin = 3; // analog 3 , potensiometro 
const int ledPin = 6; // Led pin output 6
const int output = 7; //relay pin 7
const int buttonPin = 8; //input button pin 8

float tempC = 0; 
float hum = 0;
float light = 0;
int pot = 0;
int buttonstate = 0;
float dhum = 0; //epithimith sxetikh ygrasia
float samples[8];
int i;

void setup() {
  
  pinMode(tempPin, INPUT);
  pinMode(humPin, INPUT);
  pinMode(lightPin, INPUT);
  pinMode(potPin, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(output, OUTPUT);
  
  Serial.begin(9600); //gia na fugei meta to test!!!
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(4, 0);
  lcd.print("Welcome");
  lcd.setCursor(0, 1);
  lcd.print("Humidity control");
  delay(1500);
  }

void loop() {
 
    while (digitalRead(buttonPin) == HIGH) {
     sethumidity(); 
    }
 

   //diavasma thermokrasias
  for(i = 0;i <= 7;i++){ // gets 8 samples of temperature
  samples[i] = ( 5.0 * analogRead(tempPin) * 100.0) / 1024.0;
  tempC = tempC + samples[i];
  buttonstate = digitalRead(buttonPin);
  if(buttonstate == HIGH) {
    i=7;
  }
  delay(500);  
  }
  tempC = tempC/8.0;
    

  Serial.print((byte)tempC); //gia na fugei meta to test!!!
 
  //diavasma ygrasias
  hum = analogRead(humPin);
   hum = hum*5/1024;
  hum = (hum - 0.958)/0.031;
  Serial.print((byte)hum); //gia na fugei meta to test!!!
  
 
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.write(B01001000);
  lcd.setCursor(1, 0);
  lcd.print("umidity =");
  lcd.setCursor(11, 0);
  lcd.print(hum);
  lcd.setCursor(15, 0);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("Temp =");
  lcd.setCursor(7, 1);
  lcd.print(tempC);
  lcd.setCursor(13, 1);
  lcd.write(B11011111);
  lcd.setCursor(14, 1);
  lcd.print("C");
  tempC = 0;
  //diavasma fwtoantistashs
  light = analogRead(lightPin);
  
  if(light<300) 
  {
    dhum = pot +20; //auksish ugrasias to vradu kata 20%
  }
  else
  {
    dhum = pot;
  }
  
  if(hum <= dhum) {
    digitalWrite(output, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else if (hum >= dhum+20)
  {
    digitalWrite(output, LOW);
    digitalWrite(ledPin, LOW);
  }
  

}
void sethumidity() {
  
  pot = analogRead(potPin);
  pot =  pot/10.24;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("set humidity");
     lcd.setCursor(0, 1);
  lcd.print(pot);
  lcd.setCursor(2, 1);
   lcd.print("%");
  delay(100);
}

Probably a noise problem: when you switch the humidity device, the power to the LCD is getting a noise spike, or the LCD itself is seeing a noise spike.

First, change whatever you are powering the Arduino and LCD. Use a wall wart instead of USB, or USB instead of a wall wart, and see if that changes anything. Physically separate the Arduino and the LCD from the relay and the humidity device.

Try adding more bypass capacitors to the LCD power. Use a 100uF electrolytic with the pins right on the LCD power pins. Use a bigger gauge wire on the power & ground connections. Try putting the arduino and the LcD in a metal box with a small opening for the lcd, and ground the box.

Thank you for your reply. I will try to put 100[ch956]F in the + - lcd pins. If the thing that you told me to do dont work, is there any command to put it on the void loop to re-initialize the lcd?!

Garbled characters on the LCD is normal when you hit the reset button on the arduino.

I think you didnt read carefully what ive wrote(or cause of my bad english =p) Anw i didnt get garbled chars when i hit the reset but when the relay switch on/off

Garbled characters on the LCD is normal when you hit the reset button on the arduino.

But he says that "if i press reset after this the lcd works fine again" so that's not his problem. By the way if you DO get garbled characters after reset they should only be there for a second or two - until the LCD initialization code runs.

Don

Try adding more bypass capacitors to the LCD power. Use a 100uF electrolytic with the pins right on the LCD power pins.

I agree about trying bypass capacitors, but they are usually much smaller - typically 0.01 uF. It doesn't hurt to use both.

Don

First, change whatever you are powering the Arduino and LCD. Use a wall wart instead of USB, or USB instead of a wall wart, and see if that changes anything. Physically separate the Arduino and the LCD from the relay and the humidity device.

You missed one combination here. Power the LCD and Arduino with one supply and the relay (and transistor) with another. Don't forget to connect the grounds together.

Don

The size of the bypass depends on the frequency and duration of the problem. A .01 is going to help you with high frequency noise. 100uf is going to help you with low frequency noise. In this case, we may have power "sag" due to the fact that the humidity device is sucking a ton of power when it turns on. What I'm suspecting is that the power to the LCD is seeing this sag, and reacting badly. What the big capacitor does in this case is supply power to the LCD through the sag.

If the problem was RF noise, then .01 is good.

You often see both. On a board, you often use .01 (or sometimes .1) per chip, and then a 2.2 or 3.3 per 4 or 5 chips. That is used when you have a lot of high frequency, relatively high power switching noise. This is not that kind of problem.

Be certain there is a diode across the coil of the relay. This is always a Very Good Idea. (The diode should be connected, relative to how the voltage is connected, so that it will "never conduct"... in simple theory. See "collapsing fields/ coils" if you want the advanced theory.)