# Example flow for control a motor

**URL:** <https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324>\
**Category:** General\
**Created:** [4 September 2022 15:48 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324 "2022-09-04T15:48:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [4 September 2022 15:48 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/1 "2022-09-04T15:48:21Z")

</div>

Hi guys,  
I've started writing a guide to automating with Node-Red.  
In the guide I want to show what options there are,  
for solve an automation problem.

This is the task:

_A motor is started with one push-button and stopped with an another push-button.  
The motor has an over-temperature switch.  
When this switch is closed, the engine is stopped and its prevents from starting again.  
A lamp or LED indicates that the over-temperature switch is active._

The first solution is a pure hardware solution with buttons, lamps and relays:

![scematic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48322bb6229530c530eada0ff0a199af2d535e22.png)

Of course you can solve this with Raspberry Pi and Node-Red.  
Here my question to you:  
If you assume that the Raspberry Pi is wired as follows:

GPIO19 -\> Motor OVERTEMP switch  
GPIO21 -\> START push-button  
GPIO20 -\> STOP push-button  
GPIO23 -\> MOTOR RELAY  
GPIO24 -\> MOTOR-OVERTEMP LED  
GPIO25 -\> MOTOR-ON LED

How does the flow look like for Node-Red?  
I think that the guys in Forum will find an easy solution.  
I add this solution then in my guide.

Thanks,  
Ocean (Derya)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 September 2022 16:22 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/2 "2022-09-04T16:22:46Z")

</div>

My first comment would be don't do the overtemperature stop in node red. Do the LED in node red by all means, but the interlock should be in hardware so there is no way that a bug in the flow can burn out the motor.

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [4 September 2022 17:22 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/3 "2022-09-04T17:22:35Z")

</div>

Good security aspect.  
@Colin Is that OK ?

 ![scematic2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d35785c1ba7dd7abc3f6e8024505dc627f1a3855.png)

_Solution with Rasberry Pi_

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 September 2022 17:44 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/4 "2022-09-04T17:44:21Z")

</div>

That looks like a pretty good circuit.  
You just need to sort out the NR flow in the Pi now.

**Please be very careful with the MAINS wiring on the contacts of the 5V relay.**

Have you considered using an opto-coupler or a solid-state-relay (SSR) to keep the mains and the logic circuitry well away from each other?

**Please take care.**

Here's an example of a SSR for an AC application.

> **[1.5US $ |Ssr-40da 40a Solid State Relay Input 3-32v Dc Output 24-380v Ac...](https://www.aliexpress.com/item/2255800936763073.html?spm=a2g0o.ppclist.product.2.4426uhAPuhAPqW&pdp_npi=2%40dis%21GBP%21%EF%BF%A11.36%21%EF%BF%A11.36%21%21%21%21%21%40211b446516623168893564022e2225%2110000014557949087%21btf&_t=pvid%3A8c01e1fb-a0c8-40a7-9625-ece3715779a1&afTraceInfo=4001123077825 __pc__ pcBridgePPC __xxxxxx__ 1662316889&gatewayAdapt=4itemAdapt)**
>
> Smarter Shopping, Better Living! Aliexpress.com

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 September 2022 18:24 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/5 "2022-09-04T18:24:40Z")

</div>

Just had a very quick go at creating a simple flow. (I'm sure it can be simplified.)

 ![motor_control](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73925f9e5d407aeaaa9dc56815c618c40485209c.png)  
I've used 'green' for OFF and 'red' for ON. Some people prefer the colours the other way round.  
I've also set the input pins with pull-ups, so the inputs are active LOW (pin shorted to ground).  
According to your diagram, the relay is operated when the output pin is LOW.

```auto
[{"id":"dcbdc7f59c771cef","type":"rpi-gpio in","z":"061e6706bc551e8f","name":"Start_button","pin":"21","intype":"up","debounce":"25","read":false,"bcm":true,"x":170,"y":360,"wires":[["6e2a412933384981"]]},{"id":"4593b98cd7e79233","type":"rpi-gpio in","z":"061e6706bc551e8f","name":"Stop button","pin":"20","intype":"up","debounce":"25","read":false,"bcm":true,"x":170,"y":260,"wires":[["eeccab846ba426c0"]]},{"id":"838f715684cd618a","type":"rpi-gpio out","z":"061e6706bc551e8f","name":"Motor_relay","pin":"23","set":"","level":"0","freq":"","out":"out","bcm":true,"x":790,"y":240,"wires":[]},{"id":"51dc944c0dab2799","type":"comment","z":"061e6706bc551e8f","name":"Assuming buttons are Active LOW","info":"","x":240,"y":100,"wires":[]},{"id":"be04b5291db74554","type":"inject","z":"061e6706bc551e8f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":160,"wires":[["6a27ec01cc850822"]]},{"id":"6a27ec01cc850822","type":"change","z":"061e6706bc551e8f","name":"","rules":[{"t":"set","p":"motor_relay","pt":"flow","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":160,"wires":[["7b4b5ef2c80cdb62"]]},{"id":"eeccab846ba426c0","type":"change","z":"061e6706bc551e8f","name":"","rules":[{"t":"set","p":"motor_relay","pt":"flow","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":240,"wires":[["7b4b5ef2c80cdb62"]]},{"id":"6e2a412933384981","type":"change","z":"061e6706bc551e8f","name":"","rules":[{"t":"set","p":"motor_relay","pt":"flow","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":340,"wires":[["7b4b5ef2c80cdb62"]]},{"id":"7b4b5ef2c80cdb62","type":"function","z":"061e6706bc551e8f","name":"","func":"let relay = flow.get(\"motor_relay\") || \"OFF\";\n\nif (relay == \"OFF\") {\n node.status({fill:\"green\",shape:\"dot\",text:\"Relay is OFF\"});\n msg.payload = 1;\n return msg;\n}\nelse if (relay == \"ON\") {\n node.status({fill:\"red\",shape:\"dot\",text:\"Relay is ON\"});\n msg.payload = 0;\n return msg;\n}\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":240,"wires":[["838f715684cd618a"]]},{"id":"1fa7b0c1ebe3d5be","type":"inject","z":"061e6706bc551e8f","name":"Test Stop btn","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":190,"y":220,"wires":[["eeccab846ba426c0"]]},{"id":"db91334e3e60c1ab","type":"inject","z":"061e6706bc551e8f","name":"Test Start btn","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":190,"y":320,"wires":[["6e2a412933384981"]]},{"id":"f78b40a92aa3d54d","type":"comment","z":"061e6706bc551e8f","name":"Assuming relay is activated with a LOW","info":"","x":870,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 September 2022 18:37 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/6 "2022-09-04T18:37:01Z")

</div>

Is the overtemp trip built into the motor? If so is it rated to drive the motor?

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [4 September 2022 18:53 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/7 "2022-09-04T18:53:27Z")

</div>

The thermal protection depends from motor size.  
Normally one or more bimetal switch is used and mounted in motor.  
Very big motors use electronic protection relay.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 September 2022 19:16 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/8 "2022-09-04T19:16:11Z")

</div>

That doesn't answer the question, is the bimetal strip rated so the motor can be powered through it?

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [4 September 2022 19:18 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/9 "2022-09-04T19:18:01Z")

</div>

Yes, you can use this SSR. But you use also the uln2803 driver, because the RPi pin can't drive the SSR.  
Normal relays have an insulation voltage 3-10kV between contact and coil.  
I see relay boards from china, where the relay coil and input are connected common to ground. This is stupid because the surge comes over ground. The opto-coupler is in this case not working.  
Did you teardown a SSR ?

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/061cfd6ebcc3b269d5691bf11a84430568ed6780.jpeg "Teardown of an eBay 25A Solid State Relay. (SSR)") ](https://www.youtube.com/watch?v=DxEhxjvifyY)

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [4 September 2022 19:22 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/10 "2022-09-04T19:22:01Z")

</div>

No, the contact rating is very low. Therefore I use a relay switched from thermal contact.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 September 2022 06:58 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/11 "2022-09-05T06:58:59Z")

</div>

> [@iiot2k](#):
>
> But you use also the uln2803 driver, **because the RPi pin can't drive the SSR**.

I don't understand the above statement - can you explain it a bit more?

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [5 September 2022 08:13 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/12 "2022-09-05T08:13:08Z")

</div>

Hi David,

the output current of RPi is limited to 16mA per pin and total 60mA, this is the limit of the 5V/3.3V step-down converter on RPi.  
Ok for one SSR is possible to connect direct to RPi pin.  
But this is always a risk.You can damage the output fet on RPi.  
The RPi isn't cheap and at moment not easy to get.  
Therefore I use always the driver ULN2803 (cost 50Cent and easy to get).  
This chip can drive 500mA per channel (2.5A total) and up to 50V voltage.  
And if you use relays or valfs, the ULN2803 has builtin diodes for prevent high voltage on operate.  
In the event of a surge, the ULN2803 dies and the RPi survives.  
The ULN2803 can also be used as input level converter for input voltage up to 30V.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 September 2022 08:25 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/13 "2022-09-05T08:25:41Z")

</div>

So your statement is technically inaccurate - as a RPi can drive a SSR.

I've looked at the circuit that appears in the YouTube video and estimate the current flowing through the optocoupler, when it is connected to a 3V3 supply, is 1.6mA - well within the capability of a RPi.

**I agree with you about the RPi being an expensive and hard-to-get item, and because of this fact  
I never let my IoT students connect anything directly to a GPIO pin.**

If I was building this circuit I'd probably substitute a Power MOSFET like a 2N7000 for the ULN2803A **(as in your circuit you are only using one section of the octal driver)**. This would save on PCB real-estate as the ULN2803A normally comes in a DIP18 package (a lot bigger than a T0-92 encapsulation).

At the start of this thread you said you are writing a guide to automating with Node-RED.  
So in your guide you could say something like.. "Although the RPi is capable of driving a SSR it should be borne in mind that this is an expensive item and can easily be damaged. A safer solution is to use an inexpensive component like an ULN2803A or a Power MOSFET to do the interfacing."

Just my two-pennies worth.

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [5 September 2022 08:37 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/14 "2022-09-05T08:37:38Z")

</div>

Thank you for your answers.  
You're right, a 2N2007, a resistor and a diode take up less space and  
200mA current from the 2N2007 is not small.  
But with 8 x 2N2007 it gets bigger.  
I took the ULN2803 because it was already in a different circuit diagram. (Copy/Paste 🙂 ).

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 September 2022 08:39 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/15 "2022-09-05T08:39:45Z")

</div>

My comments, about using a 2N7000, were based on the diagram you posted.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 September 2022 08:40 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/16 "2022-09-05T08:40:05Z")

</div>

> [@iiot2k](#):
>
> No, the contact rating is very low.

OK, in that case then yes, you need a relay or SSR for the protection circuit.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 September 2022 08:45 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/17 "2022-09-05T08:45:39Z")

</div>

> [@iiot2k](#):
>
> I see relay boards from china, where the relay coil and input are connected common to ground. This is stupid because the surge comes over ground

I am not sure what surge you mean, can you explain?

If you have a separate ground for the coil do you mean you have two isolated power supplies? One for the Pi and one for the coil?

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [5 September 2022 08:48 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/18 "2022-09-05T08:48:47Z")

</div>

I would like to show in the tutorial how the task is realized with  
only hardware, a PLC, an Arduino and RPi with Node-Red.

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [5 September 2022 09:05 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/19 "2022-09-05T09:05:59Z")

</div>

Yes its right, input ground and coil ground must not be connected.  
\*\*Also, the coil must be powered from another source.  
A lot of user takes the +5V from the RPi.  
Since the opto-coupler has no sense.  
A PC817 opto-coupler is usually used.  
This has an isolation voltage of 5kV.  
But only if there is no trace between the input and output pins.  
When I look at the PCB layout of the relay boards, this rule is not followed.  
The rule of thumb for isolation distance is 1mm = 1kV.

\*\* You can also use this isolated DC/DC konverter:  
 ![TEA1HI](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0d81156493d5e2b1af23a3394463cb733aac49a.jpeg)

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [5 September 2022 09:18 UTC](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324/20 "2022-09-05T09:18:50Z")

</div>

Maybe it's me, but I would avoid the "activated by a low output" whenever possible, specially when it comes to motors, cylinders, and other parts that can cause harm.

If there is an electrical failure on the Pi or a short in the board or wiring, the voltage in the output could be zero and activate the relay.

Personally, I always prepare the circuits so that a high output is needed to activate an actuator or motor, or allow it to move, like the safety mechanisms that need a high signal to release (a motor break, for instance). On a power loss, the brake will be locked and the motor won't be able to turn.

If you used low signal to activate the motor, it might be possible that in a partial power loss, the motor continued to apply force for a few seconds, at least, and create a hazardous situation.

[Next page](https://discourse.nodered.org/t/example-flow-for-control-a-motor/67324.md?page=2)
