Skip to content

[MKC-1832] MicroPython update examples with GIGA exception #2190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ As a result, you can simulate a specific voltage written to a pin. In the exampl

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

### Coding with PWM

```python
from machine import Pin, PWM, ADC

Expand All @@ -134,3 +136,36 @@ pwm.freq(1000)
while True:
pwm.duty_u16(duty)
```
In this example:

- `Pin(15)` is attached to PWM
- `duty` is set at 3000
- `freq` is set at 1 Hz

### PWM on the GIGA R1

On STM32 boards like the [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/giga-r1-wifi), PWM is handled differently. You need to use the `Timer` class along with the `Pin` class from the `pyb` module.

Here's the correct code that works on these boards:

```python
from pyb import Pin, Timer

p = Pin('A13') # Replace 'A13' with your desired PWM-capable pin
tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(25)
```

In this example:

- `p` is a `Pin` object initialized on pin `'A13'`. This pin is set up for PWM output.
- `tim` is a `Timer` object initialized with timer number 2 and a frequency of 1000 Hz.
- `ch` is a PWM channel created on timer 2, channel 1, and is associated with pin `p`.
- `ch.pulse_width_percent(25)` sets the duty cycle of the PWM signal to 25%.
- `'A13'` is marked as **DAC1** on the GIGA R1 WiFi



- 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/).

Loading