User Tools

Site Tools


connect_pico_to_wifi

This is an old revision of the document!


Connect Pico to Wifi

Aug 2026


Introduction


A very common activity for a Raspberry Pi Pico is to connect it to the Internet via WiFi (it has to be a Pico 1/2 'W' variant for this to work' Because a Pico does not have an OS of sorts, there is no built in UI to use to connect to WIFI, so we have to do this using a script.

Now, you can do this at boot, but if you lose WIFI you might need to reboot. You could define a function that you call every say 6 hours to connect to the WIFI so you know it is always connected. Or you could use a button to force connection when you press it.

After this, your main issue is working out what IP Address the Pico now has. That is something we could solve with say a small LCD Screen.


WIFI Script


Below is the script for connecting to a WIFI network. You can save this as main.py, so it runs on boot, and the rest of your code will have to be in main.py also. This is what we will do here, but later on, in another module, we will look at how to define this as a subroutine using a def statement.

Below is the WIFI connect code, it is quite short, it seems long because of the comments, but when you understand the code you can remove them.

| download
### Connect Pico to Wifi ###
 
#Import the network module
#The Pico W uses the built‑in CYW43439 Wi‑Fi chip, controlled through MicroPython’s network module.
import network
import time
 
 
#Create a WLAN station interface
#This puts the Pico W into Wi‑Fi client mode so it can join your router.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
 
#Enter your Wi‑Fi SSID and password
#You must provide your router’s network name and password.
ssid = "YOUR-SSID"
password = "YOUR-WIFI-PASSWORD"
wlan.connect(ssid, password)
 
 
#Wait for connection
#The Pico W needs a few seconds to negotiate with your router.
 
#Add a simple wait loop:
while not wlan.isconnected():
    print("Connecting...")
    time.sleep(0.5)
 
#Print your IP address
#This confirms the Pico W is online and shows the IP assigned by your router.
print("Connected!")
print(wlan.ifconfig())

For comparison, here is the same code with no comments:

| download
import network
import time
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
ssid = "YOUR-SSID"
password = "YOUR-WIFI-PASSWORD"
wlan.connect(ssid, password)
 
while not wlan.isconnected():
    print("Connecting...")
    time.sleep(0.5)
print("Connected!")
print(wlan.ifconfig())



Running the Code


I have called my script wifi-connect.py because if I run it at boot, I may not see the IP Address, I need to test this idea.

When I run my wifi-connect.py from Thonny, this is what I see:


connect_pico_to_wifi.1786968348.txt.gz · Last modified: by walkeradmin