# Using simple pwm.py sometimes doesn't work

**URL:** <https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999>\
**Category:** Jetson AGX Orin\
**Tags:** gpio, nvbugs\
**Created:** [September 11, 2023, 3:26am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999 "2023-09-11T03:26:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 11, 2023, 3:26am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/1 "2023-09-11T03:26:41Z")

</div>

My platform is orin, we use external PWM trigger to synchronize between multiple lenses, the lens is controlled by the pulse width to control the exposure. The commands to control exposure are as follows:

```auto
echo 99800000 > /sys/class/pwm/pwmchip2/pwm0/duty_cycle

```

The code for simple pwm.py is as follows：

```auto
import RPi.GPIO as GPIO
import time

output_pins = {
    'JETSON_XAVIER': 18,
    'JETSON_NANO': 33,
    'JETSON_NX': 33,
    'CLARA_AGX_XAVIER': 18,
    'JETSON_TX2_NX': 32,
    'JETSON_ORIN': 18,
    'JETSON_ORIN_NX': 33,
    'JETSON_ORIN_NANO': 33
}
output_pin = output_pins.get(GPIO.model, None)
if output_pin is None:
    raise Exception('PWM not supported on this board')

print(output_pin)
def main():
    # Pin Setup:
    # Board pin-numbering scheme
    GPIO.setmode(GPIO.BOARD)
    # set pin as an output pin with optional initial state of HIGH
    GPIO.setup(output_pin, GPIO.OUT, initial=GPIO.HIGH)
    p = GPIO.PWM(output_pin, 10)
    val = 100
    incr = 5
    p.start(val)

    print("PWM running. Press CTRL+C to exit.")
    try:
        while True:
            time.sleep(0.25)
            #if val >= 100:
             # incr = -incr
            #if val <= 0:
            # incr = -incr
            #val += incr
            #p.ChangeDutyCycle(val)
    finally:
        p.stop()
        GPIO.cleanup()

if __name__ == ' __main__':
    main()

```

We will start the simple pwm.py script first, and then use the command to control the exposure. But I ran into a problem when I set the value above 99800000 and there was no trigger signal. May I ask why?

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 11, 2023, 5:54am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/3 "2023-09-11T05:54:07Z")

</div>

Hi 1508723374,

Are you using the devkit or custom board for AGX Orin?  
What’s your Jetpack version in use?

What’s the current period for PWM

```auto
$ cat /sys/class/pwm/pwmchip2/pwm0/period

```

> [@1508723374](#):
>
> when I set the value above 99800000

Does the PWM work as expected if you set the value equal to 99800000 or less than it?

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 11, 2023, 6:42am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/4 "2023-09-11T06:42:06Z")

</div>

Yes, I used a custom adapter to connect four camera.

```auto
xxx@xxx-desktop:~$ cat /etc/nv_tegra_release
# R35 (release), REVISION: 3.1, GCID: 32827747, BOARD: t186ref, EABI: aarch64, DATE: Sun Mar 19 15:19:21 UTC 2023

```

```auto
xxx@xxx-desktop:~$ cat /sys/class/pwm/pwmchip2/pwm0/period
100000000

```

It works when I set it 200000~99800000.

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 11, 2023, 7:25am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/5 "2023-09-11T07:25:33Z")

</div>

99800000 seems resulting 99.8% duty cycle. What the range of the duty cycle would you need for your use case?

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 11, 2023, 8:02am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/6 "2023-09-11T08:02:05Z")

</div>

I need it to be about 10000~99990000

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 11, 2023, 9:13am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/9 "2023-09-11T09:13:38Z")

</div>

Could you share the result of the following command? (if you are using `pwm5` from 40-header pins)

```auto
$ cat /sys/kernel/debug/bpmp/debug/clk/pwm5/parent

```

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 12, 2023, 12:51am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/10 "2023-09-12T00:51:35Z")

</div>

99800000 can work normally, 998100000 can not work, there is no difference between the two.

```auto
xxx@xxx-desktop:~$ echo 99800000 > /sys/class/pwm/pwmchip2/pwm0/duty_cycle
xxx@xxx-desktop:~$ sudo cat /sys/kernel/debug/bpmp/debug/clk/pwm5/parent  
pllp_out0
xxx@xxx-desktop:~$ echo 99810000 > /sys/class/pwm/pwmchip2/pwm0/duty_cycle 
xxx@xxx-desktop:~$ sudo cat /sys/kernel/debug/bpmp/debug/clk/pwm5/parent  
pllp_out0

```

99800000

 ![99800000](https://global.discourse-cdn.com/nvidia/original/4X/0/3/b/03b8b009546a4c7c265bef15e73786fb9bab0103.jpeg)  
99810000  
 ![998100000](https://global.discourse-cdn.com/nvidia/original/4X/f/9/c/f9cf60516a760701a7f628917d774b90dfe36ca6.jpeg)

```auto
xxx@xxx-desktop:~$ sudo cat /sys/kernel/debug/pwm
[sudo] password for xxx: 
platform/39c0000.tachometer, 1 PWM device
 pwm-0 ((null) ): period: 0 ns duty: 0 ns polarity: normal

platform/32f0000.pwm, 1 PWM device
 pwm-0 ((null) ): period: 0 ns duty: 0 ns polarity: normal

platform/32c0000.pwm, 1 PWM device
 pwm-0 (sysfs ): requested enabled period: 100000000 ns duty: 99810000 ns polarity: normal

platform/32a0000.pwm, 1 PWM device
 pwm-0 (pwm-fan ): requested enabled period: 45334 ns duty: 11200 ns polarity: normal

platform/3280000.pwm, 1 PWM device
 pwm-0 ((null) ): period: 0 ns duty: 0 ns polarity: normal

```

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 12, 2023, 5:58am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/11 "2023-09-12T05:58:53Z")

</div>

I’ve checked with internal.  
The clock rate of parent clock (pllp\_out0) for `pwm5` is 408M, which results in max rate at **1.5M** (clk\_rate / (256 \* 1))  
You could find more details in TRM document (_PWM-\>Programming Guidelines_).

Please configure pwm period to 1.5M and try different value for duty cycle.

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 12, 2023, 7:39am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/12 "2023-09-12T07:39:25Z")

</div>

According to the formula, it is not clear why there is a limit of 200000~99800000. Because the project requires that the hardware trigger needs to be triggered ten times per second, so the period here can only be set to 100000000, and I hope that the duty cycle can be set to 99.99% on this basis. There is also how I can determine the current PWM5 clock size.

 ![image](https://global.discourse-cdn.com/nvidia/original/4X/e/2/f/e2fd33156e82d89ba6e7423a55c3b772b95e519b.png)

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 12, 2023, 9:29am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/13 "2023-09-12T09:29:48Z")

</div>

> [@1508723374](#):
>
> ten times per second

Do you mean 10Hz? Why you need to configure the period to 100M?

> [@1508723374](#):
>
> it is not clear why there is a limit of 200000~99800000.

It is relating to the clock accuracy.  
Please just configure the `/sys/class/pwm/pwmchip2/pwm0/period` to 1000000 and try with different duty cycle (0%-100%)

> [@1508723374](#):
>
> There is also how I can determine the current PWM5 clock size.

The current clock input is 408MHz.

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 13, 2023, 1:10am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/14 "2023-09-13T01:10:31Z")

</div>

> [@KevinFFF](#):
>
> Please just configure the `/sys/class/pwm/pwmchip2/pwm0/period` to 1000000 and try with different duty cycle (0%-100%)

Thank you very much for your patient reply. I have tried the method you mentioned, and his working range is between 2000 and 998000, which is a hundred times smaller than the previous phenomenon.

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 13, 2023, 1:24am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/15 "2023-09-13T01:24:47Z")

</div>

> [@KevinFFF](#):
>
> Do you mean 10Hz? Why you need to configure the period to 100M?

I think of it this way, 100m =100ms, 1S/100ms =10 times. It’s the only way to actually trigger ten times.

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 13, 2023, 6:28am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/16 "2023-09-13T06:28:20Z")

</div>

hello， [KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)Moderator

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [September 13, 2023, 8:43am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/17 "2023-09-13T08:43:45Z")

</div>

> [@1508723374](#):
>
> I think of it this way, 100m =100ms, 1S/100ms =10 times. It’s the only way to actually trigger ten times.

Sorry that I’m not clear about these relationship.  
Could you help to clarify and explain more details? Why 10Hz (10 times/second) results period to be 100M?

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 13, 2023, 8:53am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/18 "2023-09-13T08:53:58Z")

</div>

Okay, you’re not quite understanding what I’m saying, so let’s ignore the frequency. This period is measured in nanoseconds, and if I set the period to 100000000 ns, that means I have 10 cycles per second. The number of cycles is the trigger frequency I need.

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [September 14, 2023, 1:09am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/19 "2023-09-14T01:09:14Z")

</div>

Hello [KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF), is there any other way I can set it to 99990000?

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [November 1, 2023, 8:32am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/20 "2023-11-01T08:32:07Z")

</div>

Could you try using clk\_32k as parent clock and configure 256 as rate?

```auto
# cd /sys/kernel/debug/bpmp/debug/clk/pwm5
# echo clk_32k > parent
# echo 256 > rate
# cat rate

```

---

<div class="post-metadata">

**Author:** ![1508723374](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@1508723374](https://forums.developer.nvidia.com/u/1508723374)\
**Post date:** [November 2, 2023, 8:59am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/21 "2023-11-02T08:59:10Z")

</div>

Setting the parent clock according to your method still does not work, setting 99990000 is still a problem.  
 ![image](https://global.discourse-cdn.com/nvidia/original/4X/6/8/8/688ac8e38ab882ecbf7c5eb776ae635f79847cd1.png)

---

<div class="post-metadata">

**Author:** ![KevinFFF](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinfff/32/15508_2.png) [@KevinFFF](https://forums.developer.nvidia.com/u/KevinFFF)\
**Post date:** [November 29, 2023, 1:33am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/23 "2023-11-29T01:33:59Z")

</div>

Hi @1508723374,

> [@1508723374](#):
>
> still does not work,

What do you mean about “not work”?  
Could you configure this value w/o modifying the parent clock for pwm0?  
Or there’s no waveform output when you configure this value.

```auto
# echo 10000 > period

```

---

<div class="post-metadata">

**Author:** ![kayccc](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kayccc/32/14043_2.png) [@kayccc](https://forums.developer.nvidia.com/u/kayccc)\
**Post date:** [December 19, 2023, 1:59am UTC](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999/24 "2023-12-19T01:59:56Z")

</div>



[Next page](https://forums.developer.nvidia.com/t/using-simple-pwm-py-sometimes-doesnt-work/265999.md?page=2)
