# Pigpio commands into NodeRed

**URL:** https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221
**Category:** General
**Created:** [11 August 2019 15:25 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221 "2019-08-11T15:25:49Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 15:25 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/1 "2019-08-11T15:25:49Z")

</div>

Hello,  
I'm looking for a way to insert following terminal commands into NodeRed:

sudo pigpiod  
python  
import pigpio  
pi=pigpio.pi()  
pi.hardware\_PWM(12, 25000, 1e6\ ***0.25** ) # 25000Hz 25% dutycycle(normaly)

The whole thing is a fan control and the **0.25** are the duty cycle and should be infinitely adjustable.

Can someone help me plz?

---

<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: [11 August 2019 15:34 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/2 "2019-08-11T15:34:04Z")

</div>

Hi,

a) you can't (easily) run a command as sudo as that is a security privilege escalation. so you need to run pigpiod outside of Node-RED. However once you have done that then  
b) there is a node-red-node-pi-gpiod that can drive it for most use cases - [https://flows.nodered.org/node/node-red-node-pi-gpiod](https://flows.nodered.org/node/node-red-node-pi-gpiod)

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 15:49 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/3 "2019-08-11T15:49:12Z")

</div>

Hi,  
i know this node but i have to run it with the 25khz and there is no option to change it.

---

<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: [11 August 2019 16:37 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/4 "2019-08-11T16:37:04Z")

</div>

If the pigpiod daemon is running then you might be able to set the frequency by calling your exec node using this (not tried but worth a go)

```auto
import pigpio
pi=pigpio.pi()
set_PWM_frequency(12, 25000)

```

NB pigpio doesn't let you choose any frequency value

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

but is the fan really that sensitive to the frequency?

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 16:39 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/5 "2019-08-11T16:39:51Z")

</div>

Hi,  
thanks.

And yes, they are^^

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 17:39 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/6 "2019-08-11T17:39:55Z")

</div>

seem not to work.  
Another idea:  
A Pythonscript with a variable (DutyCycle), but also there I do not know yet really how I should convert it...

---

<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: [11 August 2019 18:24 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/7 "2019-08-11T18:24:14Z")

</div>

Just checking - what physical pin is your fan connected to  
Node-RED uses physical pin numbers so if your connected to pin 12 then you need to use

```auto
set_PWM_frequency(18, 25000)

```

as physical pin 12 is gpio number 18

---

<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: [11 August 2019 19:03 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/8 "2019-08-11T19:03:17Z")

</div>

Just tried it out on a PiZero and it seems to work - with correct syntax that is 🙂

I created a file called fred.py

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cf01b9522134e5e6d8f5c463bf60ef52d2367d1b.png)  
and stuck this in it

```auto
import pigpio
pi=pigpio.pi()
pi.set_PWM_frequency(18, 25000)

```

And injected value of 25 into the pigpio node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/be96d4f358d30712942ff837b399ad503b6db45c.png)

I've put an LED on the pin and it glowed as expected

I then changed the fred.py to

```auto
import pigpio
pi=pigpio.pi()
pi.set_PWM_frequency(18, 5)

```

and this time the LED blinked rapidly

So I think that proves the concept is good 🙂

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 19:23 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/9 "2019-08-11T19:23:39Z")

</div>

Well it doesn't work for me. I get the following error message from exec:

```auto
11.8.2019, 21:22:47node: ac7e4461.29989msg.payload : string[0]
""
11.8.2019, 21:22:47node: ac7e4461.29989msg.payload : string[144]
"Traceback (most recent call last):↵ File "/home/pi/scripts/test.py", line 2, in <module>↵ import pigpio↵ImportError: No module named pigpio↵"
11.8.2019, 21:22:47node: ac7e4461.29989
msg.payload : Object
{ code: 1, message: "Command failed: python /home/p…" }

```

pigpio is runnig cause pigs hp 12 25000 500000 works but the python part dont.

```auto
>>> import pigpio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pigpio

```

I'm a bit confused

---

<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: [11 August 2019 19:33 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/10 "2019-08-11T19:33:17Z")

</div>

Do you get same error if your run

python /home/pi/scripts/test.py

from plain terminal command line?

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 19:34 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/11 "2019-08-11T19:34:45Z")

</div>

yes

```auto
pi@octopi:~ $ python /home/pi/scripts/test.py
Traceback (most recent call last):
  File "/home/pi/scripts/test.py", line 2, in <module>
    import pigpio
ImportError: No module named pigpio

```

---

<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: [11 August 2019 19:47 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/12 "2019-08-11T19:47:09Z")

</div>

That IS wierd ☹

Luckily you can directly call pigs

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/556bf2268ddbd0febe059a149ebe6e36717bc14f.png)

(My LED is on pin 12 - gpio 18)

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 19:51 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/13 "2019-08-11T19:51:33Z")

</div>

Hello,  
yeah, that's what I'm playing with right now.

All I have to do now is figure out how to get a number from the inject block into the output.  
So I would have to  
pigs have hp 12 25000 500000 where the 50000 should come from an input.

---

<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: [11 August 2019 19:57 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/14 "2019-08-11T19:57:21Z")

</div>

You can use incoming msg.payload and get it added onto the end 🙂

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4589cd4016bca58f94e088d50eae2a05908b76fc.png)

Just make sure to add the space at the end

---

<div class="post-metadata">

### Author: ![Gummix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gummix/32/10828_2.png) [@Gummix](https://discourse.nodered.org/u/Gummix)
#### Post date: [11 August 2019 20:04 UTC](https://discourse.nodered.org/t/pigpio-commands-into-nodered/14221/15 "2019-08-11T20:04:40Z")

</div>

> [@Gummix](#):
>
> pigs hp 12 25000 500000

Ah ok, Thanks it works xD
