gobot.io/x/gobot/v2@v2.1.0/system/PWM.md (about) 1 # PWM's 2 3 This document describes some basics for developers. 4 5 ## Check the PWM features 6 7 Example for Tinkerboard: 8 9 ```sh 10 # ls -la /sys/class/pwm/ 11 total 0 12 drwxr-xr-x 2 root root 0 Apr 24 14:11 . 13 drwxr-xr-x 66 root root 0 Apr 24 14:09 .. 14 lrwxrwxrwx 1 root root 0 Apr 24 14:09 pwmchip0 -> ../../devices/platform/ff680000.pwm/pwm/pwmchip0 15 lrwxrwxrwx 1 root root 0 Apr 24 14:09 pwmchip1 -> ../../devices/platform/ff680010.pwm/pwm/pwmchip1 16 lrwxrwxrwx 1 root root 0 Apr 24 14:09 pwmchip2 -> ../../devices/platform/ff680030.pwm/pwm/pwmchip2 17 ``` 18 19 ```sh 20 # ls -la /sys/class/pwm/pwmchip2/ 21 total 0 22 drwxr-xr-x 3 root root 0 Apr 24 14:17 . 23 drwxr-xr-x 3 root root 0 Apr 24 14:17 .. 24 lrwxrwxrwx 1 root root 0 Apr 24 14:17 device -> ../../../ff680030.pwm 25 --w------- 1 root root 4096 Apr 24 14:17 export 26 -r--r--r-- 1 root root 4096 Apr 24 14:17 npwm 27 drwxr-xr-x 2 root root 0 Apr 24 14:17 power 28 lrwxrwxrwx 1 root root 0 Apr 24 14:17 subsystem -> ../../../../../class/pwm 29 -rw-r--r-- 1 root root 4096 Apr 24 14:17 uevent 30 --w------- 1 root root 4096 Apr 24 14:17 unexport 31 ``` 32 33 ## General PWM tests 34 35 Connect an oscilloscope or at least a meter to the pin (used pin32 for example with Tinkerboard). 36 Switch to root user by "su -". 37 38 ### Investigate state of PWMs 39 40 For Tinkerboard: 41 42 * ff680000 and ff680010 seems to be not usable (unknown, which functionality make use of that, maybe fan?) 43 * if there is no ff680020, ff680030, this can be activated. See section [Change available features](README.md#change-available-features) 44 45 ### Creating pwm0 on pwmchip2 46 47 ```sh 48 echo 0 > /sys/class/pwm/pwmchip2/export 49 ``` 50 51 investigate result: 52 53 ```sh 54 # ls /sys/class/pwm/pwmchip2/ 55 device export npwm power pwm0 subsystem uevent unexport 56 57 # ls /sys/class/pwm/pwmchip2/pwm0/ 58 capture duty_cycle enable period polarity power uevent 59 60 # cat /sys/class/pwm/pwmchip2/pwm0/period 61 0 62 63 # cat /sys/class/pwm/pwmchip2/pwm0/duty_cycle 64 0 65 66 # cat /sys/class/pwm/pwmchip2/pwm0/enable 67 0 68 69 # cat /sys/class/pwm/pwmchip2/pwm0/polarity 70 inversed 71 ``` 72 73 ### Initialization of pwm0 74 75 ```sh 76 echo 10000000 > /sys/class/pwm/pwmchip2/pwm0/period 77 echo "normal" > /sys/class/pwm/pwmchip2/pwm0/polarity 78 echo 3000000 > /sys/class/pwm/pwmchip2/pwm0/duty_cycle # this means 30% 79 echo 1 > /sys/class/pwm/pwmchip2/pwm0/enable 80 81 ``` 82 83 > Before writing the period, all other write actions will cause an error "-bash: echo: write error: Invalid argument". 84 > The "period" is in nanoseconds (or a frequency divider for 1GHz), 1000 will produce a frequency of 1MHz, 1000000 will 85 > cause a frequency of 1kHz. For the example 10000000 we have 100Hz. 86 87 Now we should measure a value of around 1V with the meter, because the basis value is 3.3V and 30% leads to 1V. 88 89 Try to inverse the sequence: 90 `echo "inversed" > /sys/class/pwm/pwmchip2/pwm0/polarity` 91 Now we should measure a value of around 2.3V with the meter, which is the difference of 1V to 3.3V. 92 93 If we have attached an oscilloscope we can play around with the values for period and duty_cycle and see what happen. 94 95 ## Links