@@ -132,7 +132,7 @@ Use the :ref:`machine.Pin <machine.Pin>` class::
132
132
print(p2.value()) # get value, 0 or 1
133
133
134
134
p4 = Pin('D4', Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
135
- p7 = Pin(" PA07" , Pin.OUT, value=1) # set pin high on creation
135
+ p7 = Pin(' PA07' , Pin.OUT, value=1) # set pin high on creation
136
136
137
137
Pins can be denoted by a string or a number. The string is either the
138
138
pin label of the respective board, like "D0" or "SDA", or in the form
@@ -157,7 +157,7 @@ See :ref:`machine.UART <machine.UART>`. ::
157
157
# Use UART 3 on a ItsyBitsy M4 board
158
158
from machine import UART
159
159
160
- uart3 = UART(3, tx=Pin(1 ), rx=Pin(0 ), baudrate=115200)
160
+ uart3 = UART(3, tx=Pin('D1' ), rx=Pin('D0' ), baudrate=115200)
161
161
uart3.write('hello') # write 5 bytes
162
162
uart3.read(5) # read up to 5 bytes
163
163
@@ -178,11 +178,12 @@ It supports all basic methods listed for that class. ::
178
178
179
179
from machine import Pin, PWM
180
180
181
- pwm = PWM(Pin(7)) # create PWM object from a pin
182
- pwm.freq() # get current frequency
183
- pwm.freq(1000) # set frequency
184
- pwm.duty_u16() # get current duty cycle, range 0-65535
185
- pwm.duty_u16(200) # set duty cycle, range 0-65535
181
+ # create PWM object from a pin and set the frequency and duty cycle
182
+ pwm = PWM(Pin('D7'), freq=2000, duty_u16=32768)
183
+ pwm.freq() # get the current frequency
184
+ pwm.freq(1000) # set/change the frequency
185
+ pwm.duty_u16() # get the current duty cycle, range 0-65535
186
+ pwm.duty_u16(200) # set the duty cycle, range 0-65535
186
187
pwm.deinit() # turn off PWM on the pin
187
188
188
189
pwm # show the PWM objects properties
@@ -245,9 +246,9 @@ Use the :ref:`machine.ADC <machine.ADC>` class::
245
246
246
247
from machine import ADC
247
248
248
- adc0 = ADC(Pin("A0" )) # create ADC object on ADC pin, average=16
249
+ adc0 = ADC(Pin('A0' )) # create ADC object on ADC pin, average=16
249
250
adc0.read_u16() # read value, 0-65536 across voltage range 0.0v - 3.3v
250
- adc1 = ADC(Pin("A1" ), average=1) # create ADC object on ADC pin, average=1
251
+ adc1 = ADC(Pin('A1' ), average=1) # create ADC object on ADC pin, average=1
251
252
252
253
The resolution of the ADC is 12 bit with 12 bit accuracy, irrespective of the
253
254
value returned by read_u16(). If you need a higher resolution or better accuracy, use
@@ -341,7 +342,7 @@ Software SPI (using bit-banging) works on all pins, and is accessed via the
341
342
# construct a SoftSPI bus on the given pins
342
343
# polarity is the idle state of SCK
343
344
# phase=0 means sample on the first edge of SCK, phase=1 means the second
344
- spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(7 ), mosi=Pin(9 ), miso=Pin(10 ))
345
+ spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin('D7' ), mosi=Pin('D9' ), miso=Pin('D10' ))
345
346
346
347
spi.init(baudrate=200000) # set the baud rate
347
348
@@ -388,7 +389,7 @@ accessed via the :ref:`machine.SoftI2C <machine.SoftI2C>` class::
388
389
389
390
from machine import Pin, SoftI2C
390
391
391
- i2c = SoftI2C(scl=Pin(10 ), sda=Pin(11 ), freq=100000)
392
+ i2c = SoftI2C(scl=Pin('D10' ), sda=Pin('D11' ), freq=100000)
392
393
393
394
i2c.scan() # scan for devices
394
395
@@ -424,7 +425,7 @@ The OneWire driver is implemented in software and works on all pins::
424
425
from machine import Pin
425
426
import onewire
426
427
427
- ow = onewire.OneWire(Pin(12 )) # create a OneWire bus on GPIO12
428
+ ow = onewire.OneWire(Pin('D12' )) # create a OneWire bus on GPIO12
428
429
ow.scan() # return a list of devices on the bus
429
430
ow.reset() # reset the bus
430
431
ow.readbyte() # read a byte
@@ -454,12 +455,12 @@ The DHT driver is implemented in software and works on all pins::
454
455
import dht
455
456
import machine
456
457
457
- d = dht.DHT11(machine.Pin(4 ))
458
+ d = dht.DHT11(machine.Pin('D4' ))
458
459
d.measure()
459
460
d.temperature() # eg. 23 (°C)
460
461
d.humidity() # eg. 41 (% RH)
461
462
462
- d = dht.DHT22(machine.Pin(4 ))
463
+ d = dht.DHT22(machine.Pin('D4' ))
463
464
d.measure()
464
465
d.temperature() # eg. 23.6 (°C)
465
466
d.humidity() # eg. 41.3 (% RH)
@@ -474,7 +475,7 @@ The APA102 on some Adafruit boards can be controlled using SoftSPI::
474
475
475
476
from machine import SoftSPI, Pin
476
477
# create the SPI object. miso can be any unused pin.
477
- spi=SoftSPI(sck=Pin(25 ), mosi=Pin(26 ), miso=Pin(14 ))
478
+ spi=SoftSPI(sck=Pin('D25' ), mosi=Pin('D26' ), miso=Pin('D14' ))
478
479
479
480
# define a little function that writes the data with
480
481
# preamble and postfix
@@ -499,7 +500,7 @@ with the Neopixel driver from the MicroPython driver library::
499
500
import machine
500
501
501
502
# 1 LED connected to Pin D8 on Adafruit Feather boards
502
- p = machine.Pin(8 , machine.Pin.OUT)
503
+ p = machine.Pin('D8' , machine.Pin.OUT)
503
504
n = neopixel.NeoPixel(p, 1)
504
505
505
506
# set the led to red.
0 commit comments