import digitalio
from board import *
import time
# use Onboard LED on the XIAO board
led = digitalio.DigitalInOut(D13)
led.direction = digitalio.Direction.OUTPUT
try:
while True: # press Ctrl+C to stop
led.value = True
time.sleep(0.1)
led.value = False
time.sleep(0.1)
except KeyboardInterrupt:
pass
led.value = 0 # LED output level LOW
led.deinit() # release the LED pin (D13)
print('Done')
import digitalio
from board import *
import time
led = digitalio.DigitalInOut(D13)
led.direction = digitalio.Direction.OUTPUT
try:
t = int(1000*time.monotonic()) # in msec
while True: # press Ctrl+C to stop
now = int(1000*time.monotonic()) # in msec
# toggle LED every 100 msec
if now - t >= 100:
t = now
led.value = not led.value
except KeyboardInterrupt:
pass
led.value = 0
led.deinit()
print('Done')
import pwmio
from board import *
import time
pwm = pwmio.PWMOut(pin=D13,duty_cycle=0,frequency=10)
pwm.duty_cycle = (2**15 - 1) # (16-bit value) -> 50%
try:
while True:
time.sleep(1.0)
except KeyboardInterrupt:
pass
pwm.duty_cycle = 0 # set duty cycle to 0
pwm.deinit() # relase the PWM output pin
print('Done')
import board
import neopixel_write
import digitalio
import time
np_pin = digitalio.DigitalInOut(board.D10)
np_pin.direction = digitalio.Direction.OUTPUT
NUM_PIXELS = 8
for i in range(256):
colors = bytearray(NUM_PIXELS*[0,255-i,0]) # GRB
neopixel_write.neopixel_write(np_pin, colors)
time.sleep(0.01)
np_pin.deinit() # release the output pin
import board
import time
import neopixel
NUM_PIXELS = 8
# bpp=3 (3 bytes per pixel), pixel order GRB
pixels = neopixel.NeoPixel(board.D10, NUM_PIXELS,
bpp=3, pixel_order=neopixel.GRB, auto_write=False)
color = (255, 0, 0) # red, green, blue
pixels[0] = color
for i in range(NUM_PIXELS): # turn on (use red color)
pixels[i] = color if i==0 else pixels[i-1]
pixels.show()
time.sleep(0.1)
pixels.fill( (0,255,0) ) # change all pixels to green
for i in range(101): # fade the RGB LEDS off
pixels.brightness = (100-i)/100.0
pixels.show()
time.sleep(0.02)
time.sleep(2.0)
# turn off the Neopixel strip and release pin
pixels.deinit()
del pixels
โค้ดตัวอย่างที่ 3: DHT22 Sensor Reading
import time
import board
from adafruit_dht import DHT22
dht = DHT22(pin=board.D9,use_pulseio=True)
try:
text = '{:.1f} deg.C, {:.1f} %RH'
while True: # press Ctrl+C to stop
dht.measure() # perform measurement
values = (dht.temperature, dht.humidity)
print( text.format(*values) )
time.sleep(2.0)
except KeyboardInterrupt:
pass
dht.exit()
del dht
ในการต่อวงจรทดลอง ได้เลือกใช้ขา D9 สำหรับต่อกับขา DATA ของโมดูล DHT22 (ใช้แรงดันไฟเลี้ยง +3.3V)
โค้ดตัวอย่างที่ 4: SI7021 Sensor Reading
adafruit_bus_device/i2c_device
adafruit_bus_device/spi_device
import time
from busio import I2C
from board import SCL, SDA
import adafruit_si7021 as si7021
SI7021_ADDR = 0x40
i2c = I2C(SCL, SDA,frequency=100000) # 100kHz speed
addr_found = []
if i2c.try_lock():
addr_found = i2c.scan()
print("I2C found:", [hex(addr) for addr in addr_found])
i2c.unlock()
if SI7021_ADDR not in addr_found:
print('SI7021 not found')
dev = si7021.SI7021(i2c,address=SI7021_ADDR)
text = '{:.1f} deg.C, {:.1f} %RH'
try:
while True: # press Ctrl+C to stop
values = (dev.temperature,dev.relative_humidity)
print( text.format(*values) )
time.sleep(1.0)
except OSError:
print('Sensor reading error!')
except KeyboardInterrupt:
pass
finally:
i2c.deinit()
print('Done')
โค้ดตัวอย่างที่ 5: BME680 Sensor Reading
import time
from busio import I2C
from board import SCL, SDA
import adafruit_bme680 as bme680
BME680_ADDR = 0x77
i2c = I2C(SCL, SDA,frequency=100000) # 100kHz speed
addr_found = []
if i2c.try_lock():
addr_found = i2c.scan()
print("I2C found:", [hex(addr) for addr in addr_found])
i2c.unlock()
if BME680_ADDR not in addr_found:
print('BME680 not found')
dev = bme680.Adafruit_BME680_I2C(i2c,address=BME680_ADDR)
text = '{:.1f} deg.C, {:.1f} %RH, {:.2f} hPa'
try:
while True: # press Ctrl+C to stop
t = dev.temperature # deg.C
h = dev.relative_humidity # %RH
p = dev.pressure # hPa
print( text.format(t,h,p) )
time.sleep(1.0)
except OSError:
print('Sensor reading error!')
except KeyboardInterrupt:
pass
finally:
i2c.deinit()
print('Done')
import time
import board
from analogio import AnalogIn
from microcontroller import pin
## pin map for Seeeduino-XIAO
## A0=PA02, A1=PA04, A2=PA10, A3=PA11, A4=PA08, A5=PA09,
## A6=PB08, A7=PA09, A8=PA07, A9=PA05, A10=PA06
## For Seeeduino XIAO: A1 and PA04 are the same pin.
ain = AnalogIn(board.A1) # use A1 pin
#ain = AnalogIn(pin.PA04) # use PA04 pin
last_btn = 0
NUM_SAMPLES = 5
last_t = int(1000*time.monotonic())
try:
while True:
now = int(1000*time.monotonic())
if now - last_t >= 50:
last_t = now
else:
continue
values = []
for i in range(NUM_SAMPLES):
# read a 16-bit value from the analog pin
value = ain.value >> 6 # reduce to 10 bits
values.append(value)
# apply the median filter
value = sorted(values)[NUM_SAMPLES//2]
if value > 600: # no button press
last_btn = 0
continue
btn = 0
if value < 30:
btn = 1
elif value < 60:
btn = 2
elif value < 120:
btn = 3
elif value < 240:
btn = 4
elif value > 320 and value < 450:
btn = 5
if last_btn != btn and btn > 0:
print( 'SW{} ({})'.format(btn, value) )
last_btn = btn
except KeyboardInterrupt:
pass
ain.deinit() # release the analog pin
print('Done')
ถ้าใช้บอร์ด XIAO ขา PA18 และ PA19 ต่อกับวงจร LED สีน้ำเงินที่มีอยู่แล้วบนบอร์ด
import time
import pwmio
import board
from microcontroller import pin
import math
led_pins = [pin.PA18, pin.PA19]
pwm_objects = [
pwmio.PWMOut(pin=pin,duty_cycle=65535,frequency=250)
for pin in led_pins ]
# create a table of (N+1) sine wave values
N=64
sine_int = lambda i: int(65535*(1-math.sin(math.pi*i/N)))
values = [sine_int(i) for i in range(N+1)]
try:
while True:
for pwm in pwm_objects:
for i in range(N+1):
pwm.duty_cycle = values[i]
time.sleep(0.04)
except KeyboardInterrupt:
pass
for pwm in pwm_objects:
pwm.deinit()
print('Done')