This post shows you how to connect the Pico W to a wireless network. For displaying the connection status, an I2C OLED (4 pins) is used. Especially, I will show you the issues that may occur in practice when Pico W connects to a wireless network. Finally, some simple solutions to deal with the issues will be presented. Let’s get started.
I. Connecting to an I2C OLED display
Firstly, we need to set up Pico W and the I2C OLED display. You simply connect the pins of Pico W and the pins of the OLED display as follows:
- Pin GPIO0 (Pico W) -> Pin SDA (OLED)
- Pin GPIO1 (Pico W) -> Pin SCL (OLED)
- Vcc (Pico W) -> Vcc (OLED)
- GND (Pico W) -> GND (OLED)
The following picture illustrates the connections on my break board. It is noted that my I2C OLED is the SSD1306 OLED display module.
Next, open your Thonny and create a python file, namely SSD1306.py ( its name depends on you). Next, you simply copy and paste the code below to check the Pico W and OLED work.
1 2 3 4 5 6 7 8 |
from machine import Pin, I2C from ssd1306 import SSD1306_I2C i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) oled = SSD1306_I2C(128, 64, i2c) oled.text("luuvachiase.net", 0, 0) oled.show() |
Firstly, the ssd1306 library needs to be imported, as shown in line 1 of the code above. Next, pin(0) and pin(1) of Pico W are defined to connect to pin (SDA) and pin(SCL) of SSD1306 OLED, respectively. Finally, we mount an object, namely “oled” to be used for the rest of our python code. For example, you only used line numbers 7 and 8 to display a text string.
Here are my results,

If you get the same, congratulations! You can go to the next step in this project.
II. Connecting to a wireless network
Connection status codes:
1 2 3 4 5 6 7 8 |
// Return value of cyw43_wifi_link_status #define CYW43_LINK_DOWN (0) #define CYW43_LINK_JOIN (1) #define CYW43_LINK_NOIP (2) #define CYW43_LINK_UP (3) #define CYW43_LINK_FAIL (-1) #define CYW43_LINK_NONET (-2) #define CYW43_LINK_BADAUTH (-3) |
This code is copied from the documentation of the RP2040 microcontroller. However, they are defined in C++. We will use these codes to display the status of the wifi connection. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def switch(WL_code): if WL_code == 0: return "Link down" elif WL_code == 1: return "Link join" elif WL_code == 2: return "Link NoIP" elif WL_code == 3: return "Link up" elif WL_code == -1: return "Link fail" elif WL_code == -2: return "Link Nonet" elif WL_code == -3: return "Wrong P&S" |
Next, create a python file, namely wireless.py, and save it on Raspberry Pico. Then, add the necessary libraries as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import machine import network import secret import time from picozero import pico_temp_sensor from time import sleep from OLED1306 import * wlan = network.WLAN(network.STA_IF) wlan.active(True) stt = 0 |
In the code above, the libraries: machine, network, and time are the pre-build libraries. Regarding the secret, it is a separate file that contains the secret information like SSID and PASSWORD of the wireless network. You can create the file secret.py (save on Raspberry Pico) as follows:
1 2 |
SSID = "XXX-XXX" PASSWORD = "XXX-XXX" |
Next, we will define a function that will be used to connect to the wireless network.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def WL_connect(): wlan.connect(secret.SSID, secret.PASSWORD) max_wait = 10 while max_wait >0: oled.fill(0) oled.text(secret.SSID, 0, 0) stt = wlan.status() oled.text(switch(stt), 0, 40) oled.text("connecting..."+str(max_wait), 0, 10) print("WL code:"+str(stt)) oled.show() if stt <0 or stt >=3: break max_wait -=1 time.sleep(1) return stt |
In this function, we define a variable max_wait = 10 to check whether the connection is successful or failed. Simultaneously, this function prints the status code on the OLED display.
Finally, we complete the code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import machine import network import secret import time from picozero import pico_temp_sensor from time import sleep from OLED1306 import * wlan = network.WLAN(network.STA_IF) wlan.active(True) stt = 0 def WL_connect(): wlan.connect(secret.SSID, secret.PASSWORD) max_wait = 10 while max_wait >0: stt = wlan.status() oled.fill(0) oled.text(secret.SSID, 0, 0) oled.text(switch(stt), 0, 40) oled.text("connecting..."+str(max_wait), 0, 10) oled.show() print(str(stt)) if stt <0 or stt >=3: break max_wait -=1 time.sleep(1) return stt # handle connection error def switch(WL_code): if WL_code == 0: return "Link down" elif WL_code == 1: return "Link join" elif WL_code == 2: return "Link NoIP" elif WL_code == 3: return "Link up" elif WL_code == -1: return "Link fail" elif WL_code == -2: return "NoIP" elif WL_code == -3: return "Wrong P&S" oled.fill(0) while True: if(WL_connect()==3): break |
We place the WL_connect() function in a while loop. Therefore, it will retry to connect to the wireless network until it achieves a successful connection (code = 3).
These are two videos that illustrate two scenarios. The first one is a successful connection. In the second case, we first unplug the access point (AP) to observe the behaviors of Pico W via the status displayed on the OLED. Then, we plug the AP into the power supply and achieve a successful connection.
-
-
- Case 1:
- Case 2:
-
The access point is unplugged in the second video before Pico W starts. Then, at the 50th second, the access point was plugged. As expected, Pico W connected to the access point successfully, Link up!.
These results have demonstrated that the WL_connect() function works well. In the next post, we will use this function to fix the issue that occurred in the case of uploading data from Pico W to the internet.