Loading...
Searching...
No Matches

Pin names, the pin map, digital pins and PWM. More...

Functions

 gpio (name)
 Gets the GPIO number from the board header name.
 pin (name, mode="in", pull=None, value=None)
 Configures the GPIO pin from board header name.
 pwm (name, freq=1000, duty_u16=0)
 Configures GPIO pin for PWM output.

Variables

dict PINS
 Every pin name on the board and the RP2040 GPIO it connects to.

Detailed Description

Pin names, the pin map, digital pins and PWM.

Pins use the names printed on the board, as strings: "D0"-"D13", "D18", "D19", "A0"-"A5", "SDA", "SCL" and "LED". Names are not case-sensitive. There are no D14-D17.

Name RP2040 Notes
D0, D1 GPIO1, GPIO0 UART0 RX, TX: uart(0)
D2, D3 GPIO6, GPIO7
D4, D5 GPIO8, GPIO9 UART1 TX, RX: uart(1)
D6, D7 GPIO10, GPIO15
D8, D9 GPIO16, GPIO17
D10-D13 GPIO13, 11, 12, 14 SPI1 CS, MOSI, MISO, SCK: spi()
D18 / SDA, D19 / SCL GPIO24, GPIO25 I2C0: i2c(). No pull-ups on the board
LED GPIO5 Green built-in LED: On-board LED
A0-A2 GPIO26-28 RP2040 ADC, 0-3.3 V. Can also be used as digital pins
A3-A5 MAX11635 AIN0-2 On-board ADC, 0-2.048 V. Analogue only

Some pairs of pins share a PWM channel, so they can't run different PWM signals at once: D1/D8, D0/D9, D4/D18, D5/D19, D6/A0, D11/A1, D12/A2. pwm() refuses the second pin of a pair until pin() is called on the first.

Function Documentation

◆ gpio()

gpio ( name)

Gets the GPIO number from the board header name.

For MicroPython's own machine module, which numbers pins by GPIO. Two names can share a GPIO: "D18" and "SDA" are both GPIO24.

Parameters
namePin name, e.g. "D7" (not case-sensitive).
Returns
GPIO number, e.g. gpio("D7") is 15.
Exceptions
ValueErrorThe name is unknown, or is A3-A5 (no RP2040 GPIO).
TypeErrorThe name is not a string.

◆ pin()

pin ( name,
mode = "in",
pull = None,
value = None )

Configures the GPIO pin from board header name.

Sets the pin configuration and returns GPIO interface. The following functions can be used to set the state of the pin or read.

  • .value(1) : Sets pin state HIGH
  • .value(0) : Sets pin state LOW
  • .on() : Sets pin state HIGH
  • .off() : Sets pin state LOW
  • value() : Read the state of the pin.

A0-A2 can be configured to work as digital pins as well.

In addition to writing & reading the pin states with the returned Pin class, the GPIO can be configured INPUT/OUTPUT or OPEN DRAIN. Internal pull-ups and pull-down resistors are also available. If function parameters listed below for GPIO configuration are left output of the function all the default setting is : DIRECTION := INPUT, PULL := NONE, and VALUE = 0.

If the pin was on PWM, calling pin() reverts the GPIO pin back to GPIO control not the peripheral it was attached to.

Note
LED GPIO pin is set to 12 mA drive for proper LED illumination by default.

Refer to MicroPython documentation for more information about the returned object : https://docs.micropython.org/en/latest/library/machine.Pin.html

Parameters
namePin name: "D0"-"D13", "D18", "D19", "A0"-"A2", "SDA", "SCL" or "LED".
mode"in", "out" or "open_drain" (drives low for 0, lets go for 1).
pullNone, "up" or "down": the RP2040's built-in pull resistor.
valueStarting level for outputs, 0 or 1; None leaves it unchanged.
Returns
A machine.Pin.
Exceptions
ValueErrorUnknown name, mode or pull, or an analog-only pin (A3-A5).
TypeErrorThe name is not a string.
Example
led = mcu.pin("D7", "out")
led.value(1)
button = mcu.pin("D2", "in", pull="up")

◆ pwm()

pwm ( name,
freq = 1000,
duty_u16 = 0 )

Configures GPIO pin for PWM output.

PWM generators on the RP2040 are set up in pairs, a PWM sliced with outputs A and B. PWM frequency can set on a per-slice basis. If a 2 PWM pins share the same PWM slice the PWM frequency of both pins will be the same. However, the user can change the duty cycle of each pair separately. PWM that share PWM slices are as follows:

- D1/D8
- D0/D9
- D4/D18
- D5/D19
- D6/A0
- D11/A1
- D12/A2

Calling pin() on a pin frees its output.

Note
Set the duty to 0 before your program ends if that matters (a buzzer or a motor).

Refer to MicroPython documentation for more information about the returned object : https://docs.micropython.org/en/latest/library/machine.PWM.html

Parameters
namePin name: "D0"-"D13", "D18", "D19", "A0"-"A2", "SDA", "SCL" or "LED".
freqFrequency in Hz, 8 Hz and up.
duty_u16Starting duty cycle, 0 (always low) to 65535 (always high); 32768 is high half the time.
Returns
A machine.PWM; change it with .freq(hz) and .duty_u16(duty).
Exceptions
ValueErrorUnknown name, an analogue-only pin (A3-A5), or the pin shares its PWM output with one already in use.
Example
glow = mcu.pwm("LED")
glow.duty_u16(32768) # half brightness
buzzer = mcu.pwm("D3", freq=440, duty_u16=32768)

Variable Documentation

◆ PINS

dict PINS
Initial value:
= {
"D0": 1, # UART0 RX
"D1": 0, # UART0 TX
"D2": 6,
"D3": 7,
"D4": 8, # UART1 TX
"D5": 9, # UART1 RX
"D6": 10,
"D7": 15,
"D8": 16,
"D9": 17,
"D10": 13, # SPI1 CS
"D11": 11, # SPI1 MOSI
"D12": 12, # SPI1 MISO
"D13": 14, # SPI1 SCK
"D18": 24, # I2C0 SDA
"D19": 25, # I2C0 SCL
"SDA": 24,
"SCL": 25,
"LED": 5,
"A0": 26, # RP2040 ADC0
"A1": 27, # RP2040 ADC1
"A2": 28, # RP2040 ADC2
"A3": _EXT | 0, # MAX11635 AIN0
"A4": _EXT | 1, # MAX11635 AIN1
"A5": _EXT | 2, # MAX11635 AIN2
}

Every pin name on the board and the RP2040 GPIO it connects to.

A3-A5 have no RP2040 GPIO; their values are markers for the MAX11635 inputs and are not GPIO numbers. Use gpio() to look a name up.