# Controlling LED brightness using node-red-node-pi-gpio with pwm results in flickering

**URL:** https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680
**Category:** Core Development
**Tags:** docker
**Created:** [30 September 2023 08:32 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680 "2023-09-30T08:32:31Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [30 September 2023 08:32 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/1 "2023-09-30T08:32:31Z")

</div>

I am using node-red-node-gpio (version 2.0.6) to control the brightness of a LED that is attached to my raspberry pi 3.

When I set the brightness to a low value (e.g. 6 (6%)) then the LED flickers.  
I would not expect this flickering - the LED should appear as it is shining with a constant low brightness.

here below the settings.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4bc939fffb386f73a29affa41d40151e359ad065.png)

To complicate matters I am running node-red and RPi.GPIO in a docker container in privileged mode.

My node-red version:

```auto
Welcome to Node-RED
===================

30 Sep 10:01:28 - [info] Node-RED version: v3.1.0
30 Sep 10:01:28 - [info] Node.js version: v18.17.1
30 Sep 10:01:28 - [info] Linux 5.10.103-v7+ arm LE
30 Sep 10:01:31 - [info] Loading palette nodes
30 Sep 10:01:38 - [info] Dashboard version 3.6.0 started at /ui

```

my RPi.GPIO version

```auto
31c7c63ab81f:~$ python
Python 3.11.5 (main, Aug 26 2023, 11:59:23) [GCC 12.2.1 20220924] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.VERSION
'0.7.1'
>>> 

```

I don't think the problem is caused due to an overloaded CPU according to top statistics taking in the container shell

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

or the top statistics from the host machine:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/803bbde3ba6746683a669cf5ca9471ca5a76b64d.png)

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [30 September 2023 08:40 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/2 "2023-09-30T08:40:22Z")

</div>

I understand that this flickering is normal when using **Software Timed PWM** but raspberry pi is also supporting **DMA timed PWM**.

See also [gpio - What's the difference between soft PWM and PWM - Raspberry Pi Stack Exchange](https://raspberrypi.stackexchange.com/questions/100641/whats-the-difference-between-soft-pwm-and-pwm)

So I am wondering if [node-red-node-pi-gpio](https://flows.nodered.org/node/node-red-node-pi-gpio) is using **Software Timed PWM** and if so: is there a way to swich to **DMA timed PWM** (e.g. by using [node-red-node-pi-gpiod](https://flows.nodered.org/node/node-red-node-pi-gpiod)).

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [30 September 2023 09:21 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/3 "2023-09-30T09:21:27Z")

</div>

> [@janvda](#):
>
> So I am wondering if [node-red-node-pi-gpio](https://flows.nodered.org/node/node-red-node-pi-gpio) is using **Software Timed PWM**

`node-red-node-pi-gpio` uses [RPi.GPIO](https://pypi.org/project/RPi.GPIO/) under the hood.

According to it's documentation, it's **Software PWM** only.

> Note that the current release does not support SPI, I2C, hardware PWM or serial functionality on the RPi yet. This is planned for the near future - watch this space! One-wire functionality is also planned.
> 
> Although hardware PWM is not available yet, software PWM is available to use on all channels.

---

<div class="post-metadata">

### Author: ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)
#### Post date: [30 September 2023 09:33 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/4 "2023-09-30T09:33:34Z")

</div>

The problem with hardware pwm on the RPi is that you need root permission to access the register. Therefore software pwm is mostly used. You can also use a pwm chip pca9685.

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [30 September 2023 10:01 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/5 "2023-09-30T10:01:20Z")

</div>

OK, as it is using **Software timed PWM** that explains the flickering.

So I am considering to switch to [node-red-node-pi-gpiod](https://flows.nodered.org/node/node-red-node-pi-gpiod) which is not based on RPi.GPIO but on [pigpio daemon](http://abyz.me.uk/rpi/pigpio/pigpiod.html).  
Would that use **DMA timed PWM**?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [30 September 2023 10:42 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/6 "2023-09-30T10:42:51Z")

</div>

Apparently pigpio PWM uses DMA.  
[https://www.js4iot.com/2021/06/04/DMA\_PWM-PIGPIO.html](https://www.js4iot.com/2021/06/04/DMA_PWM-PIGPIO.html)

@dceejay says he expects GPIO nodes to stop working (presumably when RPiOS is updated for the Pi 5) [https://discourse.nodered.org/t/raspberry-pi-5-announced-today-available-oct-23/81656/5](https://discourse.nodered.org/t/raspberry-pi-5-announced-today-available-oct-23/81656/5) so it looks like switching to node-red-node-pi-gpiod is essential.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [30 September 2023 13:59 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/7 "2023-09-30T13:59:47Z")

</div>

> [@iiot2k](#):
>
> The problem with hardware pwm on the RPi is that you need root permission to access the register. Therefore software pwm is mostly used. You can also use a pwm chip pca9685.

You should be able to adjust the permissions to allow the user running Node-RED to have access?

> [@janvda](#):
>
> So I am considering to switch

I think that most of us don't recommend using the Pi GPPIO at all unless you really have to. It is supposedly all too easy to release the magic blue smoke on your hard-to-find Pi and access to the GPIO hardware is relatively hard.

If budget allows, the addition of a wired ESP, Arduino or Pi PICO device for a few dollars/pounds/euros can make things a lot more robust.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [30 September 2023 14:29 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/8 "2023-09-30T14:29:57Z")

</div>

> [@TotallyInformation](#):
>
> I think that most of us don't recommend using the Pi GPPIO at all unless you really have to. It is supposedly all too easy to release the magic blue smoke on your hard-to-find Pi and access to the GPIO hardware is relatively hard.

I disagree with this - all my Pi interfacing to hardware do so directly

Adding a ESP adds more complexity and not required for simple projects

but EETTO 🙂

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [30 September 2023 14:31 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/9 "2023-09-30T14:31:59Z")

</div>

Not a problem, each to their own as always. Though I think this thread illustrates at least one of the issues. 🙂

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [30 September 2023 14:32 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/10 "2023-09-30T14:32:55Z")

</div>

The OP is jsut using the wrong hammer for the job 🙂  
gpiod is what is needed for hardware PWM 🙂

---

<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: [30 September 2023 15:33 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/11 "2023-09-30T15:33:13Z")

</div>

> [@iiot2k](#):
>
> The problem with hardware pwm on the RPi is that you need root permission to access the register.

I believe that when using pigpiod it is the daemon that runs as root, so node-red does not need to.

---

<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: [30 September 2023 16:18 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/12 "2023-09-30T16:18:57Z")

</div>

Correct. Pigpiod daemon runs as root and uses DMA. So is much better for pwm and servo control.

---

<div class="post-metadata">

### Author: ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)
#### Post date: [30 September 2023 16:40 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/13 "2023-09-30T16:40:24Z")

</div>

Also gpiod does not solve @janvda's problem.  
I see that the PWM frequency is 100Hz.  
This means 1/100Hz = 10ms, which is why the LED is switched on every 9.4ms for 0.6ms (6% duty cycle).  
And you see that as flickers.  
Try testing at 400Hz or more.

---

<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: [30 September 2023 20:39 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/14 "2023-09-30T20:39:05Z")

</div>

I thought that 100Hz was faster than the eye can respond.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [30 September 2023 20:48 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/15 "2023-09-30T20:48:00Z")

</div>

[info\_ieee-pem\_2014-09\_led-flickering.pdf (bio-licht.org)](https://www.bio-licht.org/02_resources/info_ieee-pem_2014-09_led-flickering.pdf)

[visible light - Limit of human eye flicker perception? - Physics Stack Exchange](https://physics.stackexchange.com/questions/19040/limit-of-human-eye-flicker-perception)

All seems quite complex. 🙂

Ambient light, movement, whether the LED goes completely off or not between cycles, all seem to have an impact.

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [30 September 2023 22:25 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/16 "2023-09-30T22:25:32Z")

</div>

I have installed _pigpiod_ version V79 using

```auto
sudo apt-get install pigpio

```

If I start the deamon using command:

```auto
sudo systemctl start pigpiod

```

then I hear a short burst of crackling noise coming out of my voice hat speaker and the following is logged in the log file:

```auto
[143.541292] voicehat-codec voicehat-codec: Disabling audio amp...
[143.541461] voicehat-codec voicehat-codec: Disabling audio amp...
[144.163622] voicehat-codec voicehat-codec: Enabling audio amp...
[144.192467] voicehat-codec voicehat-codec: Enabling audio amp...
[144.401861] voicehat-codec voicehat-codec: Disabling audio amp...
[144.402024] voicehat-codec voicehat-codec: Disabling audio amp...
[144.761855] voicehat-codec voicehat-codec: Enabling audio amp...
[144.789023] voicehat-codec voicehat-codec: Enabling audio amp...
[145.003168] voicehat-codec voicehat-codec: Disabling audio amp...
[145.003336] voicehat-codec voicehat-codec: Disabling audio amp...
[145.557429] voicehat-codec voicehat-codec: Enabling audio amp...
[145.584520] voicehat-codec voicehat-codec: Enabling audio amp...
[145.800336] voicehat-codec voicehat-codec: Disabling audio amp...
[145.800487] voicehat-codec voicehat-codec: Disabling audio amp...
[146.265967] voicehat-codec voicehat-codec: Enabling audio amp...
[146.293802] voicehat-codec voicehat-codec: Enabling audio amp...
[146.512978] voicehat-codec voicehat-codec: Disabling audio amp...
[146.513167] voicehat-codec voicehat-codec: Disabling audio amp...
[147.006695] voicehat-codec voicehat-codec: Enabling audio amp...
[147.034210] voicehat-codec voicehat-codec: Enabling audio amp...
[147.250266] voicehat-codec voicehat-codec: Disabling audio amp...
[147.250408] voicehat-codec voicehat-codec: Disabling audio amp...

```

after that I can not play any audio anymore and pulseaudio is broken.  
I had to reboot my system to get pulseaudio back working again.

So I cannot use pigpiod as it breaks my audio just by starting this daemon.  
I have no idea how I can fix this. I know this is not node-red related.

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [30 September 2023 22:50 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/17 "2023-09-30T22:50:12Z")

</div>

Regarding pigpiod breaking my audio:  
I think I have the same issue as reported here:

- [https://support.hifiberry.com/hc/en-us/community/posts/360009543357-slow-music-audio-output-works-with-Hifiberry-DAC-Zero](https://support.hifiberry.com/hc/en-us/community/posts/360009543357-slow-music-audio-output-works-with-Hifiberry-DAC-Zero)

To fix this I have to run pigpiod with the option `-t 0 `

```auto
pigpiod -t 0

```

So it is at last working !

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [2 October 2023 06:38 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/18 "2023-10-02T06:38:06Z")

</div>

**The solution:**

1. run pigpiod daemon

2. use `node-red-node-pi-gpiod `(version 0.4.0). I have configured the node as follows

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba9a62a5b1cc9a93ff6cccd32cba7b9b1bf35f4e.png)

When sending a brightness 7 (7%) I see no more flickering.  
When increasing/decreasing the brightness every 100ms I still see some flickering. So it is not completely fixed but it is good enough for me.

Here the load on raspberry pi 3B when it is constantly changing the brightness of the LED.

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

Here the load as measured in my node-red docker container

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

---

<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: [16 October 2023 06:38 UTC](https://discourse.nodered.org/t/controlling-led-brightness-using-node-red-node-pi-gpio-with-pwm-results-in-flickering/81680/19 "2023-10-16T06:38:56Z")

</div>

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