# 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:** 18\
**Page:** 1

<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 00:38 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/1 "2021-08-25T00:38:01Z")

</div>

Hello node-red community, i'm new to node-red here, and i'm still learning. I'm working my project for my final exam. So im building a device that can control ph and tds of the water, i'm already can control the sensor and send the data to node-red, and now i want to control the peristaltic pump, and controlling it based on the input.

My flow looks like this

 ![node-red flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b4abeaed3b445bdcece15ea9a68fb1ca787be1c.png)

So the input will trigger the peristaltic pump for how long it turn on based on how many number from the inputs are.

And the input is flexible, it can be text, slider or numeric.

Thank you for your time to answering my question.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 August 2021 01:39 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/2 "2021-08-25T01:39:58Z")

</div>

Hi and welcome.

**I** don't understand the question.

However.....

You get your _input_ form the `TDS` MQTT node on the left.  
And below it you have a `PH` MQTT input.

You want to control the pump depending on....... ????

PH is the Acid level if I remember correctly.

TDS... (blank)

But I think you may need to get the _formula_ - or make one - which will give you what you want.

Another option is a _state machine_ which may better serve your needs.  
But they are way way beyond my understanding.

Good luck.

Oh, the three `TDS OUT` nodes... You can use just one and connect all the 3 other nodes to the one `TDS OUT` node.

---

<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 02:22 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/3 "2021-08-25T02:22:33Z")

</div>

Thanks for replying

Well i was going to control the peristaltic pump based on the input. for example if the user input 300 for the tds then the peristaltic pump will turn on. And if the inputs are 500 or above the peristaltic pump turn on a bit longer.

TDS is total dissolved solids, its basically to measure how many minerals contained in the water.

As for the three TDS OUT nodes, its just copypaste from the first one.

I will try to make a simple formula first.

---

<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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [25 August 2021 09:24 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/5 "2021-08-25T09:24:21Z")

</div>

> [@Ghoond](#):
>
> As for the three TDS OUT nodes, its just copypaste from the first one.

Then you need to change that in the image you have supplied. Since you didn't supply the flow how would anyone else know that they all are not being sent back.

It aslo looks like you are using an Arduino device to actually turn on/off the pump so this is really an Arduino, not NodeRED question...correct?

This is not an Arduino forum, but In your arduino code why are you bothering to multiply `ml * 500` ? since you are not using it in the code (unless you have more code you are not showing)

What happens if `nyala` does not equat 500, 1000 or 2000?

If you want to have Node-RED control the pump, why not put the logic in NR to determine it the pump should be on or off and just send an ON/OFF msg to the Arduino code (which will have to be adjusted to handle it)

---

<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 11:20 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/6 "2021-08-25T11:20:44Z")

</div>

Yes sir, i've change the flow, after reading your reply.

Yes sir, the code above is arduino code. My plan is to control the pump based on how much the user input the number on the dashboard, but since i'm still a beginner to node-red i thought it will work to apply it to node-red function flow.

About the

> What happens if `nyala` does not equat 500, 1000 or 2000?

I also thinking what if the user will specifically input 245 to the dashboard, but still confuse about the formula.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [25 August 2021 11:42 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/7 "2021-08-25T11:42:48Z")

</div>

> [@Ghoond](#):
>
> About the
> 
> > What happens if `nyala` does not equat 500, 1000 or 2000?
> 
> I also thinking what if the user will specifically input 245 to the dashboard, but still confuse about the formula.

At first glance it seems that you're running the pump for a time specified in the variable "nyala", so the simple answer is to just use

` delay(nyala);`

instead of the different choices in the various if ... else blocks. However you should also think about limits on the time you want to run the pump for, e.g. to prevent contamination of the tank from over-dosing with whatever is being added.

---

<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 14:28 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/8 "2021-08-25T14:28:22Z")

</div>

Okay i will try that, but does it can be applied on node-red? for example can i put it inside the function flow, so that when the inputs are filled the pump will turned on.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [25 August 2021 15:03 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/9 "2021-08-25T15:03:18Z")

</div>

The function could easily be converted from C++ to JavaScript and used in a function node, but how it controls the pump will depend on what type of platform you're running Node-RED on, and how the pump is connected to it.

Can you give us any more information about your setup and system design?

---

<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:** [26 August 2021 04:53 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/10 "2021-08-26T04:53:35Z")

</div>

My setups are using, 12v dc peristaltic pumps, relays, nodemcu, gravity tds sensor, ph 4502c sensor, and esp32 camera.

The pumps wired to relays and the connected to nodemcu, the sensors also connected to nodemcu. As for the sensors, it worked and can send the sensor data to node-red gauge node.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [26 August 2021 12:20 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/11 "2021-08-26T12:20:01Z")

</div>

You haven't told us what you're running Node-RED on, or how it communicates with your NodeMCU sysem.

I'm also not sure whether you want to keep using the NodeMCU to handle the sensors and pumps, possibly with some control from Node-RED, or if you want to move everything to Node-RED.

---

<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:** [26 August 2021 13:35 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/12 "2021-08-26T13:35:18Z")

</div>

Oh, im sorry about that, if its about OS im running it on windows 10, for the communication im using MQTT, and the code mostly on arduino IDE, the camera is using FTDI.

Im confused which is more effective, controlling the pump on NodeMCU or on Node-RED.

Im sorry if i still not understand your meaning, english is not my first language.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [26 August 2021 16:26 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/13 "2021-08-26T16:26:59Z")

</div>

Thanks. Your English is very good, so no need to apologise.

As to whether you would be better controlling the pumps through the NodeMCU or Node-RED, it's hard to say if either is best. The amount of processing you're doing is very small, and either approach would handle it without any problem.

Maybe looking at the broader picture and considering how the system will be used will help in designing the best approach.

---

<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:** [27 August 2021 02:47 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/14 "2021-08-27T02:47:40Z")

</div>

Okay, got it.  
Um can i privately message you, i want to describe about how the system works, its pretty long, and maybe a bit confusing to you

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [27 August 2021 10:09 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/15 "2021-08-27T10:09:46Z")

</div>

Probably best to keep it on the forum. That way other people can jump in to help if they see something I miss, or have better knowledge of this type of system. I'm fairly new to Node-RED too, so will likely only be able to help with basic stuff.

---

<div class="post-metadata">

**Author:** ![fugit185](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@fugit185](https://discourse.nodered.org/u/fugit185)\
**Post date:** [4 September 2021 16:51 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/16 "2021-09-04T16:51:37Z")

</div>

> [@Ghoond](#):
>
> ph 4502c sensor

I recommend that you do all reasoning in NR and leave arduino do slave works. The sensors that you use are now high quality so don't expect them to last long, buy few spare parts

---

<div class="post-metadata">

**Author:** ![Chaos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chaos/32/107061_2.png) [@Chaos](https://discourse.nodered.org/u/Chaos)\
**Post date:** [7 September 2021 06:44 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/17 "2021-09-07T06:44:36Z")

</div>

i do somthing similar for my bathroom fan based on humidity. here is my simple code.

```auto
if(flow.get ("Ensuite_Humid") >= 80){
    msg.payload = "ON"
    flow.set("Ensuite_Fan",msg.payload);
    node.status({text:msg.payload});
        return msg;
}
if(flow.get ("Ensuite_Humid") <= 79){
    msg.payload = "OFF"
    flow.set("Ensuite_Fan",msg.payload);
    node.status({text:msg.payload});
        return msg;
}
return null

```

The incoming humidity is stored in the varable "Ensuite\_Humid" as it comes in via mqtt. by a previos function.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 November 2021 06:45 UTC](https://discourse.nodered.org/t/how-to-control-12v-dc-motor-peristaltic-pump-using-function/50141/18 "2021-11-06T06:45:02Z")

</div>

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