Built-in project · Raspberry Pi Pico W

MicroPython WiFi station mode on a Pico W

MicroPython on a Raspberry Pi Pico W: network.WLAN joins Mokxi’s simulated access point and prints the IP address, mask and gateway it was given. The whole build, a Raspberry Pi Pico W, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $6. On Mokxi they're free.

MicroPython WiFi station mode on a Pico W · Raspberry Pi Pico Wlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Station mode on the Pico W
  • Joining a WiFi network from code
  • Waiting for a connection in a loop
  • What an IP address, mask and gateway are
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 1

How it works

This MicroPython program has no parts to wire. It turns on the Pico W's WiFi in station mode and connects to the network Mokxi-Lab with the password breadboard. It checks every 250 ms until the connection is up.

Then it prints the IP address, mask and gateway the network gave the board. After that, every 10 seconds, it prints whether it is still connected or the connection is lost.

Parts list

1 part, plus the jumper wires. They retail for about $6; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Station mode on a Pico W: join a WiFi network and print the address it
# gives the board. Here that is Mokxi's simulated access point, whose name
# and password are the board's WiFi properties; on a real board, put in
# your own network's.
import network
import time

SSID = "Mokxi-Lab"
PASSWORD = "breadboard"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting to", SSID)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
    time.sleep_ms(250)
ip, mask, gateway, dns = wlan.ifconfig()
print("Connected: IP", ip, "mask", mask, "gateway", gateway)

# then keep an eye on the link, as a program that uses it would
while True:
    time.sleep(10)
    print("still connected" if wlan.isconnected() else "connection lost")

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It stays on Connecting forever. This project
    The SSID and PASSWORD must match the network. On a real board, put in your own network's name and password.
  • It says connection lost. This project
    The link dropped. Code that depends on WiFi should check wlan.isconnected() and reconnect.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.