Skip to content

Commit eb550c6

Browse files
Fixed PR
Giga PWM exception added
1 parent 45e22ff commit eb550c6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md

+34
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ As a result, you can simulate a specific voltage written to a pin. In the exampl
123123

124124
For this, we need to import `PWM` and `Pin` from the `machine` module.
125125

126+
### Coding with PWM
127+
126128
```python
127129
from machine import Pin, PWM, ADC
128130

@@ -134,3 +136,35 @@ pwm.freq(1000)
134136
while True:
135137
pwm.duty_u16(duty)
136138
```
139+
In this example:
140+
141+
- `pin1` is configured as an output pin with no pull-up or pull-down resistors.
142+
- `timer1` is initialized with a frequency of 1000 Hz.
143+
- `channel1` is created on timer 3, channel 1, and is set to PWM mode.
144+
- `channel1.pulse_width_percent(duty)` sets the duty cycle of the PWM signal as a percentage between 0 and 100.
145+
146+
### PWM on the GIGA R1
147+
148+
On STM32 boards like the Arduino GIGA R1, PWM is handled differently. You need to use the `Timer` class along with the `Pin` class from the `pyb` module.
149+
Here's the correct code that works on these boards:
150+
151+
```python
152+
from pyb import Pin, Timer
153+
154+
p = Pin('A13') # Replace 'A13' with your desired PWM-capable pin
155+
tim = Timer(2, freq=1000)
156+
ch = tim.channel(1, Timer.PWM, pin=p)
157+
ch.pulse_width_percent(25)
158+
```
159+
160+
In this example:
161+
162+
- `p` is a `Pin` object initialized on pin `'A13'`. This pin is set up for PWM output.
163+
- `tim` is a `Timer` object initialized with timer number 2 and a frequency of 1000 Hz.
164+
- `ch` is a PWM channel created on timer 2, channel 1, and is associated with pin `p`.
165+
- `ch.pulse_width_percent(25)` sets the duty cycle of the PWM signal to 25%.
166+
- `'A13'` is the pin markedas **DAC1**
167+
168+
169+
- Pin names and timer configurations, may vary between different STM32 boards. For more information, check out the [GIGA R1 MicroPython Guide](https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-micropython/).
170+

0 commit comments

Comments
 (0)