From eb550c6f4a6cb788572b61b569e1f36c24272283 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:06:02 +0200 Subject: [PATCH 1/3] Fixed PR Giga PWM exception added --- .../digital-analog-pins.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 72437cd47f..84b6716817 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -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 @@ -134,3 +136,35 @@ pwm.freq(1000) while True: pwm.duty_u16(duty) ``` +In this example: + +- `pin1` is configured as an output pin with no pull-up or pull-down resistors. +- `timer1` is initialized with a frequency of 1000 Hz. +- `channel1` is created on timer 3, channel 1, and is set to PWM mode. +- `channel1.pulse_width_percent(duty)` sets the duty cycle of the PWM signal as a percentage between 0 and 100. + +### PWM on the GIGA R1 + +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. +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 the pin markedas **DAC1** + + +- 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/). + From f880604eb2052c3a628e1f01a0c17ca4b60e4606 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:11:46 +0200 Subject: [PATCH 2/3] Updated --- .../05.digital-analog-pins/digital-analog-pins.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 84b6716817..076c06ddde 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -138,10 +138,9 @@ while True: ``` In this example: -- `pin1` is configured as an output pin with no pull-up or pull-down resistors. -- `timer1` is initialized with a frequency of 1000 Hz. -- `channel1` is created on timer 3, channel 1, and is set to PWM mode. -- `channel1.pulse_width_percent(duty)` sets the duty cycle of the PWM signal as a percentage between 0 and 100. +- `Pin(15)` is attached to PWM +- `duty` is set at 3000 +- `freq` is set at 1 Hz ### PWM on the GIGA R1 From f516682fc673d0c42d767a1f31acf017a41883ec Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:50:50 +0200 Subject: [PATCH 3/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Karl Söderby <35461661+karlsoderby@users.noreply.github.com> --- .../01.basics/05.digital-analog-pins/digital-analog-pins.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 076c06ddde..5ba0ba6616 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -144,7 +144,8 @@ In this example: ### PWM on the GIGA R1 -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. +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 @@ -162,7 +163,8 @@ In this example: - `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 the pin markedas **DAC1** +- `'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/).