Arduino freezing when motor is installed

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);
}

12V power supply amp rating?

its 2A for the 12V.

and 1A for the 9V.

I started with the motor on the 9v and the arduino on the usb and then tried the 12v for motor and 9v for arduino. But same problem

According to the schematic the motors are getting 5V.

You may want to review the current rating of a 2N2222, 800mA or so on the best of days.


oIc the motor is a 5V model.

I am using a 12V AC adaptor into an "Elegoo power MB" to get it down to 5V for the motor.
Sorry, I didnt put this in the schematic. I am not sure what this would look like and thought it.

Elegoo power MB

What is it? How much current can it supply? What is the stall current of the pump?

It is a power supply module with maximum output current is 700mA.
Unfortunately dont have any info for the pump. Maybe shouldnt have bought cheap from Amazon.

It it looks like this:

Elegoo Power MB.PNG

Idahowalker:
According to the schematic the motors are getting 5V.

You may want to review the current rating of a 2N2222, 800mA or so on the best of days.


oIc the motor is a 5V model.

I have also tried it with a fairchild SS8050 transistor with a maximum collector current of 1.5A which got the same results

Elegoo Power MB.PNG

I dont have any real details for the pump but I have measured the current.

As it starts up the maximum current I see is 150mA and it drops to about 100mA while running. When the arduino freezes I dont see any larger values.

The pump isnt under any load.

When I block the pump so the motor cant spin it reads around 500mA

Nick8529:
It is a power supply module with maximum output current is 700mA.

What the fancy websites don't tell you is that that 700mA can only be drawn with 6volt on the DC socket.
With 12volt in (7volt drop), that current rating drops to 100mA.
Leo..

The kit that this came with has datasheets for nearly everything except this.

So it could be my power supply that is the issue. This might be a stupid question but why would the arduino care about the power supply for the motor. Would it not just keep running the program and sending the signal to the transistor when required and just the motor would be affected by the supply (with what ever happens when the current is exceeded)?

Also I was trying to seen what people tend to do with power supplies but couldnt really find an answer. I want to make this into an stand alone unit so will need a power supply for the arduino and a seperate power supply for the motor but wouldnt really want to have 2 seperate plugs to the mains. How to people go about this?