Jump to content

Hardware PWM on Orange Pi Zero H2+ updates very slowly with script


Zeko9381
Go to solution Solved by Zeko9381,

Recommended Posts

I'm running the latest 5.10.21-sunxi kernel. I figured out how to use the PWM pin with the help of this post.

 

I have an LED connected to the PWM pin and when I run for example:

sudo sh -c "echo 250 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"

the LED updates instantly. But when I try to fade the LED with a script, the LED always takes 6 seconds to update.

 

This is the script:

#!/bin/bash

#Export PWM channel for user control.
sh -c "echo 0 > /sys/class/pwm/pwmchip0/export"

#Select the period of PWM signal. Value is in nanoseconds.
sh -c "echo 500 > /sys/class/pwm/pwmchip0/pwm0/period"

#Change the polarity of the PWM signal.
#The polarity can only be changed if the PWM is not enabled.
sh -c "echo "normal" > /sys/class/pwm/pwmchip0/pwm0/polarity"

#Enable the PWM signal.
sh -c "echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable"

for i in {0..500}
do
        sh -c "echo $i > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"
        cat /sys/class/pwm/pwmchip0/pwm0/duty_cycle #Just for debugging
        sleep 0.1
done

#Disable the PWM signal.
sh -c "echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable"

#UnExport PWM channel for user control.
sh -c "echo 0 > /sys/class/pwm/pwmchip0/unexport"

 

I hope someone can help me. It's so hard to find any resources for this sort of thing.

Link to comment
Share on other sites

  • Solution

Ok, this was a pretty dumb post. This example that I found seems to be incorrect. The value for the period isn't in nanoseconds, but in picoseconds, so the correct value would actually be 500000. 500 picosecond period would mean a frequency of 2 MHz, which is way too high. The problem wasn't that the LED took too long to update, but that the resolution at this frequency was way too low.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines