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

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.

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

My node-red version:

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

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

or the top statistics from the host machine:

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

So I am wondering if 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).

node-red-node-pi-gpio uses 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.

1 Like

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.

1 Like

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

So I am considering to switch to node-red-node-pi-gpiod which is not based on RPi.GPIO but on pigpio daemon.
Would that use DMA timed PWM ?

1 Like

Apparently pigpio PWM uses DMA.
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 so it looks like switching to node-red-node-pi-gpiod is essential.

1 Like

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

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.

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 :slight_smile:

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

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

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

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

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.

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

info_ieee-pem_2014-09_led-flickering.pdf (bio-licht.org)

visible light - Limit of human eye flicker perception? - Physics Stack Exchange

All seems quite complex. :slight_smile:

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

I have installed pigpiod version V79 using

sudo apt-get install pigpio

If I start the deamon using command:

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:

[  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.

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

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

pigpiod -t 0

So it is at last working !

The solution:

  1. run pigpiod daemon

    • Assure you are running it with option -t 0 otherwise this may break audio (see above comments in this thread).
      • I ran it as a service so I have to specify the option -t 0 in /etc/systemd/system/pigpiod.service.d/public.conf
    • run the deamon on the host, not in a docker container. The versions running in docker container seem to give serious problems when the host becomes rebooted (e.g. no network connectivity).
  2. use node-red-node-pi-gpiod (version 0.4.0). I have configured the node as follows

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.

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

1 Like

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