---
# System prepended metadata

title: Raspberry Pi pico

---

# Raspberry Pi pico
[參考來源](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html#raspberry-pi-pico)
    
    
## 軟體
* [Thonny](https://thonny.org/)(使用micropython)
    * 支援更新pico韌體
* [Arduino Lab for micropython](https://labs.arduino.cc/en/labs/micropython) (使用micropython)
* [Viusual Studio Code](https://code.visualstudio.com/)
* [Arduino IDE](https://www.arduino.cc/en/software) (使用C)


## 硬體
* pico![](https://i.imgur.com/kCnw8SM.png)
    * [Download Fritzing Part for Raspberry Pi Pico](https://datasheets.raspberrypi.com/pico/Pico-R3-Fritzing.fzpz)
    * [Download the Pinout Diagram ](https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf)

* pico w ![](https://i.imgur.com/Vtk5tCp.png)
    * [Download Fritzing Part for Rapsberry Pi Pico W](https://datasheets.raspberrypi.com/picow/PicoW-Fritzing.fzpz)
    * [Download the Pinout Diagram (PDF)](https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf)





* Resetting Flash memory(重置)
    * [下載重置用檔案UF2](https://datasheets.raspberrypi.com/soft/flash_nuke.uf2)
    * 按住BOOTSEL按鈕後，再插入micro usb線，即可啟動為Mass Storage模式，將上述UF2檔案拖曳進該儲存空間後，pico即自動重置

## Micropython([https://micropython.org/](https://micropython.org/))
* [micro python for rpi pico語法](https://docs.micropython.org/en/latest/rp2/quickref.html)
* 恢復支援micropython，下載下列檔案後，如上述按住BOOTSEL按鈕後，再插入micro usb線，即啟動Mass Storage模式，再將下列韌體拖曳進去後即可
* [rp2-pico micropython firmware](https://micropython.org/download/rp2-pico/)
* [rp2-pico w micropython firmware](https://micropython.org/download/rp2-pico-w/)

# 第一次使用rpi-pico
接上usb時，預設為Mass Storage模式
![](https://i.imgur.com/GLDlyAH.png)


---
# **Micro python**
# 使用Thonny編輯:
  使用右下角更新韌體或連線rpi pico w
  ![](https://i.imgur.com/oGh7u5B.png)
  ![](https://i.imgur.com/SeNQTgj.png)
  ![](https://i.imgur.com/5J5LGfN.png)
  # Blink
  ## 使用內建LED(WL_GPIO0) [參考來源3.4節](https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf)
  ```
  import machine
import time

led=machine.Pin("LED",machine.Pin.OUT)  #使用板子上內建的LED(WL_GPIO0)
while True:
    led.off()
    time.sleep(1)
    led.on()
    time.sleep(1)
  ```
## 使用arduino IDE
![](https://hackmd.io/_uploads/S1MdWNFYn.png)
 
  
  ## 使用GPIO14，外接LED
  ```
  from machine import Pin
  import time
  
  led=Pin(14,Pin.OUT)  #使用gpio 14
  while True:
      led.value(1)
      time.sleep(1)
      led.value(0)
      time.sleep(1)
  ```
  ## PWM(使用GPIO14)
 ```
import time
from machine import Pin, PWM

pwm=PWM(Pin(14))  #建立pwm於GPIO14
pwm.freq(1000)    #設定pwm頻率

duty=0
direction=1
for _ in range(8 *256):  #fade in fade out 4次 ； _ in range為不使用變數寫法，如同 for i in range(8*256)，這裡需設定一變數i
    duty+=direction
    if duty >255:
        duty=255
        direction=-1
    elif duty < 0:
        duty=0
        direction=1
    pwm.duty_u16(duty * duty)
    time.sleep(0.001)  # fade速度
 ```
 
 
  
  
 ## 連接WiFi [參考來源1](https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf)  [參考來源2](https://docs.micropython.org/en/latest/library/network.WLAN.html)
 ```
import network
import time

ssid='DAP3690-24'  # Wifi SSID 名稱
password=''        # Wifi 密碼

wlan=network.WLAN(network.STA_IF)  #設定enable station interface and connect to WiFi access point
wlan.active(True)
wlan.connect(ssid,password)

max_wait=10  # wait for connect or fail
while max_wait > 0 :
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('Wait for connection...')
    time.sleep(1)
    
if wlan.status() !=3 : #Handle onnection error
    raise RuntimeError('Network connect failed!')
else:
    print('Connected!')
    status=wlan.ifconfig()
    print('IP Address=' + status[0])
    print('Netmask=' + status[1])
    print('Gateway=' + status[2])
    print('DNS=' + status[3])

 ```


# **===========**
---

# **C/C++**
# 使用arduino IDE作為編輯器時:
* 設定環境
    * 於Preferences Additional boards manager URL填https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
    ![](https://i.imgur.com/LDDdtHY.png)
    ![](https://i.imgur.com/tiPM70o.png)
    



1. 開啟arduino IDE ![](https://i.imgur.com/UXCZfo4.png)
2. 選擇開發版(這裡使用pico W) ![](https://i.imgur.com/MQUYFVr.png)
3. 選擇連接埠(第一次顯示UF2 Board) ![](https://i.imgur.com/Zs8YG1J.png)
4. 按下upload ![](https://i.imgur.com/gZw9jtc.png)
5. 完成韌體變更後，即可看到com port出現，之後即可使用arduino IDE編撰。
6. 需要回復為micro python編譯環境，則再使用上面Restting flash memory方式，利用flash_nuke.uf2重置，再更新micro python韌體即可。

# pico連線WiFi(與ESP32-s寫法一樣)

```
#include <WiFi.h>
char ssid[] = "   ";       //wifi SSID
char passwd[] = "   ";    //wifi passwd

void setup()
{
  Serial.begin(115200);
  WifiConnecte();      //Wifi開始連線
}

void loop()
{
  
}

 void WifiConnecte()
 {
   int connect_count=0; //宣告嘗試連線次數=0
   WiFi.begin(ssid,passwd);
   Serial.print("等待連線");
   while (WiFi.status() != WL_CONNECTED)
   {
     delay(200);
     Serial.print("..");  //等待連線中
     delay(100);
     if(connect_count++ >= 10)
     {
       watchdog_reboot(0,0,10);   //如果嘗試連線次數累加至10次，則重新開機
     }     
   }
   Serial.println("Wifi連線成功");
   Serial.print("IP Address:");
   Serial.println(WiFi.localIP());
 } 

```

# 使用DHT11
DHT11資料pin接至gpio15
使用 dht sensor for Adafruit
```
#include "DHT.h"

#define DHTPIN 15 // 將DHT11 DATA連接的引腳
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  delay(2000);

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("無法從DHT11感測器讀取數據！");
    return;
  }

  Serial.print("溫度: ");
  Serial.print(temperature);
  Serial.print(" *C\t濕度: ");
  Serial.print(humidity);
  Serial.println(" %");
}
```