I am working on an auto top off and temperate monitoring for my fish tank. I am having some issues with the LCD screen bugging out (displaying garbage) when the relays activate. I did some research and that lead me to buy a "snubber." I have no idea where to place the snubber however. I created my circuit on circuits.io to represent what's going on with my code.
The switches are actually float switches but used normal switches on the drawing. I also have a temperature probe on pin 7.
I couldn't find the proper relay so this is how it's wired.
Wiring for Relay
grd -> Ground Rail on Breadboard
INT 1 -> pin 14
INT 2 -> pin 15
INT 3 -> pin 16
INT 4 -> pin 17
vcc -> 5v Rail on Breadboard
Here is the code for the project.
#include <OneWire.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
char tmpstring[10];
OneWire ds(DS18S20_Pin); // on digital pin 7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int DS18S20_Pin = 7; //DS18S20 Signal pin on digital 7
byte lowWaterPin = 8; // Goes HIGH when water below switch
byte emergencyWaterPin = 9; // Goes HIGH when water below switch
byte atoWaterPin = 10; // Goes LOW when water level in ATO container is low
int pumpPin = 14; // Use HIGH to turn OFF and LOW to turn ON
int unusedrelayPin = 15; // Currently not being used for the program, this outlet is already wired for future use
int heater1Pin = 16;
int heater2Pin = 17;
/* Wiring for Relay
grd red ground rail on breadboard
INT 1 blue aka pumpPin
INT 2 purple aka usedrelayPin
INT 3 grey aka heater1Pin
INT 4 white aka heater2Pin
vcc black positve rail on breadboard
*/
void setup() {
Serial.begin(9600);
pinMode(lowWaterPin, INPUT_PULLUP); // HIGH when float drops but pinMode inverses it
pinMode(emergencyWaterPin, INPUT_PULLUP); // HIGH when float drops but pinMode inverses it
pinMode(atoWaterPin, INPUT_PULLUP); // LOW when float drops but pinMode inverses it
pinMode(pumpPin, OUTPUT); // Relay to turn on and off the pump for the Auto Top Off
pinMode(heater1Pin, OUTPUT); // Relay to turn on and off Heater1
pinMode(heater2Pin, OUTPUT); // Relay to turn on and off Heater2
pinMode(unusedrelaypin, OUTPUT); // Currently not being used for the program, this outlet is already wired for future use
digitalWrite(unusedrelayPin, HIGH); // Turning off power to the relay
lcd.begin(16, 2);
}
void loop() {
float temperature = getTemp(); //get(Temp() on 2nd Tab, returns temp in F back to program
int tmp = (int) temperature; //stores getTemp()
int waterLevelState = digitalRead(lowWaterPin); //Primary Float Switch to control ATO system
int emergencyLevelState = digitalRead(emergencyWaterPin); //Secondary Float Switch for failsafe
int atoLevelState = digitalRead(atoWaterPin); //Float Switch to Monitor Water Level in ATO Reservoir
lcd.setCursor(0, 0);
lcd.print("Water Temp:");
lcd.print(tmp);
lcd.print("F ");
//Because of the PINMODE things are inverse of what we think
if ((waterLevelState == HIGH) || (emergencyLevelState == HIGH) || (atoLevelState == LOW)){
digitalWrite(pumpPin,HIGH);
Serial.println("ATO Pump is OFF");
lcd.setCursor(0, 1);
lcd.print("ATO OFF");
}
//Because of the PINMODE things are inverse of what we think
if ((waterLevelState == LOW) && (emergencyLevelState == LOW) && (atoLevelState == HIGH)){
digitalWrite(pumpPin,LOW);
Serial.println("ATO Pump is ON");
lcd.setCursor(0, 1);
lcd.print("ATO ON");
}
delay(2000); //Just a little delay so I can watch things happen easier
/////////////////////////////////////////
//////////Temperature Code///////////
/////////////////////////////////////////
if (tmp >= 81) {
//heaters need to turn off
digitalWrite(heater1Pin, HIGH); // audio alarm would be nice
digitalWrite(heater2Pin, HIGH);
Serial.println("High Temp");
}
if (tmp >= 76 && tmp < 81) {
//Usually just one heater is running to keep tank at ~78F
digitalWrite(heater1Pin, LOW);
digitalWrite(heater2Pin, HIGH);
Serial.println("Normal Temp");
}
if (tmp < 76) {
//Turning on both heaters because of low temp
digitalWrite(heater1Pin, LOW);
digitalWrite(heater2Pin, LOW);
Serial.println("Low Temp");
}
if (tmp=-1000){
// thermometer is having an error or not detected, heaters need to be off
digitalWrite(heater1Pin, HIGH);
digitalWrite(heater2Pin, HIGH); // audio alarm would be nice
Serial.println("TEMP ERROR!!!!");
}
/////////////////////////////////////////
//////////Temperature Code///////////
/////////////////////////////////////////
}
Second page for the GetTemp code
float getTemp() {
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return (TemperatureSum * 18 + 5) / 10 + 32;
}
///////////////////////////////////////////////////////////////////////////////////////