# How to control 12v dc motor (peristaltic pump) using function

**URL:** <https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141>\
**Category:** General\
**Tags:** node-red-dashboard\
**Created:** [25 August 2021 00:38 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141 "2021-08-25T00:38:01Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Ghoond](https://avatars.discourse-cdn.com/v4/letter/g/c57346/32.png) [@Ghoond](https://discourse.nodered.org/u/Ghoond)\
**Post date:** [25 August 2021 03:57 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/4 "2021-08-25T03:57:59Z")

</div>

So i have make the simple one, and this is the arduino code

```auto
int ml;
int nyala;
int input;
int dc_pin = D1;

void setup() {
  pinMode(dc_pin, OUTPUT);
  Serial.begin(115200);
}
  
void loop() {
  input = Serial.parseInt();
  ml = input/10;
  nyala = ml * 500;

  if(nyala == 500){
    digitalWrite(dc_pin, LOW);
  delay(500);
  MotorStop();
  }
  else if(nyala == 1000){
    digitalWrite(dc_pin, LOW);
  delay(1000);
  MotorStop();
  }
  else if(nyala == 2000){
    digitalWrite(dc_pin, LOW);
  delay(2000);
  MotorStop();
  }
}

void MotorStop(){
  digitalWrite(dc_pin, LOW);
  digitalWrite(dc_pin, LOW);
}

```

The ml is the formula to calculate how many liquid must use following the input.  
And nyala is how long the peristaltic pump have to turn on.

---

_[View the full topic](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141)._
