DC motor with Node Red

1,269 views
Skip to first unread message

Rania Labib

unread,
Mar 1, 2017, 2:46:55 PM3/1/17
to Node-RED
Hi there,

I just tried to control a motor from node red, so far I can only make the motor go backward and forward (one direction at a time using an inject node) and I can also control the speed. However, I'd like to send a command to the motor like this one:
Go forward for 2 seconds with a speed of 20, then stop for 1 second, then go forward for 3 seconds with a speed of 50

Is this something that I can accomplish in node red?

Thank you!

Nick O'Leary

unread,
Mar 1, 2017, 3:55:45 PM3/1/17
to Node-RED

Hi Rania,

Can you share some more information on how you have the motor connected, how you are currently controlling it etc. It is hard to help without those sorts of details.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/59f6ca90-2b4b-4efc-af4f-e30c39b708d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rania Labib

unread,
Mar 4, 2017, 11:59:45 PM3/4/17
to Node-RED
Hi Nick,

I'm using a 12V DC motor that is 40 rpm. I connected the motor to a motor drive (L298N) using this guide, I used 8 AA batteries to produce 12V instead of 4 batteries. The Node Red flow I used to control the motor is here.

Sorry, too many links in my post. I'm just wondering if I can define the speed and the direction of the motor in a function node rather than sending a payload to GPIO node?

Thanks!

Colin Law

unread,
Mar 5, 2017, 4:03:09 AM3/5/17
to node...@googlegroups.com
The flow you posted shows you what messages you need to send to the
the flow to control speed and direction. Am I right in thinking that
what you are actually asking is how to:
Send a message with topic Speed and payload 20
Send a message with topic Direction and payload 1
Send a message with topic On and payload 1
Wait 2 seconds
Send a message with topic On and payload 0
Wait 1 second
Send a message with topic Speed and payload 50
Send a message with topic On and payload 1
Wait 3 seconds
Send a message with topic On and payload 0

If so then you could certainly do that in a function node using the
setTimeout javascript message to do the delays. However you could also
do it with a combination of standard nodes. Possible a combination of
change nodes (to generate the messages) and delay nodes for the
delays.

Colin
> https://groups.google.com/d/msgid/node-red/a76c2d33-49cf-4fa3-ae5c-b64f69d9525d%40googlegroups.com.

Rania Labib

unread,
Mar 6, 2017, 1:56:35 PM3/6/17
to Node-RED

Hi Colin,

Thank you for your help! I actually know what kind of payload to send to the pins, as I coded this in Python before, however, I'm kind of a beginner when it comes to node red and Javascript. I came up with this easy flow that is ok for the purpose of my project, I'd appreciate it if anyone here in this forum provides a feedback on how to improve my code. Also, if I'd used one function node to define digital and PWM input, what delay function should I use? is it sleep(x) ?







[{"id":"60ac769a.ac25f8","type":"inject","z":"d4374576.4fa988","name":"","topic":"Test input","payload":"test","payloadType":"str","repeat":"","crontab":"","once":false,"x":124.83334350585938,"y":634.3334045410156,"wires":[["41e3d0a1.f3152","bdf0a359.628b2"]]},{"id":"6c02a7fe.01bcb8","type":"rpi-gpio out","z":"d4374576.4fa988","name":"Motor Pin 1","pin":"16","set":true,"level":"0","out":"out","x":1070.6665954589844,"y":521.6667022705078,"wires":[]},{"id":"9814fdaf.55dfa","type":"rpi-gpio out","z":"d4374576.4fa988","name":"Motor Pin 2","pin":"18","set":true,"level":"0","out":"out","x":1052.6665954589844,"y":614.6667022705078,"wires":[]},{"id":"41e3d0a1.f3152","type":"switch","z":"d4374576.4fa988","name":"cond.Input","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"test","vt":"str"},{"t":"eq","v":"real input from ultrasonic sensor","vt":"str"}],"checkall":"true","outputs":2,"x":342.8333740234375,"y":625.0001525878906,"wires":[["2a490a67.a12f96","379ab74a.0afaf8","287df60c.e1166a","e0e184b4.cde848","df4b6969.c12798"],[]]},{"id":"2a490a67.a12f96","type":"function","z":"d4374576.4fa988","name":"0","func":"msg.payload= \"0\";\nreturn msg;","outputs":1,"noerr":0,"x":654.6665954589844,"y":685.6667175292969,"wires":[["6c02a7fe.01bcb8"]]},{"id":"379ab74a.0afaf8","type":"function","z":"d4374576.4fa988","name":"speed 20","func":"msg.payload= \"20\";\nreturn msg;","outputs":1,"noerr":0,"x":680.6665954589844,"y":724.6667175292969,"wires":[["2e5558e1.a470a8"]]},{"id":"287df60c.e1166a","type":"function","z":"d4374576.4fa988","name":"1","func":"var one= 1;\nmsg.payload = one;\nreturn msg;","outputs":1,"noerr":0,"x":654.8333435058594,"y":641.3333969116211,"wires":[["9814fdaf.55dfa"]]},{"id":"2e5558e1.a470a8","type":"rpi-gpio out","z":"d4374576.4fa988","name":"Motor Speed (PWM)","pin":"16","set":"","level":"0","out":"pwm","x":1067.6665954589844,"y":696.6667175292969,"wires":[]},{"id":"e0e184b4.cde848","type":"delay","z":"d4374576.4fa988","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":481.83331298828125,"y":847.0000915527344,"wires":[["88b7ed33.34008"]]},{"id":"88b7ed33.34008","type":"function","z":"d4374576.4fa988","name":"0","func":"var one= 0\nmsg.payload = one;\nreturn msg;","outputs":1,"noerr":0,"x":644.6665954589844,"y":888.3334045410156,"wires":[["9814fdaf.55dfa"]]},{"id":"df4b6969.c12798","type":"delay","z":"d4374576.4fa988","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":464.6665954589844,"y":919.6667175292969,"wires":[["f0dc26ce.e46728"]]},{"id":"f0dc26ce.e46728","type":"function","z":"d4374576.4fa988","name":"1","func":"var one= 1\nmsg.payload = one;\nreturn msg;","outputs":1,"noerr":0,"x":647.6665954589844,"y":936.6667175292969,"wires":[["6c02a7fe.01bcb8"]]},{"id":"bdf0a359.628b2","type":"delay","z":"d4374576.4fa988","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":479.8333435058594,"y":978.3334655761719,"wires":[["f43f07a5.1ad238"]]},{"id":"f43f07a5.1ad238","type":"function","z":"d4374576.4fa988","name":"speed 45","func":"msg.payload= \"45\";\nreturn msg;","outputs":1,"noerr":0,"x":676.6665954589844,"y":985.6667175292969,"wires":[["2e5558e1.a470a8"]]}]

Rodrigo Leme

unread,
May 27, 2018, 7:49:47 PM5/27/18
to Node-RED
Hi Rina how do you did to make the motor go backward ?  Thx

David Dempster

unread,
May 28, 2018, 10:55:13 AM5/28/18
to Node-RED
The L298N is an H-bridge module.
It basically has four transistors, each arranged in the leg of the letter-H.
The DC motor is placed along the horizontal bar.
By switching two transisitors On (say top-left and bottom-right), current flows from left to right through the motor.
If the other two transistors (top-right and bottom-left) are switched on, current flows in the opposite direction through the motor.
As it's a DC motor the direction of rotation depending on the direction of current flow.

Hope this helps.
Reply all
Reply to author
Forward
0 new messages