# Slow down PCA9685 servo

**URL:** <https://discourse.nodered.org/t/slow-down-pca9685-servo/13464>\
**Category:** General\
**Created:** [19 July 2019 02:42 UTC](https://discourse.nodered.org/t/slow-down-pca9685-servo/13464 "2019-07-19T02:42:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mshaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mshaz/32/2294_2.png) [@mshaz](https://discourse.nodered.org/u/mshaz)\
**Post date:** [19 July 2019 02:42 UTC](https://discourse.nodered.org/t/slow-down-pca9685-servo/13464/1 "2019-07-19T02:42:29Z")

</div>

Hi

I need help, I can get the servo running but I need to slow down a bit the rotations. I m making a robot.

This was the node that i used.

> **[node-red-contrib-pca9685](https://flows.nodered.org/node/node-red-contrib-pca9685)**
>
> Control a PCA9685 board with Node-RED

How do i do this in correct way? Below is my flow.

Thank you

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5882ce72b0d06f2e7a5cb7067a8e47102c7e2954.jpeg) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2742a0921c9c87c6247452b37fcb47ae71b8db7e.jpeg)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [19 July 2019 06:45 UTC](https://discourse.nodered.org/t/slow-down-pca9685-servo/13464/2 "2019-07-19T06:45:47Z")

</div>

See [https://www.raspberrypi.org/forums/viewtopic.php?t=162782](https://www.raspberrypi.org/forums/viewtopic.php?t=162782)

---

<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:** [19 July 2019 09:31 UTC](https://discourse.nodered.org/t/slow-down-pca9685-servo/13464/3 "2019-07-19T09:31:24Z")

</div>

You can use this to set a slew rate limit on a value, if that is what you are trying to do. This example limits the slew rate to 1 unit/second, sending intermediate values every 100 msec. Hopefully it is obvious from the code how to change the rate.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bdb6d7689cd8d1c9932fc534a4b6dbc1e3ba083d.png)

```auto
[{"id":"95daacd1.0e09f8","type":"debug","z":"514a90a5.c7bae8","name":"","active":true,"console":"false","complete":"false","x":570,"y":530,"wires":[]},{"id":"c136d3fd.21da78","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":99,"y":492,"wires":[["fc58524d.34f02"]]},{"id":"fc58524d.34f02","type":"function","z":"514a90a5.c7bae8","name":"Slew rate limit 1 unit/second","func":"// Limits the slew rate incoming payload values\n// optionally sending intermediate values at specified rate\nlet maxRate = 1; // max slew rate units/second\nlet sendIntermediates = true; // whether to send intermediate values\nlet period = 100; // period in millisecs to send new values (if sendIntermediates)\nlet jumpThreshold = 2; // if the step asked for is more that this then goes immediately to that value\n\nvar newValue = Number(msg.payload);\nvar timer = context.get('timer') || 0;\n// check the value is a number\nif (!isNaN(newValue) && isFinite(newValue)) {\n var target = msg.payload;\n context.set('target', target);\n // set last value to new one if first time through\n var lastValue = context.get('lastValue');\n if (typeof lastValue == \"undefined\" || lastValue === null) {\n lastValue = newValue;\n context.set('lastValue', newValue);\n }\n // calc new value\n msg.payload = calcOutput();\n // stop the timer\n if (timer) {\n clearTimeout(timer);\n context.set('timer', null);\n }\n // restart it if required to send intermediate values\n if (sendIntermediates) {\n timer = setInterval(function(){\n // the timer has run down calculate next value and send it\n var newValue = calcOutput();\n if (newValue != context.get('lastValueSent')) {\n context.set('lastValueSent', newValue);\n node.send({payload: newValue});\n }\n },period);\n context.set('timer', timer);\n }\n context.set('lastValueSent', msg.payload);\n} else {\n // payload is not a number so ignore it\n // also stop the timer as we don't know what to send any more\n if (timer) {\n clearTimeout(timer);\n context.set('timer', null);\n }\n msg = null;\n}\nreturn msg;\n\n// determines the required output value\nfunction calcOutput() {\n var lastValue = context.get('lastValue');\n var target = context.get('target');\n // set to current value if first time through or step > threshold\n if (typeof lastValue == \"undefined\" || lastValue === null) lastValue = target;\n var now = new Date();\n var lastTime = context.get('lastTime') || now;\n // limit value to last value +- rate * time\n var maxDelta = (now.getTime() - lastTime.getTime()) * maxRate/1000;\n if (Math.abs(target - lastValue) > jumpThreshold) {\n // step > threshold so go there imediately\n newValue = target;\n } else if (target > lastValue) {\n newValue = Math.min( lastValue + maxDelta, target);\n } else {\n newValue = Math.max( lastValue - maxDelta, target);\n }\n context.set('lastValue', newValue);\n context.set('lastTime', now); \n return newValue;\n}","outputs":1,"noerr":0,"x":351,"y":530,"wires":[["95daacd1.0e09f8"]]},{"id":"1501600b.8cd8d","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":99,"y":569,"wires":[["fc58524d.34f02"]]}]

```

This flow can be found at [https://flows.nodered.org/flow/13c793620b6ec830ef610c13fd845bcb](https://flows.nodered.org/flow/13c793620b6ec830ef610c13fd845bcb)
