# Javascript for PWM (Pulse Width Modulation) to dim a heater

**URL:** <https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730>\
**Category:** General\
**Created:** [27 February 2021 18:41 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730 "2021-02-27T18:41:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [27 February 2021 18:41 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/1 "2021-02-27T18:41:22Z")

</div>

Hello, NodeRed is new for me, and I'm new here....  
I Installed Node-RED on a Unipi Neuron S103 (Raspberry Pi inside) and want to dim an electrical heater. I want to send short pulses (PWM) to a solid state relais. Can somebody write a PWM in JavaScript?

---

<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:** [27 February 2021 19:22 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/2 "2021-02-27T19:22:12Z")

</div>

The GPIO Out node can be configured to output PWM. Or does that not do what what you want?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 February 2021 19:35 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/3 "2021-02-27T19:35:16Z")

</div>

yes GPIO can do simple PWM but for a heater I think @colin 's PID control may be more appropriate 🙂 [node-red-contrib-pid (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-pid) depending on what the actual bigger requirement is.

---

<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:** [27 February 2021 20:13 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/4 "2021-02-27T20:13:05Z")

</div>

Perhaps I misunderstood what you are trying to do. What frequency pwm are you trying to generate?

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [27 February 2021 20:41 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/5 "2021-02-27T20:41:58Z")

</div>

@colin When there is 100 Watt solarpower production that can be consumed.... then I want to put an electrical heater from 2000 Watt continuous 100ms ON and 1900ms OFF.

@dceejay maybe I can use PID, when it is switching fast (and no shorter times than 10ms)

---

<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:** [27 February 2021 20:52 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/6 "2021-02-27T20:52:43Z")

</div>

In that case have a look at node-red-contrib-timeprop . It is often used with the pid node for heater switching so should be perfect for your problem.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 09:30 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/7 "2021-02-28T09:30:53Z")

</div>

@Colin Great! This node is installed, looks very interesting. It needs a 0 to 1 input for a 0% tot 100% output power. But it is all new for me....

I have the electrical power production: ( p1monitor/smartmeter/production\_kw : msg.payload : string[5] "1.378") 1378 Watt.  
When the measured "production\_kw" is more then 100 Watt. Then I want the output 5% or 0,05 higher.  
And this every 10 seconds.  
The output of this, between 0 and 1, goes to the node-red-contrib-timeprop.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 09:33 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/8 "2021-02-28T09:33:21Z")

</div>

Do you think there is a PID I can use to solve this?

---

<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:** [28 February 2021 09:37 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/9 "2021-02-28T09:37:12Z")

</div>

I don't know yet, I don't fully understand the problem. Is the production figure affected by the heater load? So that I can better understand can you describe what this is attempting to achieve?

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 09:41 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/10 "2021-02-28T09:41:25Z")

</div>

Yes, the production is affected by the heater.  
When there is 1000 Watt Solar Production the leaves the house to the street, = injected in the public grid, then we loose those 1000 Watt.  
It is better to consume the 1000 Watt, then there is 0 Watt Solar production that goes to the public grid.

---

<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:** [28 February 2021 09:58 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/11 "2021-02-28T09:58:26Z")

</div>

So if, say the production was measured at, say 500W, why don't you increase the heater power by 500W for the next ten seconds Or possible a bit less if you want to stay on the right side of zero, so maybe increase the power by current production - 100.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 10:01 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/12 "2021-02-28T10:01:18Z")

</div>

That would be great.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 10:02 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/13 "2021-02-28T10:02:55Z")

</div>

Maybe it is possible to write this in javascript.... but not for me.

---

<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:** [28 February 2021 10:33 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/14 "2021-02-28T10:33:40Z")

</div>

Something like this maybe

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

```auto
[{"id":"6c924292.4f7b54","type":"function","z":"37f5b818.697af8","name":"Calculate new output","func":"// Expects current net production in msg.payload\nconst headroom = 100 // Watts headroom to keep \nconst approachFactor = 0.8 // what fraction to go each time\nconst max = 2000 // max heater output\n// Retrieve previous saved heater output, default to 0\nlet previousOutput = context.get(\"previousOutput\") || 0\n// calculate new heater power required\nlet newOutput = previousOutput + (msg.payload - headroom) * approachFactor\n// clamp to range 0 to max\nif (newOutput > 2000) newOutput = 2000\nelse if (newOutput < 0) newOutput = 0\n// save new output\ncontext.set(\"previousOutput\", newOutput)\nmsg.payload = newOutput\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1060,"wires":[["4c5f3ff6.078818","23cc3b86.d2bb6c"]]},{"id":"8b9d6e8a.a7152","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"500","payloadType":"num","x":90,"y":1040,"wires":[["6c924292.4f7b54"]]},{"id":"b5d8ef98.66d6c8","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":90,"y":1080,"wires":[["6c924292.4f7b54"]]},{"id":"347534ff.4318c4","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-100","payloadType":"num","x":90,"y":1120,"wires":[["6c924292.4f7b54"]]},{"id":"4c5f3ff6.078818","type":"debug","z":"37f5b818.697af8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":1140,"wires":[]},{"id":"33191a72.6bfe7e","type":"comment","z":"37f5b818.697af8","name":"Net production","info":"","x":110,"y":1000,"wires":[]},{"id":"23cc3b86.d2bb6c","type":"range","z":"37f5b818.697af8","minin":"0","maxin":"2000","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"Scale 0 to 1","x":550,"y":1060,"wires":[[]]},{"id":"aa06f32e.f5656","type":"comment","z":"37f5b818.697af8","name":"To timeprop","info":"","x":650,"y":1020,"wires":[]}]

```

I have included an approach factor so that it does not attempt to straight there in one go but homes in on it, as otherwise I suspect you might find it bouncing up and down too much. Try it and see. When you get to testing it use a Chart node set to show the production and the heater power, that is the best way of checking how it is working. Or perhaps you are already saving the production to Influx or something, in which case that is great. Add the heater output into the chart.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 13:42 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/15 "2021-02-28T13:42:31Z")

</div>

@Colin This looks very useful.  
And I'm a beginner, even copying this is difficult.  
I'm not at home now, is there a way to reach my hardware when I know my IP-adresses?

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 14:59 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/16 "2021-02-28T14:59:44Z")

</div>

@Colin Can you program it on my computer? (With teamviewer, when I'm at home)  
Of course I want to pay for this service. How do we arrange this?  
Tomorrow I wil be at home.  
Then I will be launched, I will be able to make changes myself.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 15:03 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/17 "2021-02-28T15:03:08Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/9241aaf8cfa02d49e7be12ae3a25539b7351da68.png)

---

<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:** [28 February 2021 15:09 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/18 "2021-02-28T15:09:51Z")

</div>

> [@Jeff](#):
>
> Can you program it on my computer

No, sorry, I am retired and I don't work for money any more, I am glad to say.

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [28 February 2021 15:55 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/19 "2021-02-28T15:55:10Z")

</div>

Having done that read the node-red docs page Working with Messages which is particularly useful.  
Having done that you should then have a better understanding of what is required and can ask again with specific questions about what you still are not sure how to do.

---

<div class="post-metadata">

**Author:** ![Jeff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff/32/37977_2.png) [@Jeff](https://discourse.nodered.org/u/Jeff)\
**Post date:** [28 February 2021 18:33 UTC](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730/20 "2021-02-28T18:33:15Z")

</div>

@Colin Anyway, thanks for your help!

The forum limited me 3 hours because I'm new on the forum and I sended to much messages.  
(I'm happy for you, doing what you want and not doing it for money. I have hobbies that I don't want to do for money too. It's the only way to be totally free.)

[Next page](https://discourse.nodered.org/t/javascript-for-pwm-pulse-width-modulation-to-dim-a-heater/41730.md?page=2)
