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: