How to Connect a Raspberry Pi Pico W to WiFi with MicroPython

Raspberry Pi Pico W board

Getting a Raspberry Pi Pico W onto your WiFi network is the first step for almost every connected project, whether that's fetching the time from the internet, sending sensor readings somewhere or hosting a tiny web page. Below is the MicroPython code I use to connect a Raspberry Pi Pico W to WiFi and then blink the onboard LED, so you can see at a glance that it worked.

Tested on a Raspberry Pi Pico W.

What you need

  • A Raspberry Pi Pico W. The original Pico has no wireless chip, so this code won't work on it.
  • A micro-USB cable that carries data, not just power.
  • MicroPython installed on the Pico W and Thonny on your computer. If you haven't done this yet, see Get started with Raspberry Pi Pico.
  • Your WiFi network name (SSID) and password. The Pico W only supports 2.4 GHz WiFi.

MicroPython code

Replace YOUR SSID and YOUR PASSWORD with your own network details, then press Run in Thonny.

import network
import machine
import time

# Set SSID and password for your WiFi network
ssid = "YOUR SSID"
password = "YOUR PASSWORD"

# Create WLAN object
wlan = network.WLAN(network.STA_IF)

# Enable wireless
wlan.active(True)

# Connect to WiFi network
wlan.connect(ssid, password)

# Check connection status
while not wlan.isconnected():
    pass

# Print the connection status
print("Connected to WiFi")

#start your code here
pin = machine.Pin("LED", machine.Pin.OUT)

# blink LED
while True:
  pin.on()
  time.sleep(0.5)
  pin.off()
  time.sleep(0.5)

How the code works

  • network.WLAN(network.STA_IF) creates the WiFi interface in station mode, which means the Pico joins an existing network as a client. (The alternative, network.AP_IF, turns the Pico into its own access point.)
  • wlan.active(True) powers up the wireless chip.
  • wlan.connect(ssid, password) starts connecting. It returns straight away and the connection carries on in the background.
  • The while not wlan.isconnected() loop waits until the Pico has joined the network and been given an IP address.
  • Once connected, the onboard LED blinks every half a second. On the Pico W the LED is wired to the wireless chip rather than a normal GPIO pin, which is why it's referred to as "LED" instead of a pin number.

A more robust version with a timeout

The simple loop above waits forever, so if the password is wrong the Pico just sits there with no clue as to why. This version gives up after 20 seconds, tells you what went wrong and prints the IP address when it succeeds:

import network
import time

ssid = "YOUR SSID"
password = "YOUR PASSWORD"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

# Wait up to 20 seconds for the connection to succeed or fail
max_wait = 20
while max_wait > 0:
    status = wlan.status()
    if status < 0 or status >= 3:
        break
    print("Waiting for connection...")
    max_wait -= 1
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError("WiFi connection failed, status = " + str(wlan.status()))

print("Connected, IP address:", wlan.ifconfig()[0])

The number returned by wlan.status() tells you what happened:

  • 3 – connected and given an IP address
  • -1 – the connection failed
  • -2 – network not found (check the SSID, and that it's a 2.4 GHz network)
  • -3 – wrong password

Running it automatically at power-up

In Thonny, choose File > Save as, pick Raspberry Pi Pico as the location and save the file as main.py. MicroPython runs main.py every time the board starts, so the Pico will connect to WiFi on its own whenever it has power, even with no computer attached.

Troubleshooting

  • It never connects – the SSID and password are both case-sensitive, so check them carefully. The robust version above will tell you which one is the problem.
  • Network not found – the Pico W can't see 5 GHz-only networks. If your router uses separate names for each band, use the 2.4 GHz one.
  • Odd errors after updating code – unplug the Pico and plug it back in to reset the wireless chip, then run the code again.
  • Keep your password out of shared code – put the SSID and password in a separate file on the Pico (for example secrets.py) and import them, so you can share your main script without giving away your WiFi details.

Next steps

Now that the Pico W is online, try setting its clock from the internet with NTP, or go back to basics with the LED blink examples.

This post contains affiliate links. As an Amazon Associate I earn from qualifying purchases, at no extra cost to you.

Popular posts from this blog

How to Blink an LED on the Raspberry Pi Pico and Pico W (MicroPython)