Hi,
I am new to arduino and this forum. I got an Elegoo Mega 2560 r3 a few weeks ago and I am now trying to make my first useful project, a plant waterer.
This plant waterer will measure the soil moisture as well as temperature and light levels and when the moisture in the soil gets too low it will turn a pump on for a set amount of time. I have attached the DS1307 RTC module and an SD card module so that the values can be recorded with a date and time.
I initially set it all up without a pump attached and had the serial monitor tell me when the pump was turned on and off which all worked how I expected.
When I attach the pump it works well for a little bit but then the arduino will freeze, no more info will come through to the serial monitor and the pump stays on constantly. I have got it to work in 20sec intervals with the pump staying on for 3s just for testing.
I have searched this forum and other places to try and find a solution, and motor interference seems to be a common issue. I have placed a 0.1uF capacator accross the pump terminals but this didnt help.
I have the arduino powered by the 9v AC adaptor and the motor by a 12v AC adaptor into a voltage regulator down to 5v with the grounds connectected. The LCD is from the arduino supply which I have placed a button on the backlight. I have noticed that when this is switched on, the motor runs slower. I am no electrical expert but I wouldnt have thought this would happen if they are on 2 supplies.
Any help with this would be appreciated. My code and schematic are below, I am new to all of this so sorry if these arent great.
The items I have been using are:
- temp sensor: Dallas DS18B20
- photosensitive resistor: KLS6-3537
- LCD: longtech LCM1602A
- transistor: PN2222
- diode: 1N4004
- moisture sensor: here
- pump: here
// Temperature and light levels measured at same time. Moisture (hydro) measured at different time
// LCD Display added
// Pump Added
// ========== Lower moisture level percentage ==========
const byte lowerLimit = 40;
// ========== Dallas DS18B20 Temperature Sensor info ==========
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5 //temperature pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celcius = 0;
// ========== Real Time Clock info ==========
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
// ========== LCD info ==========
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // might need changing or others changing to fit
// ========== Timings ==========
unsigned long TLStartMillis;
unsigned long hydStartMillis;
unsigned long pumpStartMillis;
unsigned long currentTLMillis;
unsigned long currentHydMillis;
unsigned long currentPumpMillis;
const unsigned long TLPeriod = 15000; // temperature and light intervals
const unsigned long hydPeriod = 20000; // moisture sensing intervals
const unsigned long pumpPeriod = 3000; // pump running time
#define hydPin A2 // moisture sensing pin
#define lightPin A3 // light level pin
#define pumpPin 3 // pump pin
// ========== SD Card info ==========
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 53;
// ========== Boolean for if the lower moisture limit has been reached ==========
bool limitReached = false;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // LCD
sensors.begin(); // Dallas Temperarture Sensor
clock.begin(); // Real Time Clock
clock.setDateTime(__DATE__, __TIME__);
/*
Tips:This command will be executed every time when Arduino restarts.
Comment this line out to store the memory of DS3231 module
*/
pinMode(hydPin, INPUT);
pinMode(lightPin, INPUT);
pinMode(pumpPin, OUTPUT);
TLStartMillis = millis();
hydStartMillis = millis();
// SD Card
pinMode(pinCS, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
myFile = SD.open("tempL.txt", FILE_WRITE);
myFile.close();
myFile = SD.open("moist.txt", FILE_WRITE);
myFile.close();
}
void loop() {
dt = clock.getDateTime();
temperatureLight(); // function to record temperature and light levels
moistureLevels(); // function to record moisture levels and turn pump on if required
lcd.setCursor(0, 0);
lcd.print(" Temp Light Moi");
lcd.setCursor(4, 1);
lcd.print("C");
lcd.setCursor(10, 1);
lcd.print("%");
lcd.setCursor(15, 1);
lcd.print("%");
}
void temperatureLight() { // function to record temperature and light levels
currentTLMillis = millis();
sensors.requestTemperatures();
Celcius = sensors.getTempCByIndex(0);
int light = map(analogRead(lightPin), 0, 1023, 0, 100);
myFile = SD.open("tempL.txt", FILE_WRITE);
if (currentTLMillis - TLStartMillis >= TLPeriod) // test if temp sensing interval has elapsed
{
myFile.print(dt.day); myFile.print("/"); myFile.print(dt.month); myFile.print(",");
myFile.print(dt.hour); myFile.print(":"); myFile.print(dt.minute); myFile.print(",");
myFile.print(Celcius, 1); myFile.print(",");
myFile.print(light); myFile.println(",");
TLStartMillis = currentTLMillis;
}
myFile.close();
lcd.setCursor(0, 1);
lcd.print(Celcius, 1);
lcd.setCursor(8, 1);
lcd.print(light);
}
void moistureLevels() { // function to record moisture levels and turn pump on
currentHydMillis = millis();
int moisture = map(analogRead(hydPin), 0, 1023, 100, 0);
myFile = SD.open("moist.txt", FILE_WRITE); // set up file to write moisture results
if (currentHydMillis - hydStartMillis >= hydPeriod) // test if temp sensing interval has elapsed
{
myFile.print(dt.day); myFile.print("/"); myFile.print(dt.month); myFile.print(",");
myFile.print(dt.hour); myFile.print(":"); myFile.print(dt.minute); myFile.print(",");
myFile.print(moisture); myFile.println(",");
if (moisture <= lowerLimit)
{ limitReached = true;
pumpStartMillis = millis();
}
hydStartMillis = currentHydMillis;
}
currentPumpMillis = millis();
if (limitReached == true) // if lower limit has been reached
{
digitalWrite(pumpPin, HIGH); // turn pump on
Serial.println("pump on");
if (currentPumpMillis - pumpStartMillis >= pumpPeriod) // test if temp pump interval has elapsed
{
digitalWrite(pumpPin, LOW);
Serial.println("pump off"); // once interval elapsed turn pump off
limitReached = false; // return limitReached off
}
}
myFile.close();
lcd.setCursor(13, 1);
lcd.print(moisture);
}