Raspberry Pi Pico
內建LED開關
from machine import Pin
import utime
led= Pin("LED", Pin.OUT)
led.on()
utime.sleep(2)
led.off()
內建LED閃爍
from machine import Pin
import utime
led= Pin("LED", Pin.OUT)
while True:
led.toggle()
utime.sleep(0.5)
LED紅綠燈
from machine import Pin
import utime
R = Pin(16, Pin.OUT)
Y = Pin(17, Pin.OUT)
G = Pin(18, Pin.OUT)
R.off()
Y.off()
G.off()
while True:
#紅燈亮3秒
R.on()
utime.sleep(3)
R.off()
#黃燈閃3秒
for i in range(6):
Y.on()
utime.sleep(0.5)
Y.off()
utime.sleep(0.5)
#綠燈亮3秒
G.value(1)
utime.sleep(3)
G.value(0)
PWM控制LED漸亮
from machine import Pin,PWM
import utime
led= PWM(Pin(17))
led.freq(1000) # set frequency
i=0
while True:
led.duty_u16(i) # set duty cycle, range 0-65535
utime.sleep(0.3)
i+=5000
if i > 65535:
i=0
PWM蜂鳴器
from machine import Pin,PWM
import utime
bee= PWM(Pin(17))
bee.freq(1000) # set frequency 音頻高低
def buzz():
bee.duty_u16(51200) # set duty cycle, range 0-65535 音量大小
utime.sleep(0.5)
bee.deinit() #中斷
utime.sleep(0.5)
while True:
buzz()
按鈕開關控制內建LED燈
0----2
| \
1----3
0、2腳連通;1、3腳連通;按鈕壓下時0~3連通
GPIO輸入下拉式電阻接法:
PIN設定為輸入,啟用內建下拉電阻接地,按鈕另一端接3.3V
按鈕未按下時為0,按下時為1
from machine import Pin
import utime
button = Pin(18, Pin.IN ,Pin.PULL_DOWN)
led = Pin("LED", Pin.OUT)
while True:
print(button.value())
if button.value() == 1:
led.toggle()
utime.sleep(0.2)
GPIO輸入上拉式電阻接法:
PIN設定為輸入,啟用內建上拉電阻接電源,按鈕另一端接地
按鈕未按下時為1,按下時為0
from machine import Pin
import utime
button = Pin(18, Pin.IN ,Pin.PULL_UP) #啟用上拉式電阻
led = Pin("LED", Pin.OUT)
while True:
print(button.value())
if button.value() == 0:
led.toggle()
utime.sleep(0.2)
HC-SR04超音波感測器
VCC-GPIO40
GND-GPIO38
TRIG-GPIO15
ECHO-GPIO14
from machine import Pin
import utime
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
def ultra():
#暫停兩毫秒,確保上一個設定低電位已經完成
utime.sleep_us(2)
trigger.high()
#拉高電位後,等待5毫秒後,立即設定為低電位
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
#建立一個while迴圈檢查回波引腳是否值為0,紀錄當時時間
signaloff = utime.ticks_us()
while echo.value() == 1:
#建立一個while迴圈檢查回波引腳是否值為1,紀錄當時時間
signalon = utime.ticks_us()
#計算發送與接收時間差
timepassed = signalon - signaloff
#聲波行進時間 x 聲速(343.2 m/s,即每微秒0.0343公分),來回距離再除以2
distance = (timepassed * 0.0343) / 2
print("The distance is : ",distance,"cm")
while True:
ultra()
#等待1秒
utime.sleep(1)
https://koding.work/use-raspberry-pi-pico-hc-sr04-to-detect-distance/
被動紅外線偵測(PIR)
Time Delay(右):逆時針轉到底(最小)
Sensitivity(左):敏感度(理論值3~7m)
JUMP(L):Single Triggle
JUMP(R):Repeat Triggle
from machine import Pin
import utime
pir = Pin(20, Pin.IN, Pin.PULL_DOWN) #為避免“浮動”輸入,使用內部下拉電阻
led = Pin("LED", Pin.OUT)
n = 0
print('啟動紅外線感測')
utime.sleep(1)
print('Ready')
while True:
if pir.value() == 1:
led.on()
n = n+1
print('Motion Detected',n)
else:
led.off()
utime.sleep(1)
LCD1602
VCC--GPIO40(VBUS)
GND--GND
SDA--GPIO00
SCL--GPIO01
將pico_i2c_lcd.py與lcd_api.py存放於pico中
import machine
import utime
from pico_i2c_lcd import I2cLcd
i2c=machine.I2C(0,sda=machine.Pin(0),scl=machine.Pin(1),freq=400000)
I2C_ADDR=i2c.scan()[0]
lcd=I2cLcd(i2c,I2C_ADDR,2,16)
print(i2c.scan())
lcd.putstr("Hello\n")
lcd.putstr("World")
函式庫下載
DHT11
DHT11電壓可接3.3V~5V
from machine import Pin
import utime
from dht import DHT11, InvalidChecksum
# Wait 1 second to let the sensor power up
utime.sleep(1)
pin = Pin(22, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)
while True:
try:
print("Temperature: {}".format(sensor.temperature))
print("Humidity: {}".format(sensor.humidity))
utime.sleep(3)
except InvalidChecksum:
print("Checksum from the sensor was invalid")
函式庫:
專題-1602顯示溫溼度
import machine
import utime
from pico_i2c_lcd import I2cLcd
from dht import DHT11, InvalidChecksum
i2c=machine.I2C(0,sda=machine.Pin(0),scl=machine.Pin(1),freq=400000)
I2C_ADDR=i2c.scan()[0]
lcd=I2cLcd(i2c,I2C_ADDR,2,16)
utime.sleep(2)
pin = machine.Pin(22, machine.Pin.OUT, machine.Pin.PULL_DOWN)
sensor = DHT11(pin)
while True:
try:
t="Temperature:" + str(sensor.temperature) +"\n"
h="Humidity:"+ str(sensor.humidity)
lcd.clear()
lcd.putstr(t)
lcd.putstr(h)
print(t)
print(h)
utime.sleep(3)
except InvalidChecksum:
print("Checksum from the sensor was invalid")
共陰極七段顯示器
import machine
a = machine.Pin(10, machine.Pin.OUT)
b = machine.Pin(11, machine.Pin.OUT)
c = machine.Pin(12, machine.Pin.OUT)
d = machine.Pin(13, machine.Pin.OUT)
e = machine.Pin(14, machine.Pin.OUT)
f = machine.Pin(15, machine.Pin.OUT)
g = machine.Pin(16, machine.Pin.OUT)
p = machine.Pin(17, machine.Pin.OUT)
a.value(1)
b.value(1)
c.value(1)
d.value(1)
e.value(1)
f.value(1)
g.value(1)
p.value(0)
g.value(0)
共陰極七段顯示器-陣列版
import machine
import utime
k=[0]*8
k[0] = machine.Pin(10, machine.Pin.OUT) #a腳位
k[1] = machine.Pin(11, machine.Pin.OUT) #b腳位
k[2] = machine.Pin(12, machine.Pin.OUT) #c腳位
k[3] = machine.Pin(13, machine.Pin.OUT) #d腳位
k[4] = machine.Pin(14, machine.Pin.OUT) #e腳位
k[5] = machine.Pin(15, machine.Pin.OUT) #f腳位
k[6] = machine.Pin(16, machine.Pin.OUT) #g腳位
k[7] = machine.Pin(17, machine.Pin.OUT) #p腳位(小數點)
n=[[0]*8]*16
n[0]=[1,1,1,1,1,1,0,0]
n[1]=[0,1,1,0,0,0,0,0]
n[2]=[1,1,0,1,1,0,1,0]
n[3]=[1,1,1,1,0,0,1,0]
n[4]=[0,1,1,0,0,1,1,0]
n[5]=[1,0,1,1,0,1,1,0]
n[6]=[1,0,1,1,1,1,1,0]
n[7]=[1,1,1,0,0,0,0,0]
n[8]=[1,1,1,1,1,1,1,0]
n[9]=[1,1,1,1,0,1,1,0]
n[10]=[1,1,1,0,1,1,1,0] #A
n[11]=[0,0,1,1,1,1,1,0] #b
n[12]=[1,0,0,1,1,1,0,0] #C
n[13]=[0,1,1,1,1,0,1,0] #d
n[14]=[1,0,0,1,1,1,1,0] #E
n[15]=[1,0,0,0,1,1,1,0] #F
for i in range(16):
for j in range(8):
k[j].value(n[i][j])
utime.sleep(1)
專題-叫號器
import machine
import utime
bee= machine.PWM(machine.Pin(8))
def buzz():
bee.freq(1000) # set frequency 音頻高低
bee.duty_u16(51200) # set duty cycle, range 0-65535 音量大小
utime.sleep(0.5)
bee.freq(800) # set frequency 音頻高低
bee.duty_u16(51200) # set duty cycle, range 0-65535 音量大小
utime.sleep(0.5)
bee.deinit()
utime.sleep(0.5)
k=[0]*8
k[0] = machine.Pin(10, machine.Pin.OUT)
k[1] = machine.Pin(11, machine.Pin.OUT)
k[2] = machine.Pin(12, machine.Pin.OUT)
k[3] = machine.Pin(13, machine.Pin.OUT)
k[4] = machine.Pin(14, machine.Pin.OUT)
k[5] = machine.Pin(15, machine.Pin.OUT)
k[6] = machine.Pin(16, machine.Pin.OUT)
k[7] = machine.Pin(17, machine.Pin.OUT)
button = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_DOWN)
n=[[0]*8]*16
n[0]=[1,1,1,1,1,1,0,0]
n[1]=[0,1,1,0,0,0,0,0]
n[2]=[1,1,0,1,1,0,1,0]
n[3]=[1,1,1,1,0,0,1,0]
n[4]=[0,1,1,0,0,1,1,0]
n[5]=[1,0,1,1,0,1,1,0]
n[6]=[1,0,1,1,1,1,1,0]
n[7]=[1,1,1,0,0,0,0,0]
n[8]=[1,1,1,1,1,1,1,0]
n[9]=[1,1,1,1,0,1,1,0]
n[10]=[1,1,1,0,1,1,1,0]
n[11]=[0,0,1,1,1,1,1,0]
n[12]=[1,0,0,1,1,1,0,0]
n[13]=[0,1,1,1,1,0,1,0]
n[14]=[1,0,0,1,1,1,1,0]
n[15]=[1,0,0,0,1,1,1,0]
i=0
while True:
for j in range(8):
k[j].value(n[i][j])
if button.value()==1:
buzz()
i+=1
if i>=16:i=0
utime.sleep(0.2)
MAX7219 8x8 點矩陣Led
* MAX7219 VCC pin to VBUS
* MAX7219 GND pin to GND
* MAX7219 DIN pin to digital GPIO3
* MAX7219 CS pin to digital GPIO5
* MAX7219 CLK pin to digital GPIO2
文字僅能顯示ASCII字元
from machine import Pin, SPI
import max7219
import utime
#Intialize the SPI
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
cs = Pin(5, Pin.OUT)
display = max7219.Matrix8x8(spi, cs, 1)
#設定亮度,數值 1 to 15.
display.brightness(1)
#清除畫面
display.fill(0)
text="Augus"
while True:
for i in text:
display.text(i,0,0,1) # (string, x, y, col=1)
display.show()
utime.sleep(1)
display.fill(0)
函式庫