Relay switching corrupts 16x2 arduino LCD screen

So my friend and i have been trying to create an incubator with an arduino; we got a DS18B20 temp sensor, AMT1001 sensor for humidity and a LCD to display both values. A button was even added to display the time elapsed.

Once we've got those working, we started to hook up a Tongling Relay that switched a lamp on when the temperature is below 37.5 C...

The setup works fine for a while, until at some random point in the switching process it simply fails and the screen is all corrupted. Showing weird characters. Once you reset it, it's working fine again. We have no idea why this happens and even tried it with a 5v and a 12v adapter because we thought it might've been an issue with the power. Sadly that didn't work. Maybe we need to add a capacitor somewhere so the spike in the switching of the relays doesn't screw up the LCD ? Wouldn't know where exactly to put it though.

Appreciate any input.

Sketch:

Code:

/********************************************************************/
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h> // include the library code
#include <TimeLib.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 10
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonPin = 13;
 
time_t elapsedTime;
 
int RHpin = A0;
float RH = 0;
int buttonState = 0;
 
void setup(void)
{
  //heating module is connected via relay on pin 6
  pinMode(6, OUTPUT);
 
  //button for time elapsed display
  pinMode(buttonPin, INPUT);
 
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  lcd.noAutoscroll();
 
  sensors.begin();
}
 
void loop(void)
{
 
 // call sensors.requestTemperatures() to issue a global temperature
 // request to all devices on the bus
/********************************************************************/
 sensors.requestTemperatures(); // Send the command to get temperature readings  
/********************************************************************/
   float temp = sensors.getTempCByIndex(0);
   elapsedTime = now();
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire
   
   RH = analogRead(RHpin);
   RH = (((RH*5)/1023)*100)/3;
 
   //Temperature can't go below 37.5, so the lamp needs to be on until it reaches temperatures above 37.5
   if(temp<37.5){
   digitalWrite(6, LOW); // Relay connects power
   }else{
   digitalWrite(6, HIGH); // Relay OFF
   }
 
   //TODO: Relay for Humidity fan
   //TODO: Relay for SERVO motor for  turning eggs (timed)
   buttonState = digitalRead(buttonPin);
 
   //if button pressed, show elapsed time in D:H:M:S
   if (buttonState == HIGH){
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("D:");
     lcd.print(elapsedTime / SECS_PER_DAY);
     lcd.print(" H:");
     lcd.print(hour(elapsedTime));
     lcd.print(" M:");
     lcd.print(minute(elapsedTime));
     lcd.print(" S:");
     lcd.print(second(elapsedTime));
     
   }else{
    //Otherwise we're just displaying the current temperature and humidity
     lcd.setCursor(0,0);
     lcd.print("Temp.   : ");// (6 char)
     lcd.print(temp); // Why "byIndex"?  (5 char)
     lcd.print("c");
     lcd.setCursor(0,1);
     lcd.print("Humidity: ");//10 char
     lcd.print(RH);//5? char
     lcd.print("%");
   }
     
   delay(300);
}

The problem:

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code on an offsite resource. Many people avoid following such links for security reasons, and also because it's easier to read online when posted with code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Edited the post to have the code embedded.

Hi,

This is a common problem. Some ElectroMagnetic Interference.

How is this all packaged? Where is system ground? Are the relay(s) optically isolated?

Some help here:

http://arduino-info.wikispaces.com/RelayIsolation

and this:

http://arduino-info.wikispaces.com/Arduino-Project-Planning-Electrical

Let us know what you figure out...

Have you tried running the sketch with the relay disconnected? Then with the lamp load disconnected?

Hi.
Ops pics


Tom... :slight_smile:

@terryking228

Holy crap. I mean, i have honestly no idea where to start or stop with that. Most of the wiring is done by either soldering or the standard wires that you put on a breadboard; there's not that much power going through it but considering the system is not earthed through the power plugs, i guess that's a start?

Both of them are type C plugs (a 5v USB plug -> arduino and a 12v center positive Adapter -> Breadboard powersupply). How would you recommend grounding an arduino where the plugs are Type C and (this house has no proper ground other than in the kitchen, i believe)?

@aarg

"Have you tried running the sketch with the relay disconnected?"
Yes, with the whole relay disconnected from the whole system it works fine, as soon as the relay gets connected and it starts switching is when the problems occur.

"Then with the lamp load disconnected?"
No, I haven't actually. Sounds like another thing to test, though considering it's on a different circuit, i doubt it's gonna change much?

@tom
thanks, edited those to show as well

@aarg

So, just tested it and it looks like it works fine with the lamp DISCONNECTED...
Does that mean that the relay is bad or is there another explanation?

Waswat:
"Then with the lamp load disconnected?"
No, I haven't actually. Sounds like another thing to test, though considering it's on a different circuit, i doubt it's gonna change much?

That doubt is not as great as the doubt about why it fails. You don't have anything to lose by trying it. It will help narrow it down to the relay circuit or else load.

Waswat:
@anon57585045

So, just tested it and it looks like it works fine with the lamp DISCONNECTED...

Aha! You need to start answering the questions posed in reply #3.

Aye! And as a total noob when it comes to electrical engineering, i have NO clue where to go from there? Switch out relays, try another lamp?

Waswat:
Aye! And as a total noob when it comes to electrical engineering, i have NO clue where to go from there? Switch out relays, try another lamp?

But you didn't answer the questions in reply #3....

Also I'm concerned about your electrical safety.

I did... see #6. What else do you want me to add?

Looks like it was fixed by using a 12v lamp rather than the usual 220v; still a bit of an issue as it's gonna be more difficult to heat up with a lower voltage for an incubator but i'll see what i can do with it.

Hi,
Can you post a picture of your project so we can see your component layout?

The placement of wiring and wire gauge has a lot to do with circuit stability.

Thanks.. Tom... :slight_smile: