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"]]}]
Workshopshed

Flow Info

created 1 year, 3 months ago

Node Types

Core
  • comment (x1)
  • debug (x5)
  • delay (x1)
  • function (x7)
  • inject (x3)
  • switch (x1)
Other
  • subflow (x1)
  • subflow:97192799.714bd (x1)

Tags

  • motor
  • stepper
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option