# Dynamic PWM Frequency?

**URL:** <https://discourse.nodered.org/t/dynamic-pwm-frequency/47076>\
**Category:** General\
**Created:** [12 June 2021 18:37 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076 "2021-06-12T18:37:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![freds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/freds/32/43247_2.png) [@freds](https://discourse.nodered.org/u/freds)\
**Post date:** [12 June 2021 18:37 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/1 "2021-06-12T18:37:20Z")

</div>

I am trying to create a test jig to drive a RPM sensor and I need to take the RPM value and divide it by 2.82 and toggle a rpi GPIO pin that number of times a second.

The GPIO node in PWM mode will honor the set frequency value, however it will not accept it as a message input.

---

<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:** [12 June 2021 19:19 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/2 "2021-06-12T19:19:14Z")

</div>

Add a debug node showing what you are sending to the gpio node and post the output here.

---

<div class="post-metadata">

**Author:** ![freds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/freds/32/43247_2.png) [@freds](https://discourse.nodered.org/u/freds)\
**Post date:** [12 June 2021 19:33 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/3 "2021-06-12T19:33:33Z")

</div>

Here's my flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d8b9cb6896015a63be701cbe8a049ccb7cb3b62a.jpeg)

```auto
[{"id":"7175d55.5e6142c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"9166b787.249cc8","type":"calculator","z":"7175d55.5e6142c","name":"Calculate Generator R terminal value","inputMsgField":"payload","outputMsgField":"frequency","operation":"div","constant":"2.82","round":true,"decimals":0,"x":350,"y":180,"wires":[["3272a7d2.5495e8"]]},{"id":"82ef93d.b682e7","type":"ui_slider","z":"7175d55.5e6142c","name":"","label":"slider","tooltip":"","group":"66629d30.efc514","order":0,"width":0,"height":0,"passthru":true,"outs":"all","topic":"topic","topicType":"msg","min":0,"max":"2400","step":"25","x":330,"y":80,"wires":[["b243462b.3487a8","1f25c30d.bc5c5d","9166b787.249cc8"]]},{"id":"1f25c30d.bc5c5d","type":"ui_gauge","z":"7175d55.5e6142c","name":"Current RPM","group":"66629d30.efc514","order":5,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"2400","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":530,"y":80,"wires":[]},{"id":"b243462b.3487a8","type":"debug","z":"7175d55.5e6142c","name":"RPM Setting","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":40,"wires":[]},{"id":"c799f521.3a7388","type":"debug","z":"7175d55.5e6142c","name":"PWM Inputs","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":260,"wires":[]},{"id":"2c6fd8ae.8bcae8","type":"inject","z":"7175d55.5e6142c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":90,"y":60,"wires":[["82ef93d.b682e7"]]},{"id":"62c1bc63.dd39a4","type":"inject","z":"7175d55.5e6142c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"600","payloadType":"num","x":90,"y":100,"wires":[["82ef93d.b682e7"]]},{"id":"94bec17c.5e77a","type":"inject","z":"7175d55.5e6142c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1800","payloadType":"num","x":90,"y":140,"wires":[["82ef93d.b682e7"]]},{"id":"b301b983.60d478","type":"rpi-gpio out","z":"7175d55.5e6142c","name":"PWM GPIO12","pin":"32","set":"","level":"0","freq":"","out":"pwm","x":540,"y":320,"wires":[]},{"id":"3272a7d2.5495e8","type":"change","z":"7175d55.5e6142c","name":"Set PWM 50% duty cycle","rules":[{"t":"set","p":"payload","pt":"msg","to":"50","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":280,"wires":[["c799f521.3a7388","b301b983.60d478"]]},{"id":"66629d30.efc514","type":"ui_group","name":"RPM Testing","tab":"de246fc6.c97b5","order":1,"disp":true,"width":"6","collapse":false},{"id":"de246fc6.c97b5","type":"ui_tab","name":"Testing","icon":"dashboard","order":7,"disabled":false,"hidden":false}]

```

Here's the debug output:

```auto
{"_msgid":"bbcdd507.8e73e8","payload":50,"topic":"","frequency":213}

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 June 2021 21:09 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/4 "2021-06-12T21:09:15Z")

</div>

> [@freds](#):
>
> The GPIO node in PWM mode will honor the set frequency value, however it will not accept it as a message input.

Whart do you mean by that? It you look at the GPIO node, you will see '50' under it.

---

<div class="post-metadata">

**Author:** ![freds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/freds/32/43247_2.png) [@freds](https://discourse.nodered.org/u/freds)\
**Post date:** [13 June 2021 15:09 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/5 "2021-06-13T15:09:00Z")

</div>

The value 50 is the percentage of the duty cycle, not the frequency which only has a static configuration on the node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/2191ff2d9682cd08b2a6ed7fd4bd6c8f993dc84f.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2021 16:53 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/6 "2021-06-13T16:53:15Z")

</div>

Looks like you are using to high a value. Notice what the `Help` says:

# rpi-gpio out

Raspberry Pi output node. Can be used in Digital or PWM modes.

### [Inputs](http://testpi.local:1880/#)

payload number | string | boolean

### [Details](http://testpi.local:1880/#)

Digital mode - expects a `msg.payload` with either a 0 or 1 (or true or false), and will set the selected physical pin high or low depending on the value passed in.

The initial value of the pin at deploy time can also be set to 0 or 1.

`PWM mode - expects an input value of a number 0 - 100. It can be floating point.`

PWM mode can be used to drive a servo using input values between 10 and 20 only, but will accept floating point values. The GPIO2 pin is best for this as it uses hardware to do the PWM. For better servo support consider the alternative node-red-node-pi-gpiod node.

Requires the RPi.GPIO python library version 0.5.10 (or better) in order to work.

---

<div class="post-metadata">

**Author:** ![freds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/freds/32/43247_2.png) [@freds](https://discourse.nodered.org/u/freds)\
**Post date:** [13 June 2021 18:10 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/7 "2021-06-13T18:10:52Z")

</div>

Well obviously I am using the wrong kind of node.

How can I toggle a GPIO pin X times a second on a Raspberry Pi?

My desired range is 213 to 833 HZ..

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 August 2021 18:11 UTC](https://discourse.nodered.org/t/dynamic-pwm-frequency/47076/8 "2021-08-12T18:11:16Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
