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

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

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.

Hi and welcome.

I don't understand the question.


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.

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.

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

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

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

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

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.

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)

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.

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


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.

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.

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?

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.

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.

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.

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.

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

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.

1 Like

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

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

if(flow.get ("Ensuite_Humid") >= 80){
    msg.payload = "ON"
        return msg;
if(flow.get ("Ensuite_Humid") <= 79){
    msg.payload = "OFF"
        return msg;
return null

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

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