You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/micropython/01.basics/06.board-examples/board-examples.md
+209-1Lines changed: 209 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1546,4 +1546,212 @@ If you need to stop the audio streaming, you can call `.stop_streaming()`.
1546
1546
1547
1547
```python
1548
1548
audio.stop_streaming()
1549
-
```
1549
+
```
1550
+
1551
+
## Portenta C33
1552
+
1553
+
### Pinout Mapping
1554
+
1555
+
The Portenta C33 has two ways its pins are physically available: through its MKR-styled connectors and its High-Density connectors. Most pins are referred to via their port name or function. In the image below, the Portenta C33 MKR-styled connectors pinout is shown.
1556
+
1557
+
The MKR-styled connectors pinout is mapped in MicroPython as follows:
The complete MicroPython pinout is available and downloadable as PDF from the link below:
1585
+
1586
+
- Portenta C33 MicroPython pinout
1587
+
1588
+
### Input/Output Pins
1589
+
1590
+
The `Pin` class in the `machine` module is essential for controlling Input/Output (I/O) pins of the Portenta C33 board. These pins are crucial for a wide range of applications, including reading sensor data, controlling actuators, and interfacing with other hardware components.
1591
+
1592
+
#### Pin Initialization
1593
+
1594
+
To begin using an I/O pin of the Portenta C33 board with MicroPython, you need to initialize it using the `Pin` class from the `machine` module. This involves specifying the pin identifier and its mode (input, output, etc.).
1595
+
1596
+
```python
1597
+
from machine import Pin
1598
+
1599
+
# Initializing pin P107 as an output
1600
+
p107 = Pin('P107', Pin.OUT)
1601
+
```
1602
+
1603
+
#### Configuring Pin Modes
1604
+
1605
+
You can configure a pin as an input or output. For input pins, it's often useful to activate an internal pull-up or pull-down resistor. This helps to stabilize the input signal, especially in cases where the pin is reading a mechanical switch or a button.
1606
+
1607
+
```python
1608
+
# Configuring pin P105 as an input with its pull-up resistor enabled
1609
+
p105 = Pin('P105', Pin.IN, Pin.PULL_UP)
1610
+
```
1611
+
1612
+
#### Reading from and Writing to Pins
1613
+
1614
+
To read a digital value from a pin, use the `.value()` method without any arguments. This is particularly useful for input pins. Conversely, to write a digital value, use the `.value()` method with an argument. Passing `1` sets the pin to `HIGH`, while `0` sets it to `LOW`. This is applicable to output pins.
1615
+
1616
+
```python
1617
+
# Reading from P105
1618
+
pin_value = p105.value()
1619
+
1620
+
# Writing to P107
1621
+
p107.value(1) # Set p2 to high
1622
+
```
1623
+
1624
+
#### Advanced Pin Configuration
1625
+
1626
+
The Pin class allows dynamic reconfiguration of pins and setting up interrupt callbacks. This feature is essential for creating responsive and interactive applications.
1627
+
1628
+
```python
1629
+
# Reconfiguring P105 as an input with a pull-down resistor
1630
+
p105.init(Pin.IN, Pin.PULL_DOWN)
1631
+
1632
+
# Setting up an interrupt on P105
1633
+
p105.irq(lambdap: print("- IRQ triggered!", p))
1634
+
```
1635
+
1636
+
#### Practical Example
1637
+
1638
+
In this example, we will configure one pin as an input to read the state of a button and another pin as an output to control an LED. The LED will turn on when the button is pressed and off when it's released.
1639
+
1640
+
```python
1641
+
from machine import Pin
1642
+
import time
1643
+
1644
+
# Configure pin P107 as an output (for the LED)
1645
+
led = Pin('P107', Pin.OUT_PP)
1646
+
1647
+
# Configure pin P105 as input with pull-up resistor enabled (for the button)
1648
+
button = Pin('P105', Pin.IN, Pin.PULL_UP)
1649
+
1650
+
whileTrue:
1651
+
# Read the state of the button
1652
+
button_state = button.value()
1653
+
if button_state ==0:
1654
+
# Turn on LED if button is pressed (button_state is LOW)
1655
+
led.value(1)
1656
+
else:
1657
+
# Turn off LED if button is not pressed (button_state is HIGH)
1658
+
led.value(0)
1659
+
1660
+
# Short delay to debounce the button
1661
+
time.sleep(0.1)
1662
+
```
1663
+
1664
+
This practical example demonstrates controlling an LED based on a button's state. The LED, connected to pin `P107` (configured as an output), is turned on or off depending on the button's input read from pin `P105` (set as an input with a pull-up resistor). The main loop continually checks the button's state; pressing the button fixes the LED on while releasing it turns the LED off. A brief delay is included for debouncing, ensuring stable operation without false triggers from the button.
1665
+
1666
+
### Pulse Width Modulation
1667
+
1668
+
Pulse Width Modulation (PWM) is a method to emulate an analog output using a digital pin. It does this by rapidly toggling the pin between low and high states. Two primary aspects define PWM behavior:
1669
+
1670
+
-**Frequency**: This is the speed at which the pin toggles between low and high states. A higher frequency means the pin toggles faster.
1671
+
-**Duty cycle**: This refers to the ratio of the high state duration to the total cycle duration. A 100% duty cycle means the pin remains high all the time, while a 0% duty cycle means it stays low.
1672
+
1673
+
The available PWM pins of the Portenta C33 board in MicroPython are the following:
1674
+
1675
+
|**Available PWM Pins**|
1676
+
|:----------------------:|
1677
+
|`P105`|
1678
+
|`P106`|
1679
+
|`P111`|
1680
+
|`P303`|
1681
+
|`P401`|
1682
+
|`P601`|
1683
+
1684
+
#### Setting Up PWM
1685
+
1686
+
To use PWM, start by initializing a pin and then creating a PWM object associated with that pin.
1687
+
1688
+
```python
1689
+
import machine
1690
+
1691
+
# Initialize a pin for PWM (e.g., pin P105)
1692
+
p105 = machine.Pin('P105')
1693
+
pwm1 = machine.PWM(p105)
1694
+
```
1695
+
1696
+
#### Configuring PWM Parameters
1697
+
1698
+
The frequency and duty cycle of the PWM signal are set based on the specific needs of your application:
1699
+
1700
+
```python
1701
+
# Set the frequency to 500 Hz
1702
+
pwm1.freq(500)
1703
+
1704
+
# Adjusting the duty cycle to 50 for 50% duty
1705
+
pwm1.duty(50)
1706
+
```
1707
+
1708
+
#### Checking PWM Configuration
1709
+
1710
+
You can check the current configuration of the PWM object by printing it:
1711
+
1712
+
```python
1713
+
# Will show the current frequency and duty cycle
1714
+
print(pwm1)
1715
+
```
1716
+
1717
+
Retrieve the frequency and duty cycle values:
1718
+
1719
+
```python
1720
+
current_freq = pwm1.freq()
1721
+
current_duty = pwm1.duty()
1722
+
```
1723
+
1724
+
#### Deinitializing PWM
1725
+
1726
+
When PWM is no longer needed, the pin can be deinitialized:
1727
+
1728
+
```python
1729
+
pwm1.deinit()
1730
+
```
1731
+
1732
+
#### Practical Example
1733
+
1734
+
In this example, we will use PWM to control the brightness of an LED connected to pin `P105` of the Portenta C33 board.
0 commit comments