Arduino liquid dispenser project keeps suddenly resetting

I'm trying to make a liquid dispenser that allows the user to input the amount they would like it to dispense. I'm using a 12V 1A diaphragm pump and a 12V external power supply. For some reason, while the pump is programmed to turn on to dispense a certain amount of liquid, it suddenly stops even before the set time for the pump to open and resets the entire thing. Where did I go wrong?

This is the schematic diagram for it.

This is the code I used for it.

#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

int TIP120pin = 2;

LiquidCrystal_I2C lcd(0x27,16,2);

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {10, 9, 8, 7}; 
byte colPins[COLS] = {6, 5, 4, 3}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
char customKey;

String amtStr = "";
byte column = 0;
int amt;


void setup()
{
  Serial.begin(9600);
  pinMode(TIP120pin, OUTPUT); 
  analogWrite(TIP120pin, 0);
  
  lcd.init();
  lcd.clear();         
  lcd.backlight();  
  
  //print instructions
  lcd.home();
  lcd.print("How much liquid");
  lcd.setCursor(0,1);
  lcd.print("do you want?");
  delay(3000);
  lcd.clear();
  lcd.home();
  lcd.print("min:200 max:1000");
  lcd.setCursor(0,1);
  lcd.print("Press D to enter");
  delay(5000);
  lcd.clear();
}
 

void loop()
{  
  //print guiding text  
  lcd.home();
  lcd.print("amount (in mL):");
  lcd.setCursor(0,1);
  
  customKey = customKeypad.getKey();
  //user enters amount
  if (customKey) {
    if(customKey == 'D'){
      Serial.println("user has entered amount");
      Serial.println("amount contained in amtStr is");
      Serial.println(amtStr);
      amt = amtStr.toInt();
      Serial.println("calculated amount is");
      Serial.println(amt);
      openPump();
      Serial.println("left openPump function");
      clearData();
      Serial.println("clearData function executed");
    }
      
    else {
      amtStr += customKey;
      Serial.println("amtStr currently contains");
      Serial.println(amtStr);
      lcd.setCursor(column, 1);
      lcd.print(customKey);
      column++;
    }
  } 
}

void openPump (){
  Serial.println("openPump function was called");
  float flowRateRaw =  1.63; //in L/min
  float flowRate = (flowRateRaw * 1000) / 60; //convert to mL/sec
  Serial.println("calculated flow rate is");
  Serial.println(flowRate);
  float time = amt / flowRate;
  int time_int = time;
  float time_dec = time - time_int;
  Serial.println("calculated time to open is");
  Serial.println(time);
  Serial.println("calculated time_int is");
  Serial.println(time_int);
  Serial.println("calculated time_dec is");
  Serial.println(time_dec);
  analogWrite(TIP120pin, 255);
  Serial.println("pump turned on");
  Serial.println("timer...");
  for (float i = 1; i <= time_int; i++){
    delay(1000);
    Serial.println(i);
  }
  delay(time_dec*1000);
  Serial.println(time);
  //delay(time * 1000);
  analogWrite(TIP120pin, 0);
  Serial.println("pump turned off");
  
}

void clearData (){
  amtStr = "";
  amt = 0;
  column = 0;
  lcd.clear();
}

The diode should be across the motor not in series with it.

You also need decoupling capacitors on the other parts of your circuit.

See:-
De-coupling tutorial

It doesn't look like D1 is over the motor contacts.

Ah, Grumpy_Mike did beat me to it.

you mention the power source uis 12v, what is rated to supply ?

In the actual project, I placed 8 1.5V batteries in series.

AA size or?
Is arduino powered from same supply?
Is your transistor rated for >1A?

Thank you for the reply! In the actual project, I do have it in series. The cathode is connected to the collector and the anode is connected to the emitter. May I also ask which parts of the circuit in specific need decoupling capacitors?

Yes, I used an AA size. The Arduino is powered with my laptop. The transistor I used is the TIP120 which apparently has a max Ic of 5A.

Read post#2
The diode across the pump +/- contacts.

The diode should be in parallel with the pump motor, not the transistor, connect diode cathode to 12V+, anode to transistor collector.

This is how my setup looks like currently.


Also route the pump wires a way it doesn't run together/next to other wiring.

And the 12V negative should be connected directly to the TIP120 emitter with a jumper to Arduino GND.

Yes but only by one second! :nerd_face:

On every external component or board that doesn't have any on board. Did you also see the specific circuit for motor supplies at the end of the tutorial. The one with a small resistor as opposed to an inductor should be tried first.

Good first post, too!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.