Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Workshopshed
Last active September 26, 2018 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Workshopshed/54dd7dff5d1432e6bcc53b987dd5f0b6 to your computer and use it in GitHub Desktop.
Save Workshopshed/54dd7dff5d1432e6bcc53b987dd5f0b6 to your computer and use it in GitHub Desktop.
Stepper Motor

A function generator to run a uni-polar stepper motor such as the tiny BYJ-48 from a Raspberry Pi. Note that you'll need some kind of buffer module/chip as the Pi can't supply sufficient current to a stepper motor.

Inputs "Step" and "Direction" message types and 4 outputs for the motor pins. If you pass a value of 10 when you send a step message then the outputs will cycle through 10 steps. Use the delay node to control the rate of motor stepping.

The "LED" function nodes use the status indicator so you can see what's happening without having to hook it up to GPIOs

[{"id":"97192799.714bd","type":"subflow","name":"Stepper Motor","info":"","in":[{"x":160,"y":100,"wires":[{"id":"92e9f528.857ba"}]}],"out":[{"x":1044,"y":79,"wires":[{"id":"b833a444.b121f8","port":0}]},{"x":1041,"y":137,"wires":[{"id":"b833a444.b121f8","port":1}]},{"x":1042,"y":189,"wires":[{"id":"b833a444.b121f8","port":2}]},{"x":1044,"y":242,"wires":[{"id":"b833a444.b121f8","port":3}]},{"x":1025,"y":379,"wires":[{"id":"b833a444.b121f8","port":4},{"id":"8aa480a.a401a","port":0}]}]},{"id":"8aa480a.a401a","type":"function","z":"97192799.714bd","name":"Toggle Direction","func":"var direction = flow.get('direction')||0;\n\nif (direction === -1)\n{\n direction = 1;\n}\nelse {\n direction = -1;\n}\nflow.set('direction',direction);\nmsg.payload = direction;\nreturn msg;","outputs":1,"noerr":0,"x":431,"y":267,"wires":[[]]},{"id":"92e9f528.857ba","type":"switch","z":"97192799.714bd","name":"Step or Direction","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Step","vt":"str"},{"t":"eq","v":"Direction","vt":"str"}],"checkall":"true","outputs":2,"x":291,"y":171,"wires":[["1dff41aa.06dbde"],["8aa480a.a401a"]]},{"id":"34fcd685.ab0932","type":"comment","z":"97192799.714bd","name":"Flow Variables","info":"* Direction\n* Sequence\n* SequenceIndex","x":279,"y":42,"wires":[]},{"id":"b833a444.b121f8","type":"function","z":"97192799.714bd","name":"Sequence Generator","func":"var seqindex = context.get('seqindex')||0;\nvar sequence = [ [1,0,0,0],[1,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,1,1],[0,0,0,1],[1,0,0,1]]\nvar direction = flow.get('direction');\n\nseqindex = (seqindex % (sequence.length));\nif (seqindex == -1) {seqindex = sequence.length-1;}\n\nmsg.payload = sequence[seqindex];\nmsg.seqindex = seqindex;\nmsg.direction = direction;\n\ncontext.set('seqindex',seqindex+direction);\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Step\"});\nnode.status({});\n\nreturn GenerateOutput(seqindex);\n\nfunction GenerateOutput(index) {\n return [ NewMessage(sequence[index][0]), NewMessage(sequence[index][1]),NewMessage(sequence[index][2]),NewMessage(sequence[index][3]), msg ];\n}\n\nfunction NewMessage(val) {\n return { payload:val };\n}","outputs":"5","noerr":0,"x":720,"y":174,"wires":[[],[],[],[],[]]},{"id":"1dff41aa.06dbde","type":"function","z":"97192799.714bd","name":"Multistep","func":"var outputMsgs = [];\nvar steps = msg.payload;\nfor (var count = 0; count < steps; count++) { \n outputMsgs.push({payload:1});\n}\nreturn [ outputMsgs ];","outputs":1,"noerr":0,"x":434,"y":90,"wires":[["637b5925.225a28"]]},{"id":"637b5925.225a28","type":"delay","z":"97192799.714bd","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"2","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":615,"y":76,"wires":[["b833a444.b121f8"]]},{"id":"19a25cb3.2752a3","type":"inject","z":"3db945ee.2c881a","name":"Step","topic":"Step","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":330,"y":185,"wires":[["52c41dde.2b7024"]]},{"id":"e3a75e9a.2abe28","type":"inject","z":"3db945ee.2c881a","name":"Direction","topic":"Direction","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":333,"y":240,"wires":[["52c41dde.2b7024"]]},{"id":"52c41dde.2b7024","type":"subflow:97192799.714bd","z":"3db945ee.2c881a","name":"","x":623,"y":207.5,"wires":[["6bf4aed4.02b14"],["e1f7bd52.ebfb18"],["f4d3178.cd67e68"],["63bd4a0b.8bc31c"],["43c248eb.08c858"]]},{"id":"75ecccf0.7a940c","type":"debug","z":"3db945ee.2c881a","name":"1","active":false,"console":"false","complete":"payload","x":1023,"y":78.5,"wires":[]},{"id":"5e4f7f85.05023","type":"debug","z":"3db945ee.2c881a","name":"2","active":false,"console":"false","complete":"payload","x":1028,"y":147.5,"wires":[]},{"id":"700a200f.68175","type":"debug","z":"3db945ee.2c881a","name":"3","active":false,"console":"false","complete":"payload","x":1032,"y":202.5,"wires":[]},{"id":"4e3bc2c6.bef53c","type":"debug","z":"3db945ee.2c881a","name":"4","active":false,"console":"false","complete":"payload","x":1032,"y":270.5,"wires":[]},{"id":"43c248eb.08c858","type":"debug","z":"3db945ee.2c881a","name":"Motor Debug","active":false,"console":"false","complete":"true","x":858,"y":350,"wires":[]},{"id":"123c7681.19d5f1","type":"inject","z":"3db945ee.2c881a","name":"Multistep","topic":"Step","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"x":319,"y":125,"wires":[["52c41dde.2b7024"]]},{"id":"6bf4aed4.02b14","type":"function","z":"3db945ee.2c881a","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":878,"y":79,"wires":[["75ecccf0.7a940c"]]},{"id":"e1f7bd52.ebfb18","type":"function","z":"3db945ee.2c881a","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":870,"y":145,"wires":[["5e4f7f85.05023"]]},{"id":"f4d3178.cd67e68","type":"function","z":"3db945ee.2c881a","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":867,"y":204,"wires":[["700a200f.68175"]]},{"id":"63bd4a0b.8bc31c","type":"function","z":"3db945ee.2c881a","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":864,"y":274,"wires":[["4e3bc2c6.bef53c"]]}]
@srancsik
Copy link

srancsik commented Oct 4, 2016

Thank you very much for this.
I have a question, what is the wiring looking like from the panel to the Raspberry Pi's GPIO ports?
Thank you in advance for the reply

@knjigor
Copy link

knjigor commented Jan 27, 2017

Hi,
I was looking days for this, but have same question, what Raspberry Pi's GPIO ports are used for controlling stepper?

@JWD42
Copy link

JWD42 commented Aug 11, 2017

@srancsik/@knjigor you would add the rpi gpio 'out' nodes and assign them to whatever pins you want - output "1" would go to gpio pin of your choice which in turn would connect to 'in1' on your motor driver (uln2003, a3967slb, etc.) and so on for all four.

however, my issue is the sequence is delivered out too slowly - it sends the correct sequence to the driver board, however it is very slow, even after removing the delay node in the stepper motor subflow... it simply heats up the motor coils (literally), however I know the motor works, as i can drive it in c++ with wiringpi just fine.

I am just now looking at node-red (never used java script in my life) so i can set all this up for my daughters to "make robots" (the sole reason i got the PI Zero W to begin with) - they played with scratch a while back but the whole broadcast stuff to get it to work with the rpi gpio seems pretty stupid so when i saw the 'pretty drag and drop' of node-red i thought they could figure out the simple logic of it rather quickly.

Any help with understanding what is limiting the delivery speed would be appreciated.
I can always fall back to showing them python, c++, or whatever, but if node-red can actually go to the scale of controlling simple robots, that would be great.

@jcbevns
Copy link

jcbevns commented Sep 26, 2018

Hi, I'm also having problems with the delay of the motor (28BYJ-48). The steps are successfully sent to motor and the LEDs on my ULN2003 stepper are in sequence, but it is just far too slow. I'm running on a Raspi B (rev1) with node-red server running. Delay node in stepper motor sub-flow from 1ms to 100ms or 1000ms all too slow.

Python script running on the pi with a delay of 1ms spins at expected rate, but the NodeRed just doesn't seem to deliver fast enough.

Anybody had a decent output to a stepper? Node-red too slow on a pi or should I be looking elsewhere.

Anybody tried python functions in node-red python function blocks? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment