By Tseng
圖片來源: raspberrypi.org
Learn More →
Learn More →
啟動上拉電阻 python 語法
BOARD 選項是指定在電路版上接脚的號碼
python寫法
BCM 選項是指定GPIO後面的號碼
python寫法
http://bit.ly/3tB2okQ
```
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(17,gpio.OUT)
gpio.output(17,1)
time.sleep(1)
gpio.output(17,0)
gpio.cleanup()
```

http://bit.ly/3UE7EAk
```
import RPi.GPIO as io
import time
io.setmode(io.BCM)
io.setup(17,io.OUT)
while True:
try:
io.output(17,1)
time.sleep(1)
io.output(17,0)
time.sleep(1)
except KeyboardInterrupt:
break
pass
io.cleanup()
```
提示:使用串列list簡化程式
http://bit.ly/3AgD9rP

---
從暗到最亮,單向循環
http://bit.ly/3O5T16q
```
import RPi.GPIO as gpio
import time
led=17
gpio.setmode(gpio.BCM)
gpio.setup(led,gpio.OUT)
led=gpio.PWM(led,100) #設定pwm頻率100Hz
while True:
try:
for a in range(0,101,2):
led.start(a) # 啟動PWM DutyCycle,dutycycle從0~100,間隔2
time.sleep(0.2)
except KeyboardInterrupt:
break
pass
gpio.cleanup()
```
http://bit.ly/3tyE0Ao
按下按鈕時,終端機顯示 Button Pressed!!

```
# pull down 測試
import RPi.GPIO as gpio
import time
button1=26
gpio.setmode(gpio.BCM)
gpio.setup(button1,gpio.IN,pull_up_down=gpio.PUD_DOWN)
while True:
try:
input_state=gpio.input(button1)
if (input_state==True):
print('Button Pressed!!')
time.sleep(0.3)
except KeyboardInterrupt:
break
pass
gpio.cleanup()
```
##### 開關彈跳問題未解

{%youtube VKR4m-85HVU %}
```
# pull down 測試
import RPi.GPIO as gpio
import time
button1=13
button2=19
button3=26
gpio.setmode(gpio.BCM)
gpio.setup(button1,gpio.IN,pull_up_down=gpio.PUD_DOWN)
gpio.setup(button2,gpio.IN,pull_up_down=gpio.PUD_DOWN)
gpio.setup(button3,gpio.IN,pull_up_down=gpio.PUD_DOWN)
while True:
try:
in_state1=gpio.input(button1)
in_state2=gpio.input(button2)
in_state3=gpio.input(button3)
if in_state1==True:
print('Button1 Pressed!')
elif in_state2==True:
print('Button2 Pressed!')
elif in_state3==True:
print('Button3 Pressed!')
time.sleep(0.3)
except KeyboardInterrupt:
break
pass
gpio.cleanup()
```
安裝fswebcam
```
sudo apt-get install fswebcam
```
{%youtube xO0SJ8wduG0 %}
fswebcam用法可下指令查詢
```
fswebcam --help
```

最簡易的使用方式: fswebcam+ +檔名 即可透過webcam拍攝一張照片,並存放於當下目錄中
```
fswebcam test.jpg
```
{%youtube 5bMK6H2rCvE %}
---

提示: python呼叫外部程式
```
import os
os.system('指令')
```
{%youtube 6u6ET7vAqKk %}
因為python程式常有版本和不同套件的需求,因此須針對版本建立不同的python環境,以便於開發
首先須有python3-venv套件
安裝
建立虛擬環境
啟動環境
退出環境
燒錄img
燒錄完成後,將micro sd放回raspberry pi並開機
第一次開機預設帳密皆為ubuntu(登入後會要求變更)
檢視網卡狀態
啟動有線網卡
修改有線網卡ip( /etc/netplan 下的yaml檔)
修改此yaml檔內容
完成ip設定
修改無線網卡部分
修改同上yaml檔案
變更後重啟,wifi即可使用
安裝Desktop(檔案很大,需要一些時間)
This script will:
remove the existing version of Node-RED if present.
if it detects Node.js is already installed, it will ensure it is at least v14. If less than v14 it will stop and let the user decide whether to stay with Node-RED version 1 - or upgrade Nodejs to a more recent LTS version. If nothing is found it will install the Node.js 16 LTS release using the NodeSource package.
install the latest version of Node-RED using npm.
optionally install a collection of useful Pi-specific nodes.
setup Node-RED to run as a service and provide a set of commands to work with the service.
啟動Node-RED
OS:32bit
GitHUB
OS:64bit
GitHUB
安裝完後,opencv為4.6版
另一種安裝方式
上面指令若無法安裝;改用;參考
安裝後,opencv會更新至4.9版(2024.02.16.)
參考
1.直接把 EXTERNALLY-MANAGED這個檔案刪除(變更檔名)
檔案位置 /usr/lib/python3.x/EXTERNALLY-MANAGED (x為python版本)
另兩種方式請參閱上述參考連結
2.使用python虛擬環境(參閱上面)
RFID-RC522 | Raspberry pi |
---|---|
3.3V | 3.3V |
RST | GPIO25 |
GND | GND |
IRQ | GPIO24 |
MISO | GPIO9 |
MOSI | GPIO10 |
SCK | GPIO11 |
SDA | GPIO8 |
1.先建立opencv用虛擬環境
2.切換至虛擬環境
3.進入虛擬環境後,類似下圖
4.安裝opencv套件
5.程式碼請存放於py-opencv資料夾下