@@ -11,16 +11,20 @@ compared with the length of a single period (low plus high time). Maximum
11
11
duty cycle is when the pin is high all of the time, and minimum is when it is
12
12
low all of the time.
13
13
14
- * More comprehensive example with all 16 PWM channels and 8 timers::
14
+ * More comprehensive example with all ** 16 PWM channels and 8 timers ** ::
15
15
16
+ from time import sleep
16
17
from machine import Pin, PWM
17
18
try:
18
- f = 100 # Hz
19
- d = 1024 // 16 # 6.25%
20
- pins = (15, 2, 4, 16, 18, 19, 22, 23, 25, 26, 27, 14 , 12, 13 , 32, 33)
19
+ F = 10000 # Hz
20
+ D = 65536 // 16 # 6.25%
21
+ pins = (2, 4, 12, 13, 14, 15, 16, 18, 19, 22, 23, 25, 26, 27, 32, 33)
21
22
pwms = []
22
23
for i, pin in enumerate(pins):
23
- pwms.append(PWM(Pin(pin), freq=f * (i // 2 + 1), duty= 1023 if i==15 else d * (i + 1)))
24
+ f = F * (i // 2 + 1)
25
+ d = min(65535, D * (i + 1))
26
+ pwms.append(PWM(pin, freq=f, duty_u16=d))
27
+ sleep(2 / f)
24
28
print(pwms[i])
25
29
finally:
26
30
for pwm in pwms:
@@ -31,65 +35,100 @@ low all of the time.
31
35
32
36
Output is::
33
37
34
- PWM(Pin(15), freq=100, duty=64, resolution=10, mode=0, channel=0, timer=0)
35
- PWM(Pin(2), freq=100, duty=128, resolution=10, mode=0, channel=1, timer=0)
36
- PWM(Pin(4), freq=200, duty=192, resolution=10, mode=0, channel=2, timer=1)
37
- PWM(Pin(16), freq=200, duty=256, resolution=10, mode=0, channel=3, timer=1)
38
- PWM(Pin(18), freq=300, duty=320, resolution=10, mode=0, channel=4, timer=2)
39
- PWM(Pin(19), freq=300, duty=384, resolution=10, mode=0, channel=5, timer=2)
40
- PWM(Pin(22), freq=400, duty=448, resolution=10, mode=0, channel=6, timer=3)
41
- PWM(Pin(23), freq=400, duty=512, resolution=10, mode=0, channel=7, timer=3)
42
- PWM(Pin(25), freq=500, duty=576, resolution=10, mode=1, channel=0, timer=0)
43
- PWM(Pin(26), freq=500, duty=640, resolution=10, mode=1, channel=1, timer=0)
44
- PWM(Pin(27), freq=600, duty=704, resolution=10, mode=1, channel=2, timer=1)
45
- PWM(Pin(14), freq=600, duty=768, resolution=10, mode=1, channel=3, timer=1)
46
- PWM(Pin(12), freq=700, duty=832, resolution=10, mode=1, channel=4, timer=2)
47
- PWM(Pin(13), freq=700, duty=896, resolution=10, mode=1, channel=5, timer=2)
48
- PWM(Pin(32), freq=800, duty=960, resolution=10, mode=1, channel=6, timer=3)
49
- PWM(Pin(33), freq=800, duty=1023, resolution=10, mode=1, channel=7, timer=3)
50
-
51
- * Example of a smooth frequency change::
38
+ PWM(Pin(2), freq=10000, duty_u16=4096)
39
+ PWM(Pin(4), freq=10000, duty_u16=8192)
40
+ PWM(Pin(12), freq=20000, duty_u16=12288)
41
+ PWM(Pin(13), freq=20000, duty_u16=16384)
42
+ PWM(Pin(14), freq=30030, duty_u16=20480)
43
+ PWM(Pin(15), freq=30030, duty_u16=24576)
44
+ PWM(Pin(16), freq=40000, duty_u16=28672)
45
+ PWM(Pin(18), freq=40000, duty_u16=32768)
46
+ PWM(Pin(19), freq=50000, duty_u16=36864)
47
+ PWM(Pin(22), freq=50000, duty_u16=40960)
48
+ PWM(Pin(23), freq=60060, duty_u16=45056)
49
+ PWM(Pin(25), freq=60060, duty_u16=49152)
50
+ PWM(Pin(26), freq=69930, duty_u16=53248)
51
+ PWM(Pin(27), freq=69930, duty_u16=57344)
52
+ PWM(Pin(32), freq=80000, duty_u16=61440)
53
+ PWM(Pin(33), freq=80000, duty_u16=65535)
54
+
55
+
56
+ * Example of a **smooth frequency change **::
52
57
53
58
from time import sleep
54
59
from machine import Pin, PWM
55
60
56
- F_MIN = 500
57
- F_MAX = 1000
61
+ F_MIN = 1000
62
+ F_MAX = 10000
58
63
59
64
f = F_MIN
60
- delta_f = 1
65
+ delta_f = F_MAX // 50
61
66
62
- p = PWM(Pin(5), f)
63
- print(p)
67
+ pwm = PWM(Pin(27), f)
64
68
65
69
while True:
66
- p.freq(f)
67
-
68
- sleep(10 / F_MIN)
70
+ pwm.freq(f)
71
+ sleep(1 / f)
72
+ sleep(0.1)
73
+ print(pwm)
69
74
70
75
f += delta_f
71
- if f >= F_MAX or f <= F_MIN:
76
+ if f > F_MAX or f < F_MIN:
72
77
delta_f = -delta_f
78
+ print()
79
+ if f > F_MAX:
80
+ f = F_MAX
81
+ elif f < F_MIN:
82
+ f = F_MIN
73
83
74
- See PWM wave at Pin(5) with an oscilloscope.
84
+ See PWM wave on Pin(27) with an oscilloscope.
85
+
86
+ Output is::
75
87
76
- * Example of a smooth duty change::
88
+ PWM(Pin(27), freq=998, duty_u16=32768)
89
+ PWM(Pin(27), freq=1202, duty_u16=32768)
90
+ PWM(Pin(27), freq=1401, duty_u16=32768)
91
+ PWM(Pin(27), freq=1598, duty_u16=32768)
92
+ ...
93
+ PWM(Pin(27), freq=9398, duty_u16=32768)
94
+ PWM(Pin(27), freq=9615, duty_u16=32768)
95
+ PWM(Pin(27), freq=9804, duty_u16=32768)
96
+ PWM(Pin(27), freq=10000, duty_u16=32768)
97
+
98
+ PWM(Pin(27), freq=10000, duty_u16=32768)
99
+ PWM(Pin(27), freq=9804, duty_u16=32768)
100
+ PWM(Pin(27), freq=9615, duty_u16=32768)
101
+ PWM(Pin(27), freq=9398, duty_u16=32768)
102
+ ...
103
+ PWM(Pin(27), freq=1598, duty_u16=32768)
104
+ PWM(Pin(27), freq=1401, duty_u16=32768)
105
+ PWM(Pin(27), freq=1202, duty_u16=32768)
106
+ PWM(Pin(27), freq=998, duty_u16=32768)
107
+
108
+
109
+ * Example of a **smooth duty change **::
77
110
78
111
from time import sleep
79
112
from machine import Pin, PWM
80
113
81
- DUTY_MAX = 2**16 - 1
114
+ DUTY_MAX = 65535
82
115
83
116
duty_u16 = 0
84
- delta_d = 16
117
+ delta_d = 256
85
118
86
- p = PWM(Pin(5), 1000, duty_u16=duty_u16)
87
- print(p)
119
+ pwm = PWM(Pin(27), freq=1000, duty_u16=duty_u16)
88
120
89
121
while True:
90
- p.duty_u16(duty_u16)
122
+ pwm.duty_u16(duty_u16)
123
+ sleep(2 / pwm.freq())
124
+ print(pwm)
91
125
92
- sleep(1 / 1000)
126
+ if duty_u16 >= DUTY_MAX:
127
+ print()
128
+ sleep(2)
129
+ elif duty_u16 <= 0:
130
+ print()
131
+ sleep(2)
93
132
94
133
duty_u16 += delta_d
95
134
if duty_u16 >= DUTY_MAX:
@@ -99,9 +138,106 @@ low all of the time.
99
138
duty_u16 = 0
100
139
delta_d = -delta_d
101
140
102
- See PWM wave at Pin(5) with an oscilloscope.
141
+ PWM wave on Pin(27) with an oscilloscope.
142
+
143
+ Output is::
144
+
145
+ PWM(Pin(27), freq=998, duty_u16=0)
146
+ PWM(Pin(27), freq=998, duty_u16=256)
147
+ PWM(Pin(27), freq=998, duty_u16=512)
148
+ PWM(Pin(27), freq=998, duty_u16=768)
149
+ PWM(Pin(27), freq=998, duty_u16=1024)
150
+ ...
151
+ PWM(Pin(27), freq=998, duty_u16=64512)
152
+ PWM(Pin(27), freq=998, duty_u16=64768)
153
+ PWM(Pin(27), freq=998, duty_u16=65024)
154
+ PWM(Pin(27), freq=998, duty_u16=65280)
155
+ PWM(Pin(27), freq=998, duty_u16=65535)
156
+
157
+ PWM(Pin(27), freq=998, duty_u16=65279)
158
+ PWM(Pin(27), freq=998, duty_u16=65023)
159
+ PWM(Pin(27), freq=998, duty_u16=64767)
160
+ PWM(Pin(27), freq=998, duty_u16=64511)
161
+ ...
162
+ PWM(Pin(27), freq=998, duty_u16=1023)
163
+ PWM(Pin(27), freq=998, duty_u16=767)
164
+ PWM(Pin(27), freq=998, duty_u16=511)
165
+ PWM(Pin(27), freq=998, duty_u16=255)
166
+ PWM(Pin(27), freq=998, duty_u16=0)
167
+
168
+
169
+ * Example of a **smooth duty change and PWM output inversion **::
170
+
171
+ from utime import sleep
172
+ from machine import Pin, PWM
173
+
174
+ try:
175
+ DUTY_MAX = 65535
176
+
177
+ duty_u16 = 0
178
+ delta_d = 65536 // 32
179
+
180
+ pwm = PWM(Pin(27))
181
+ pwmi = PWM(Pin(32), invert=1)
182
+
183
+ while True:
184
+ pwm.duty_u16(duty_u16)
185
+ pwmi.duty_u16(duty_u16)
186
+
187
+ duty_u16 += delta_d
188
+ if duty_u16 >= DUTY_MAX:
189
+ duty_u16 = DUTY_MAX
190
+ delta_d = -delta_d
191
+ elif duty_u16 <= 0:
192
+ duty_u16 = 0
193
+ delta_d = -delta_d
194
+
195
+ sleep(.01)
196
+ print(pwm)
197
+ print(pwmi)
198
+
199
+ finally:
200
+ try:
201
+ pwm.deinit()
202
+ except:
203
+ pass
204
+ try:
205
+ pwmi.deinit()
206
+ except:
207
+ pass
208
+
209
+ Output is::
210
+
211
+ PWM(Pin(27), freq=5000, duty_u16=0)
212
+ PWM(Pin(32), freq=5000, duty_u16=32768, invert=1)
213
+ PWM(Pin(27), freq=5000, duty_u16=2048)
214
+ PWM(Pin(32), freq=5000, duty_u16=2048, invert=1)
215
+ PWM(Pin(27), freq=5000, duty_u16=4096)
216
+ PWM(Pin(32), freq=5000, duty_u16=4096, invert=1)
217
+ PWM(Pin(27), freq=5000, duty_u16=6144)
218
+ PWM(Pin(32), freq=5000, duty_u16=6144, invert=1)
219
+ PWM(Pin(27), freq=5000, duty_u16=8192)
220
+ PWM(Pin(32), freq=5000, duty_u16=8192, invert=1)
221
+ ...
222
+
223
+
224
+ See PWM waves on Pin(27) and Pin(32) with an oscilloscope.
225
+
226
+ Note: New PWM parameters take effect in the next PWM cycle.
227
+
228
+ pwm = PWM(2, duty=512)
229
+ print(pwm)
230
+ >>> PWM(Pin(2 ), freq = 5000 , duty = 1023 ) # the duty is not relevant
231
+ pwm.init(freq=2, duty=64)
232
+ print(pwm)
233
+ >>> PWM(Pin(2 ), freq = 2 , duty = 16 ) # the duty is not relevant
234
+ time.sleep(1 / 2) # wait one PWM period
235
+ print(pwm)
236
+ >>> PWM(Pin(2 ), freq = 2 , duty = 64 ) # the duty is actual
237
+
238
+ Note: machine.freq(20_000_000) reduces the highest PWM frequency to 10 MHz.
103
239
104
- Note: the Pin.OUT mode does not need to be specified. The channel is initialized
240
+ Note: the Pin.OUT mode does not need to be specified. The channel is initialized
105
241
to PWM mode internally once for each Pin that is passed to the PWM constructor.
106
242
107
243
The following code is wrong::
0 commit comments